From a0745297a6583ab1acc57279d047cecb252b6b7c Mon Sep 17 00:00:00 2001 From: Danny Graham Date: Thu, 27 Jun 2013 14:26:45 -0700 Subject: [PATCH 1/3] adds categories to feed output; fixes #12 --- lib/rss.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/rss.js b/lib/rss.js index b2a99d9..3732952 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -70,6 +70,10 @@ function generateXML (data){ ifTruePush(item.url, item_values, { link: item.url }); ifTruePush(item.link || item.guid || item.title, item_values, { guid: [ { _attr: { isPermaLink: !item.guid && !!item.url } }, item.guid || item.url || item.title ] }); + for (var i in item.categories) { + ifTruePush(item.categories[i], item_values, { category: { _cdata: item.categories[i] } }); + } + ifTruePush(item.author || data.author, item_values, { 'dc:creator': { _cdata: item.author || data.author } }); ifTruePush(item.date, item_values, { pubDate: new Date(item.date).toGMTString() }); channel.push({ item: item_values }); From 91121e8c32aa18028477b14c34d1ea8535185e11 Mon Sep 17 00:00:00 2001 From: Danny Graham Date: Wed, 3 Jul 2013 08:38:47 -0700 Subject: [PATCH 2/3] updates tests and documentation with category support --- lib/rss.js | 6 +++--- readme.md | 6 ++++-- test/test.js | 20 ++++++++++++++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/rss.js b/lib/rss.js index 3732952..7e132bc 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -70,9 +70,9 @@ function generateXML (data){ ifTruePush(item.url, item_values, { link: item.url }); ifTruePush(item.link || item.guid || item.title, item_values, { guid: [ { _attr: { isPermaLink: !item.guid && !!item.url } }, item.guid || item.url || item.title ] }); - for (var i in item.categories) { - ifTruePush(item.categories[i], item_values, { category: { _cdata: item.categories[i] } }); - } + item.categories.forEach(function(category) { + ifTruePush(category, item_values, { category: { _cdata: category } }); + }); ifTruePush(item.author || data.author, item_values, { 'dc:creator': { _cdata: item.author || data.author } }); ifTruePush(item.date, item_values, { pubDate: new Date(item.date).toGMTString() }); diff --git a/readme.md b/readme.md index 575450e..e7c158c 100644 --- a/readme.md +++ b/readme.md @@ -33,6 +33,7 @@ description: 'use this for the content. It can include html.', url: 'http://example.com/article4?this&that', // link to the item guid: '1123', // optional - defaults to url + categories: ['Category 1','Category 2','Category 3','Category 4'], // optional - array of item categories author: 'Guest Author', // optional - defaults to feed author property date: 'May 27, 2012' // any format that js Date can parse. }); @@ -61,11 +62,12 @@ an have any number of items. Ten to tenty is usually good. * _guid_ A unique string feed readers use to know if an item is new or has already been seen. If you use a guid never change it. If you don't provide a guid then your item urls must be unique. + * _categories_ Optional. If provided, each array item will be added as a category element * _author_ Optional. If included it is the name of the item's creator. If not provided the item author will be the same as the feed author. This is typical except on multi-author blogs. - * _date_ The date and time of when the intem was created. Feed - readers use this to determin the sort order. Some readers will also use it to determin + * _date_ The date and time of when the item was created. Feed + readers use this to determine the sort order. Some readers will also use it to determine if the content should be presented as unread. ### Methods diff --git a/test/test.js b/test/test.js index 597c5c4..5faa7fd 100644 --- a/test/test.js +++ b/test/test.js @@ -50,9 +50,17 @@ module.exports = { url: 'http://example.com/article4?this&that', author: 'Guest Author', date: 'May 27, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 5 & test for categories', + description: 'description 5', + url: 'http://example.com/article5', + categories: ['Category 1','Category 2','Category 3','Category 4'], + author: 'Guest Author', + date: 'May 28, 2012 04:00:00 GMT' }); - var expectedResult = '\n<![CDATA[title]]>http://example.comhttp://example.com/icon.pngtitlehttp://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT'; + var expectedResult = '\n<![CDATA[title]]>http://example.comhttp://example.com/icon.pngtitlehttp://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; var result = feed.xml(); test.equal(result.length, expectedResult.length); @@ -94,9 +102,17 @@ module.exports = { url: 'http://example.com/article4?this&that', author: 'Guest Author', date: 'May 27, 2012 04:00:00 GMT' + }) + .item({ + title: 'item 5 & test for categories', + description: 'description 5', + url: 'http://example.com/article5', + categories: ['Category 1','Category 2','Category 3','Category 4'], + author: 'Guest Author', + date: 'May 28, 2012 04:00:00 GMT' }); - var expectedResult = '\n<![CDATA[title]]>http://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT'; + var expectedResult = '\n<![CDATA[title]]>http://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; var result = feed.xml(); test.equal(result.length, expectedResult.length); From 0eb51074d46656d3f52aad09cc1551ae4b82e966 Mon Sep 17 00:00:00 2001 From: Danny Graham Date: Wed, 3 Jul 2013 09:48:00 -0700 Subject: [PATCH 3/3] adds support for feed options: category, language, copyright, managingEditor, webMaster, pubDate, docs, ttl --- lib/rss.js | 25 +++++++++++++++++++++++-- readme.md | 18 +++++++++++++++++- test/test.js | 24 ++++++++++++++++++++---- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/lib/rss.js b/lib/rss.js index 7e132bc..5981cce 100644 --- a/lib/rss.js +++ b/lib/rss.js @@ -13,6 +13,14 @@ function RSS (options, items) { this.site_url = options.site_url; this.image_url = options.image_url; this.author = options.author; + this.categories = options.categories; + this.pubDate = options.pubDate; + this.docs = options.docs; + this.copyright = options.copyright; + this.language = options.language; + this.managingEditor = options.managingEditor; + this.webMaster = options.webMaster; + this.ttl = options.ttl; this.items = items || []; this.item = function (options) { @@ -58,9 +66,22 @@ function generateXML (data){ channel.push({ generator: 'NodeJS RSS Module' }); channel.push({ lastBuildDate: new Date().toGMTString() }); - ifTruePush(data.feed_url, channel, { 'atom:link': { _attr: { href: data.feed_url, rel: 'self', type: 'application/rss+xml' } } }); - // { updated: new Date().toGMTString() } + ifTruePush(data.feed_url, channel, { 'atom:link': { _attr: { href: data.feed_url, rel: 'self', type: 'application/rss+xml' } } }); + ifTruePush(data.author, channel, { 'author': { _cdata: data.author } }); + ifTruePush(data.pubDate, channel, { 'pubDate': new Date(data.pubDate).toGMTString() }); + ifTruePush(data.copyright, channel, { 'copyright': { _cdata: data.copyright } }); + ifTruePush(data.language, channel, { 'language': { _cdata: data.language } }); + ifTruePush(data.managingEditor, channel, { 'managingEditor': { _cdata: data.managingEditor } }); + ifTruePush(data.webMaster, channel, { 'webMaster': { _cdata: data.webMaster } }); + ifTruePush(data.docs, channel, { 'docs': data.docs }); + ifTruePush(data.ttl, channel, { 'ttl': data.ttl }); + if (data.categories) { + data.categories.forEach(function(category) { + ifTruePush(category, channel, { category: { _cdata: category } }); + }); + } + // { updated: new Date().toGMTString() } data.items.forEach(function(item) { var item_values = [ diff --git a/readme.md b/readme.md index e7c158c..c666faf 100644 --- a/readme.md +++ b/readme.md @@ -24,7 +24,15 @@ feed_url: 'http://example.com/rss.xml', site_url: 'http://example.com', image_url: 'http://example.com/icon.png', - author: 'Dylan Greene' + docs: 'http://example.com/rss/docs.html', + author: 'Dylan Greene', + managingEditor: 'Dylan Green', + webMaster: 'Dylan Green', + copyright: '2013 Dylan Green', + language: 'en', + categories: ['Category 1','Category 2','Category 3'], + pubDate: 'May 20, 2012 04:00:00 GMT', + ttl: '60' }); /* loop over data and add to feed */ @@ -48,7 +56,15 @@ * _feed_url_ Url to the rss feed. * _site_url_ Url to the site that the feed is for. * _image_url_ Optional. Small image for feed readers to use. + * _docs_ Optional. Url to documentation on this feed. * _author_ Who owns this feed. + * _managingEditor_ Optional. Who manages content in this feed. + * _webMaster_ Optional. Who manages feed availability and technical support. + * _copyright_ Optional. Copyright information for this feed. + * _language_ Optional. The language of the content of this feed. + * _categories_ Optional. One or more categories this feed belongs to. + * _pubDate_ Optional. The publication date for content in the feed + * _ttl_ Optional. Number of minutes feed can be cached before refreshing from source. ### Item Options diff --git a/test/test.js b/test/test.js index 5faa7fd..598d710 100644 --- a/test/test.js +++ b/test/test.js @@ -22,7 +22,15 @@ module.exports = { feed_url: 'http://example.com/rss.xml', site_url: 'http://example.com', image_url: 'http://example.com/icon.png', - author: 'Dylan Greene' + author: 'Dylan Greene', + categories: ['Category 1','Category 2','Category 3'], + pubDate: 'May 20, 2012 04:00:00 GMT', + docs: 'http://example.com/rss/docs.html', + copyright: '2013 Dylan Green', + language: 'en', + managingEditor: 'Dylan Green', + webMaster: 'Dylan Green', + ttl: '60' }); feed.item({ @@ -60,7 +68,7 @@ module.exports = { date: 'May 28, 2012 04:00:00 GMT' }); - var expectedResult = '\n<![CDATA[title]]>http://example.comhttp://example.com/icon.pngtitlehttp://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; + var expectedResult = '\n<![CDATA[title]]>http://example.comhttp://example.com/icon.pngtitlehttp://example.comNodeJS RSS Module' + new Date().toUTCString() +'Sun, 20 May 2012 04:00:00 GMThttp://example.com/rss/docs.html60<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; var result = feed.xml(); test.equal(result.length, expectedResult.length); @@ -74,7 +82,15 @@ module.exports = { description: 'description', feed_url: 'http://example.com/rss.xml', site_url: 'http://example.com', - author: 'Dylan Greene' + author: 'Dylan Greene', + categories: ['Category 1','Category 2','Category 3'], + pubDate: 'May 20, 2012 04:00:00 GMT', + docs: 'http://example.com/rss/docs.html', + copyright: '2013 Dylan Green', + language: 'en', + managingEditor: 'Dylan Green', + webMaster: 'Dylan Green', + ttl: '60' }); feed.item({ @@ -112,7 +128,7 @@ module.exports = { date: 'May 28, 2012 04:00:00 GMT' }); - var expectedResult = '\n<![CDATA[title]]>http://example.comNodeJS RSS Module' + new Date().toUTCString() +'<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; + var expectedResult = '\n<![CDATA[title]]>http://example.comNodeJS RSS Module' + new Date().toUTCString() +'Sun, 20 May 2012 04:00:00 GMThttp://example.com/rss/docs.html60<![CDATA[item 1]]>http://example.com/article1http://example.com/article1Thu, 24 May 2012 04:00:00 GMT<![CDATA[item 2]]>http://example.com/article2http://example.com/article2Fri, 25 May 2012 04:00:00 GMT<![CDATA[item 3]]>http://example.com/article3item3Sat, 26 May 2012 04:00:00 GMT<![CDATA[item 4 & html test with <strong>]]>html]]>http://example.com/article4?this&thathttp://example.com/article4?this&thatSun, 27 May 2012 04:00:00 GMT<![CDATA[item 5 & test for categories]]>http://example.com/article5http://example.com/article5Mon, 28 May 2012 04:00:00 GMT'; var result = feed.xml(); test.equal(result.length, expectedResult.length);