Comparing version 2.0.4 to 3.0.0
@@ -1,3 +0,3 @@ | ||
/// <reference path="types/index.d.ts" /> | ||
import { Feed } from "./feed"; | ||
declare const _default: (ins: Feed) => string; | ||
export default _default; |
142
lib/atom1.js
"use strict"; | ||
/// <reference path="types/index.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var xml = require("xml"); | ||
var config_1 = require("./config"); | ||
var DOCTYPE = '<?xml version="1.0" encoding="utf-8"?>\n'; | ||
var convert = require("xml-js"); | ||
exports.default = (function (ins) { | ||
var options = ins.options; | ||
var feed = []; | ||
feed.push({ _attr: { xmlns: "http://www.w3.org/2005/Atom" } }); | ||
feed.push({ id: options.id }); | ||
feed.push({ title: options.title }); | ||
if (options.updated) { | ||
feed.push({ updated: options.updated.toISOString() }); | ||
} | ||
else { | ||
feed.push({ updated: new Date().toISOString() }); | ||
} | ||
feed.push({ generator: options.generator || config_1.generator }); | ||
var base = { | ||
_declaration: { _attributes: { version: "1.0", encoding: "utf-8" } }, | ||
feed: { | ||
_attributes: { xmlns: "http://www.w3.org/2005/Atom" }, | ||
id: options.id, | ||
title: options.title, | ||
updated: options.updated ? options.updated.toISOString() : new Date().toISOString(), | ||
generator: options.generator || config_1.generator | ||
} | ||
}; | ||
if (options.author) { | ||
feed.push({ author: formatAuthor(options.author) }); | ||
base.feed.author = formatAuthor(options.author); | ||
} | ||
// link (rel="alternate") | ||
base.feed.link = []; | ||
if (options.link) { | ||
feed.push({ link: { _attr: { rel: "alternate", href: options.link } } }); | ||
base.feed.link.push({ _attributes: { rel: "alternate", href: options.link } }); | ||
} | ||
// link (rel="self") | ||
var atomLink = options.feed || (options.feedLinks && options.feedLinks.atom); | ||
if (atomLink) { | ||
feed.push({ link: { _attr: { rel: "self", href: atomLink } } }); | ||
base.feed.link.push({ _attributes: { rel: "self", href: atomLink } }); | ||
} | ||
// link (rel="hub") | ||
if (options.hub) { | ||
feed.push({ link: { _attr: { rel: "hub", href: options.hub } } }); | ||
base.feed.link.push({ _attributes: { rel: "hub", href: options.hub } }); | ||
} | ||
/************************************************************************** | ||
* "feed" node: optional elements | ||
*************************************************************************/ | ||
if (options.description) { | ||
feed.push({ subtitle: options.description }); | ||
base.feed.subtitle = options.description; | ||
} | ||
if (options.image) { | ||
feed.push({ logo: options.image }); | ||
base.feed.logo = options.image; | ||
} | ||
if (options.favicon) { | ||
feed.push({ icon: options.favicon }); | ||
base.feed.icon = options.favicon; | ||
} | ||
if (options.copyright) { | ||
feed.push({ rights: options.copyright }); | ||
base.feed.rights = options.copyright; | ||
} | ||
ins.categories.forEach(function (category) { | ||
feed.push({ category: [{ _attr: { term: category } }] }); | ||
base.feed.category = []; | ||
ins.categories.map(function (category) { | ||
base.feed.category.push({ _attributes: { term: category } }); | ||
}); | ||
ins.contributors.forEach(function (contributor) { return feed.push({ contributor: formatAuthor(contributor) }); }); | ||
// icon | ||
/************************************************************************** | ||
* "entry" nodes | ||
*************************************************************************/ | ||
ins.items.forEach(function (item) { | ||
// | ||
// entry: required elements | ||
// | ||
var entry = [ | ||
{ title: { _attr: { type: "html" }, _cdata: item.title } }, | ||
{ id: item.id || item.link }, | ||
{ link: [{ _attr: { href: item.link } }] }, | ||
{ updated: item.date.toISOString() } | ||
]; | ||
// | ||
// entry: recommended elements | ||
// | ||
base.feed.contributor = []; | ||
ins.contributors.map(function (contributor) { | ||
base.feed.contributor.push(formatAuthor(contributor)); | ||
}); | ||
base.feed.entry = []; | ||
ins.items.map(function (item) { | ||
var entry = { | ||
title: { _attributes: { type: "html" }, _cdata: item.title }, | ||
id: item.id || item.link, | ||
link: [{ _attributes: { href: item.link } }], | ||
updated: item.date.toISOString() | ||
}; | ||
if (item.description) { | ||
entry.push({ | ||
summary: { _attr: { type: "html" }, _cdata: item.description } | ||
}); | ||
entry.summary = { | ||
_attributes: { type: "html" }, | ||
_cdata: item.description | ||
}; | ||
} | ||
if (item.content) { | ||
entry.push({ | ||
content: { _attr: { type: "html" }, _cdata: item.content } | ||
}); | ||
entry.content = { | ||
_attributes: { type: "html" }, | ||
_cdata: item.content | ||
}; | ||
} | ||
// entry author(s) | ||
if (Array.isArray(item.author)) { | ||
item.author.forEach(function (author) { return entry.push({ author: formatAuthor(author) }); }); | ||
entry.author = []; | ||
item.author.map(function (author) { | ||
entry.author.push(formatAuthor(author)); | ||
}); | ||
} | ||
// content | ||
// link - relative link to article | ||
// | ||
// entry: optional elements | ||
// | ||
// category | ||
// contributor | ||
if (item.contributor && Array.isArray(item.contributor)) { | ||
item.contributor.forEach(function (contributor) { return entry.push({ contributor: formatAuthor(contributor) }); }); | ||
entry.contributor = []; | ||
item.contributor.map(function (contributor) { | ||
entry.contributor.push(formatAuthor(contributor)); | ||
}); | ||
} | ||
// published | ||
if (item.published) { | ||
entry.push({ published: item.published.toISOString() }); | ||
entry.published = item.published.toISOString(); | ||
} | ||
// source | ||
// rights | ||
if (item.copyright) { | ||
entry.push({ rights: item.copyright }); | ||
entry.rights = item.copyright; | ||
} | ||
feed.push({ entry: entry }); | ||
base.feed.entry.push(entry); | ||
}); | ||
return DOCTYPE + xml([{ feed: feed }], true); | ||
return convert.js2xml(base, { compact: true, ignoreComment: true, spaces: 4 }); | ||
}); | ||
var formatAuthor = function (author) { | ||
var name = author.name, email = author.email, link = author.link; | ||
var contributor = []; | ||
if (name) { | ||
contributor.push({ name: name }); | ||
} | ||
if (email) { | ||
contributor.push({ email: email }); | ||
} | ||
if (link) { | ||
contributor.push({ uri: link }); | ||
} | ||
return contributor; | ||
return { | ||
name: name, | ||
email: email, | ||
uri: link | ||
}; | ||
}; | ||
//# sourceMappingURL=atom1.js.map |
@@ -1,2 +0,2 @@ | ||
/// <reference path="types/index.d.ts" /> | ||
import { FeedOptions, Item, Author, Extension } from "./typings"; | ||
export declare class Feed { | ||
@@ -13,14 +13,5 @@ options: FeedOptions; | ||
addExtension: (extension: Extension) => number; | ||
/** | ||
* Returns a Atom 1.0 feed | ||
*/ | ||
atom1: () => string; | ||
/** | ||
* Returns a RSS 2.0 feed | ||
*/ | ||
rss2: () => string; | ||
/** | ||
* Returns a JSON1 feed | ||
*/ | ||
json1: () => string; | ||
} |
"use strict"; | ||
/// <reference path="types/index.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -7,3 +6,3 @@ var atom1_1 = require("./atom1"); | ||
var rss2_1 = require("./rss2"); | ||
var Feed = /** @class */ (function () { | ||
var Feed = (function () { | ||
function Feed(options) { | ||
@@ -19,13 +18,4 @@ var _this = this; | ||
this.addExtension = function (extension) { return _this.extensions.push(extension); }; | ||
/** | ||
* Returns a Atom 1.0 feed | ||
*/ | ||
this.atom1 = function () { return atom1_1.default(_this); }; | ||
/** | ||
* Returns a RSS 2.0 feed | ||
*/ | ||
this.rss2 = function () { return rss2_1.default(_this); }; | ||
/** | ||
* Returns a JSON1 feed | ||
*/ | ||
this.json1 = function () { return json_1.default(_this); }; | ||
@@ -32,0 +22,0 @@ this.options = options; |
@@ -1,3 +0,3 @@ | ||
/// <reference path="types/index.d.ts" /> | ||
import { Feed } from "./feed"; | ||
declare const _default: (ins: Feed) => string; | ||
export default _default; |
"use strict"; | ||
/// <reference path="types/index.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -31,3 +30,3 @@ exports.default = (function (ins) { | ||
} | ||
extensions.forEach(function (e) { | ||
extensions.map(function (e) { | ||
feed[e.name] = e.objects; | ||
@@ -38,4 +37,2 @@ }); | ||
id: item.id, | ||
// json_feed distinguishes between html and text content | ||
// but since we only take a single type, we'll assume HTML | ||
content_html: item.content | ||
@@ -64,3 +61,2 @@ }; | ||
if (author instanceof Array) { | ||
// json feed only supports 1 author per post | ||
author = author[0]; | ||
@@ -77,3 +73,3 @@ } | ||
if (item.extensions) { | ||
item.extensions.forEach(function (e) { | ||
item.extensions.map(function (e) { | ||
feedItem[e.name] = e.objects; | ||
@@ -80,0 +76,0 @@ }); |
@@ -1,3 +0,3 @@ | ||
/// <reference path="types/index.d.ts" /> | ||
import { Feed } from "./feed"; | ||
declare const _default: (ins: Feed) => string; | ||
export default _default; |
137
lib/rss2.js
"use strict"; | ||
/// <reference path="types/index.ts" /> | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var xml = require("xml"); | ||
var convert = require("xml-js"); | ||
var config_1 = require("./config"); | ||
var DOCTYPE = '<?xml version="1.0" encoding="utf-8"?>\n'; | ||
exports.default = (function (ins) { | ||
@@ -11,55 +9,41 @@ var options = ins.options; | ||
var isContent = false; | ||
var channel = [ | ||
{ title: options.title }, | ||
{ link: options.link }, | ||
{ description: options.description }, | ||
{ | ||
lastBuildDate: options.updated ? options.updated.toUTCString() : new Date().toUTCString() | ||
}, | ||
{ docs: "http://blogs.law.harvard.edu/tech/rss" }, | ||
{ generator: options.generator || config_1.generator } | ||
]; | ||
var rss = [{ _attr: { version: "2.0" } }, { channel: channel }]; | ||
/** | ||
* Channel language | ||
* https://validator.w3.org/feed/docs/rss2.html#ltimagegtSubelementOfLtchannelgt | ||
*/ | ||
var base = { | ||
_declaration: { _attributes: { version: "1.0", encoding: "utf-8" } }, | ||
rss: { | ||
_attributes: { version: "2.0" }, | ||
channel: { | ||
title: { _text: options.title }, | ||
link: { _text: options.link }, | ||
description: { _text: options.description }, | ||
lastBuildDate: { _text: options.updated ? options.updated.toUTCString() : new Date().toUTCString() }, | ||
docs: { _text: "http://blogs.law.harvard.edu/tech/rss" }, | ||
generator: { _text: options.generator || config_1.generator } | ||
} | ||
} | ||
}; | ||
if (options.language) { | ||
channel.push({ | ||
language: options.language | ||
}); | ||
base.rss.channel.language = { _text: options.language }; | ||
} | ||
/** | ||
* Channel Image | ||
* http://cyber.law.harvard.edu/rss/rss.html#ltimagegtSubelementOfLtchannelgt | ||
*/ | ||
if (options.image) { | ||
channel.push({ | ||
image: [{ title: options.title }, { url: options.image }, { link: options.link }] | ||
}); | ||
base.rss.channel.image = { | ||
title: { _text: options.title }, | ||
url: { _text: options.image }, | ||
link: { _text: options.link } | ||
}; | ||
} | ||
/** | ||
* Channel Copyright | ||
* http://cyber.law.harvard.edu/rss/rss.html#optionalChannelElements | ||
*/ | ||
if (options.copyright) { | ||
channel.push({ copyright: options.copyright }); | ||
base.rss.channel.copyright = { _text: options.copyright }; | ||
} | ||
/** | ||
* Channel Categories | ||
* http://cyber.law.harvard.edu/rss/rss.html#comments | ||
*/ | ||
ins.categories.forEach(function (category) { | ||
channel.push({ category: category }); | ||
ins.categories.map(function (category) { | ||
if (!base.rss.channel.category) { | ||
base.rss.channel.category = []; | ||
} | ||
base.rss.channel.category.push({ _text: category }); | ||
}); | ||
/** | ||
* Feed URL | ||
* http://validator.w3.org/feed/docs/warning/MissingAtomSelfLink.html | ||
*/ | ||
var atomLink = options.feed || (options.feedLinks && options.feedLinks.atom); | ||
if (atomLink) { | ||
isAtom = true; | ||
channel.push({ | ||
"atom:link": { | ||
_attr: { | ||
base.rss.channel["atom:link"] = [ | ||
{ | ||
_attributes: { | ||
href: atomLink, | ||
@@ -70,55 +54,46 @@ rel: "self", | ||
} | ||
}); | ||
]; | ||
} | ||
/** | ||
* Hub for PubSubHubbub | ||
* https://code.google.com/p/pubsubhubbub/ | ||
*/ | ||
if (options.hub) { | ||
isAtom = true; | ||
channel.push({ | ||
"atom:link": { | ||
_attr: { | ||
href: options.hub, | ||
rel: "hub" | ||
} | ||
if (!base.rss.channel["atom:link"]) { | ||
base.rss.channel["atom:link"] = []; | ||
} | ||
base.rss.channel["atom:link"] = { | ||
_attributes: { | ||
href: options.hub, | ||
rel: "hub" | ||
} | ||
}); | ||
}; | ||
} | ||
/** | ||
* Channel Categories | ||
* http://cyber.law.harvard.edu/rss/rss.html#hrelementsOfLtitemgt | ||
*/ | ||
ins.items.forEach(function (entry) { | ||
var item = []; | ||
base.rss.channel.item = []; | ||
ins.items.map(function (entry) { | ||
var item = {}; | ||
if (entry.title) { | ||
item.push({ title: { _cdata: entry.title } }); | ||
item.title = { _cdata: entry.title }; | ||
} | ||
if (entry.link) { | ||
item.push({ link: entry.link }); | ||
item.link = { _text: entry.link }; | ||
} | ||
if (entry.guid) { | ||
item.push({ guid: entry.guid }); | ||
item.guid = { _text: entry.guid }; | ||
} | ||
else if (entry.link) { | ||
item.push({ guid: entry.link }); | ||
item.guid = { _text: entry.link }; | ||
} | ||
if (entry.date) { | ||
item.push({ pubDate: entry.date.toUTCString() }); | ||
item.pubDate = { _text: entry.date.toUTCString() }; | ||
} | ||
if (entry.description) { | ||
item.push({ description: { _cdata: entry.description } }); | ||
item.description = { _cdata: entry.description }; | ||
} | ||
if (entry.content) { | ||
isContent = true; | ||
item.push({ "content:encoded": { _cdata: entry.content } }); | ||
item["content:encoded"] = { _cdata: entry.content }; | ||
} | ||
/** | ||
* Item Author | ||
* http://cyber.law.harvard.edu/rss/rss.html#ltauthorgtSubelementOfLtitemgt | ||
*/ | ||
if (Array.isArray(entry.author)) { | ||
item.author = []; | ||
entry.author.map(function (author) { | ||
if (author.email && author.name) { | ||
item.push({ author: author.email + " (" + author.name + ")" }); | ||
item.author.push({ _text: author.email + " (" + author.name + ")" }); | ||
} | ||
@@ -128,14 +103,14 @@ }); | ||
if (entry.image) { | ||
item.push({ enclosure: [{ _attr: { url: entry.image } }] }); | ||
item.enclosure = { _attributes: { url: entry.image } }; | ||
} | ||
channel.push({ item: item }); | ||
base.rss.channel.item.push(item); | ||
}); | ||
if (isContent) { | ||
rss[0]._attr["xmlns:content"] = "http://purl.org/rss/1.0/modules/content/"; | ||
base.rss._attributes["xmlns:content"] = "http://purl.org/rss/1.0/modules/content/"; | ||
} | ||
if (isAtom) { | ||
rss[0]._attr["xmlns:atom"] = "http://www.w3.org/2005/Atom"; | ||
base.rss._attributes["xmlns:atom"] = "http://www.w3.org/2005/Atom"; | ||
} | ||
return DOCTYPE + xml([{ rss: rss }], true); | ||
return convert.js2xml(base, { compact: true, ignoreComment: true, spaces: 4 }); | ||
}); | ||
//# sourceMappingURL=rss2.js.map |
{ | ||
"name": "feed", | ||
"version": "2.0.4", | ||
"version": "3.0.0", | ||
"description": "Feed is a RSS, Atom and JSON feed generator for Node.js, making content syndication simple and intuitive!", | ||
@@ -26,4 +26,3 @@ "homepage": "https://github.com/jpmonette", | ||
"dependencies": { | ||
"luxon": "^1.3.3", | ||
"xml": "^1.0.1" | ||
"xml-js": "^1.6.11" | ||
}, | ||
@@ -30,0 +29,0 @@ "devDependencies": { |
@@ -5,5 +5,5 @@ import { sampleFeed } from "./setup"; | ||
it("should generate a valid feed", () => { | ||
let actual = sampleFeed.atom1(); | ||
const actual = sampleFeed.atom1(); | ||
expect(actual).toMatchSnapshot(); | ||
}); | ||
}); |
@@ -5,5 +5,5 @@ import { sampleFeed } from "./setup"; | ||
it("should generate a valid feed", () => { | ||
let actual = sampleFeed.rss2(); | ||
const actual = sampleFeed.rss2(); | ||
expect(actual).toMatchSnapshot(); | ||
}); | ||
}); |
130
src/atom1.ts
@@ -1,32 +0,29 @@ | ||
/// <reference path="types/index.ts" /> | ||
import * as xml from "xml"; | ||
import { generator } from "./config"; | ||
import * as convert from "xml-js"; | ||
import { Feed } from "./feed"; | ||
import { Author, Item } from "./typings"; | ||
const DOCTYPE = '<?xml version="1.0" encoding="utf-8"?>\n'; | ||
export default (ins: Feed) => { | ||
const { options } = ins; | ||
let feed: any = []; | ||
const base: any = { | ||
_declaration: { _attributes: { version: "1.0", encoding: "utf-8" } }, | ||
feed: { | ||
_attributes: { xmlns: "http://www.w3.org/2005/Atom" }, | ||
id: options.id, | ||
title: options.title, | ||
updated: options.updated ? options.updated.toISOString() : new Date().toISOString(), | ||
generator: options.generator || generator | ||
} | ||
}; | ||
feed.push({ _attr: { xmlns: "http://www.w3.org/2005/Atom" } }); | ||
feed.push({ id: options.id }); | ||
feed.push({ title: options.title }); | ||
if (options.updated) { | ||
feed.push({ updated: options.updated.toISOString() }); | ||
} else { | ||
feed.push({ updated: new Date().toISOString() }); | ||
if (options.author) { | ||
base.feed.author = formatAuthor(options.author); | ||
} | ||
feed.push({ generator: options.generator || generator }); | ||
base.feed.link = []; | ||
if (options.author) { | ||
feed.push({ author: formatAuthor(options.author) }); | ||
} | ||
// link (rel="alternate") | ||
if (options.link) { | ||
feed.push({ link: { _attr: { rel: "alternate", href: options.link } } }); | ||
base.feed.link.push({ _attributes: { rel: "alternate", href: options.link } }); | ||
} | ||
@@ -38,3 +35,3 @@ | ||
if (atomLink) { | ||
feed.push({ link: { _attr: { rel: "self", href: atomLink } } }); | ||
base.feed.link.push({ _attributes: { rel: "self", href: atomLink } }); | ||
} | ||
@@ -44,3 +41,3 @@ | ||
if (options.hub) { | ||
feed.push({ link: { _attr: { rel: "hub", href: options.hub } } }); | ||
base.feed.link.push({ _attributes: { rel: "hub", href: options.hub } }); | ||
} | ||
@@ -53,29 +50,37 @@ | ||
if (options.description) { | ||
feed.push({ subtitle: options.description }); | ||
base.feed.subtitle = options.description; | ||
} | ||
if (options.image) { | ||
feed.push({ logo: options.image }); | ||
base.feed.logo = options.image; | ||
} | ||
if (options.favicon) { | ||
feed.push({ icon: options.favicon }); | ||
base.feed.icon = options.favicon; | ||
} | ||
if (options.copyright) { | ||
feed.push({ rights: options.copyright }); | ||
base.feed.rights = options.copyright; | ||
} | ||
ins.categories.forEach((category: string) => { | ||
feed.push({ category: [{ _attr: { term: category } }] }); | ||
base.feed.category = []; | ||
ins.categories.map((category: string) => { | ||
base.feed.category.push({ _attributes: { term: category } }); | ||
}); | ||
ins.contributors.forEach((contributor: Author) => feed.push({ contributor: formatAuthor(contributor) })); | ||
base.feed.contributor = []; | ||
ins.contributors.map((contributor: Author) => { | ||
base.feed.contributor.push(formatAuthor(contributor)); | ||
}); | ||
// icon | ||
base.feed.entry = []; | ||
/************************************************************************** | ||
* "entry" nodes | ||
*************************************************************************/ | ||
ins.items.forEach((item: Item) => { | ||
ins.items.map((item: Item) => { | ||
// | ||
@@ -85,8 +90,8 @@ // entry: required elements | ||
let entry: any = [ | ||
{ title: { _attr: { type: "html" }, _cdata: item.title } }, | ||
{ id: item.id || item.link }, | ||
{ link: [{ _attr: { href: item.link } }] }, | ||
{ updated: item.date.toISOString() } | ||
]; | ||
let entry: convert.ElementCompact = { | ||
title: { _attributes: { type: "html" }, _cdata: item.title }, | ||
id: item.id || item.link, | ||
link: [{ _attributes: { href: item.link } }], | ||
updated: item.date.toISOString() | ||
}; | ||
@@ -97,11 +102,13 @@ // | ||
if (item.description) { | ||
entry.push({ | ||
summary: { _attr: { type: "html" }, _cdata: item.description } | ||
}); | ||
entry.summary = { | ||
_attributes: { type: "html" }, | ||
_cdata: item.description | ||
}; | ||
} | ||
if (item.content) { | ||
entry.push({ | ||
content: { _attr: { type: "html" }, _cdata: item.content } | ||
}); | ||
entry.content = { | ||
_attributes: { type: "html" }, | ||
_cdata: item.content | ||
}; | ||
} | ||
@@ -111,3 +118,7 @@ | ||
if (Array.isArray(item.author)) { | ||
item.author.forEach((author: Author) => entry.push({ author: formatAuthor(author) })); | ||
entry.author = []; | ||
item.author.map((author: Author) => { | ||
entry.author.push(formatAuthor(author)); | ||
}); | ||
} | ||
@@ -127,3 +138,7 @@ | ||
if (item.contributor && Array.isArray(item.contributor)) { | ||
item.contributor.forEach((contributor: Author) => entry.push({ contributor: formatAuthor(contributor) })); | ||
entry.contributor = []; | ||
item.contributor.map((contributor: Author) => { | ||
entry.contributor.push(formatAuthor(contributor)); | ||
}); | ||
} | ||
@@ -133,3 +148,3 @@ | ||
if (item.published) { | ||
entry.push({ published: item.published.toISOString() }); | ||
entry.published = item.published.toISOString(); | ||
} | ||
@@ -141,9 +156,9 @@ | ||
if (item.copyright) { | ||
entry.push({ rights: item.copyright }); | ||
entry.rights = item.copyright; | ||
} | ||
feed.push({ entry }); | ||
base.feed.entry.push(entry); | ||
}); | ||
return DOCTYPE + xml([{ feed }], true); | ||
return convert.js2xml(base, { compact: true, ignoreComment: true, spaces: 4 }); | ||
}; | ||
@@ -153,17 +168,8 @@ | ||
const { name, email, link } = author; | ||
let contributor = []; | ||
if (name) { | ||
contributor.push({ name }); | ||
} | ||
if (email) { | ||
contributor.push({ email }); | ||
} | ||
if (link) { | ||
contributor.push({ uri: link }); | ||
} | ||
return contributor; | ||
return { | ||
name, | ||
email, | ||
uri: link | ||
}; | ||
}; |
@@ -1,6 +0,5 @@ | ||
/// <reference path="types/index.ts" /> | ||
import renderAtom from "./atom1"; | ||
import renderJSON from "./json"; | ||
import renderRSS from "./rss2"; | ||
import { FeedOptions, Item, Author, Extension } from "./typings"; | ||
@@ -7,0 +6,0 @@ export class Feed { |
@@ -1,5 +0,7 @@ | ||
/// <reference path="types/index.ts" /> | ||
import { Extension, Item, Author } from "./typings"; | ||
import { Feed } from "./feed"; | ||
export default (ins: Feed) => { | ||
const { options, items, extensions } = ins; | ||
let feed: any = { | ||
@@ -36,3 +38,3 @@ version: "https://jsonfeed.org/version/1", | ||
extensions.forEach((e: Extension) => { | ||
extensions.map((e: Extension) => { | ||
feed[e.name] = e.objects; | ||
@@ -85,3 +87,3 @@ }); | ||
if (item.extensions) { | ||
item.extensions.forEach((e: Extension) => { | ||
item.extensions.map((e: Extension) => { | ||
feedItem[e.name] = e.objects; | ||
@@ -88,0 +90,0 @@ }); |
114
src/rss2.ts
@@ -1,9 +0,6 @@ | ||
/// <reference path="types/index.ts" /> | ||
import * as xml from "xml"; | ||
import * as convert from "xml-js"; | ||
import { generator } from "./config"; | ||
import { Feed } from "./feed"; | ||
import { Item, Author } from "./typings"; | ||
const DOCTYPE = '<?xml version="1.0" encoding="utf-8"?>\n'; | ||
export default (ins: Feed) => { | ||
@@ -14,15 +11,17 @@ const { options } = ins; | ||
const channel: any = [ | ||
{ title: options.title }, | ||
{ link: options.link }, | ||
{ description: options.description }, | ||
{ | ||
lastBuildDate: options.updated ? options.updated.toUTCString() : new Date().toUTCString() | ||
}, | ||
{ docs: "http://blogs.law.harvard.edu/tech/rss" }, | ||
{ generator: options.generator || generator } | ||
]; | ||
const base: any = { | ||
_declaration: { _attributes: { version: "1.0", encoding: "utf-8" } }, | ||
rss: { | ||
_attributes: { version: "2.0" }, | ||
channel: { | ||
title: { _text: options.title }, | ||
link: { _text: options.link }, | ||
description: { _text: options.description }, | ||
lastBuildDate: { _text: options.updated ? options.updated.toUTCString() : new Date().toUTCString() }, | ||
docs: { _text: "http://blogs.law.harvard.edu/tech/rss" }, | ||
generator: { _text: options.generator || generator } | ||
} | ||
} | ||
}; | ||
const rss: any[] = [{ _attr: { version: "2.0" } }, { channel }]; | ||
/** | ||
@@ -33,5 +32,3 @@ * Channel language | ||
if (options.language) { | ||
channel.push({ | ||
language: options.language | ||
}); | ||
base.rss.channel.language = { _text: options.language }; | ||
} | ||
@@ -44,5 +41,7 @@ | ||
if (options.image) { | ||
channel.push({ | ||
image: [{ title: options.title }, { url: options.image }, { link: options.link }] | ||
}); | ||
base.rss.channel.image = { | ||
title: { _text: options.title }, | ||
url: { _text: options.image }, | ||
link: { _text: options.link } | ||
}; | ||
} | ||
@@ -55,3 +54,3 @@ | ||
if (options.copyright) { | ||
channel.push({ copyright: options.copyright }); | ||
base.rss.channel.copyright = { _text: options.copyright }; | ||
} | ||
@@ -63,4 +62,7 @@ | ||
*/ | ||
ins.categories.forEach(category => { | ||
channel.push({ category }); | ||
ins.categories.map(category => { | ||
if (!base.rss.channel.category) { | ||
base.rss.channel.category = []; | ||
} | ||
base.rss.channel.category.push({ _text: category }); | ||
}); | ||
@@ -75,6 +77,5 @@ | ||
isAtom = true; | ||
channel.push({ | ||
"atom:link": { | ||
_attr: { | ||
base.rss.channel["atom:link"] = [ | ||
{ | ||
_attributes: { | ||
href: atomLink, | ||
@@ -85,3 +86,3 @@ rel: "self", | ||
} | ||
}); | ||
]; | ||
} | ||
@@ -95,10 +96,11 @@ | ||
isAtom = true; | ||
channel.push({ | ||
"atom:link": { | ||
_attr: { | ||
href: options.hub, | ||
rel: "hub" | ||
} | ||
if (!base.rss.channel["atom:link"]) { | ||
base.rss.channel["atom:link"] = []; | ||
} | ||
base.rss.channel["atom:link"] = { | ||
_attributes: { | ||
href: options.hub, | ||
rel: "hub" | ||
} | ||
}); | ||
}; | ||
} | ||
@@ -110,25 +112,27 @@ | ||
*/ | ||
ins.items.forEach((entry: Item) => { | ||
let item: any[] = []; | ||
base.rss.channel.item = []; | ||
ins.items.map((entry: Item) => { | ||
let item: any = {}; | ||
if (entry.title) { | ||
item.push({ title: { _cdata: entry.title } }); | ||
item.title = { _cdata: entry.title }; | ||
} | ||
if (entry.link) { | ||
item.push({ link: entry.link }); | ||
item.link = { _text: entry.link }; | ||
} | ||
if (entry.guid) { | ||
item.push({ guid: entry.guid }); | ||
item.guid = { _text: entry.guid }; | ||
} else if (entry.link) { | ||
item.push({ guid: entry.link }); | ||
item.guid = { _text: entry.link }; | ||
} | ||
if (entry.date) { | ||
item.push({ pubDate: entry.date.toUTCString() }); | ||
item.pubDate = { _text: entry.date.toUTCString() }; | ||
} | ||
if (entry.description) { | ||
item.push({ description: { _cdata: entry.description } }); | ||
item.description = { _cdata: entry.description }; | ||
} | ||
@@ -138,3 +142,3 @@ | ||
isContent = true; | ||
item.push({ "content:encoded": { _cdata: entry.content } }); | ||
item["content:encoded"] = { _cdata: entry.content }; | ||
} | ||
@@ -146,5 +150,6 @@ /** | ||
if (Array.isArray(entry.author)) { | ||
item.author = []; | ||
entry.author.map((author: Author) => { | ||
if (author.email && author.name) { | ||
item.push({ author: author.email + " (" + author.name + ")" }); | ||
item.author.push({ _text: author.email + " (" + author.name + ")" }); | ||
} | ||
@@ -155,17 +160,16 @@ }); | ||
if (entry.image) { | ||
item.push({ enclosure: [{ _attr: { url: entry.image } }] }); | ||
item.enclosure = { _attributes: { url: entry.image } }; | ||
} | ||
channel.push({ item }); | ||
base.rss.channel.item.push(item); | ||
}); | ||
if (isContent) { | ||
rss[0]._attr["xmlns:content"] = "http://purl.org/rss/1.0/modules/content/"; | ||
base.rss._attributes["xmlns:content"] = "http://purl.org/rss/1.0/modules/content/"; | ||
} | ||
if (isAtom) { | ||
rss[0]._attr["xmlns:atom"] = "http://www.w3.org/2005/Atom"; | ||
base.rss._attributes["xmlns:atom"] = "http://www.w3.org/2005/Atom"; | ||
} | ||
return DOCTYPE + xml([{ rss }], true); | ||
return convert.js2xml(base, { compact: true, ignoreComment: true, spaces: 4 }); | ||
}; |
{ | ||
"compilerOptions": { | ||
"allowUnreachableCode": false, | ||
"allowUnusedLabels": false, | ||
"baseUrl": ".", | ||
"declaration": true, | ||
"module": "commonjs", | ||
"noFallthroughCasesInSwitch": true, | ||
"noImplicitAny": true, | ||
"noImplicitReturns": true, | ||
"outDir": "./lib", | ||
"declaration": true, | ||
"removeComments": true, | ||
"sourceMap": true, | ||
"strict": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"allowUnreachableCode": false, | ||
"allowUnusedLabels": false, | ||
"sourceMap": true, | ||
"baseUrl": ".", | ||
"target": "es5" | ||
@@ -14,0 +16,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1
288919
943
+ Addedxml-js@^1.6.11
+ Addedsax@1.3.0(transitive)
+ Addedxml-js@1.6.11(transitive)
- Removedluxon@^1.3.3
- Removedxml@^1.0.1
- Removedluxon@1.28.1(transitive)
- Removedxml@1.0.1(transitive)