diff --git a/lib/rss.js b/lib/rss.js index 45667060..654e4ba8 100755 --- a/lib/rss.js +++ b/lib/rss.js @@ -27,6 +27,7 @@ function RSS (options, items) { this.ttl = options.ttl; //option to return feed as GeoRSS is set automatically if feed.lat/long is used this.geoRSS = options.geoRSS || false; + this.cdata = options.cdata || true; //will wrap content to CDATA this.items = items || []; this.item = function (options) { @@ -61,11 +62,20 @@ function ifTruePush(bool, array, data) { } } +function getValue (bool, argument) { + if(bool){ + return {_cdata: argument}; + }else{ + return argument; + } +} + function generateXML (data){ + var cdata = data.cdata; var channel = []; - channel.push({ title: { _cdata: data.title } }); - channel.push({ description: { _cdata: data.description || data.title } }); + channel.push({ title: getValue(cdata, data.title) }); + channel.push({ description: getValue(cdata, data.description || data.title) }); channel.push({ link: data.site_url || 'http://github.com/dylang/node-rss' }); // image_url set? if (data.image_url) { @@ -74,36 +84,36 @@ function generateXML (data){ channel.push({ generator: data.generator }); channel.push({ lastBuildDate: 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 }); - ifTruePush(data.hub, channel, { 'atom:link': { _attr: { href: data.hub, rel: 'hub' } } }); + ifTruePush(data.feed_url, channel, { 'atom:link': { _attr: { href: data.feed_url, rel: 'self', type: 'application/rss+xml' } } }); + ifTruePush(data.author, channel, { 'author': getValue(cdata, data.author) }); + ifTruePush(data.pubDate, channel, { 'pubDate': new Date(data.pubDate).toGMTString() }); + ifTruePush(data.copyright, channel, { 'copyright': getValue(cdata, data.copyright) }); + ifTruePush(data.language, channel, { 'language': getValue(cdata, data.language) }); + ifTruePush(data.managingEditor, channel, { 'managingEditor': getValue(cdata, data.managingEditor) }); + ifTruePush(data.webMaster, channel, { 'webMaster': getValue(cdata, data.webMaster) }); + ifTruePush(data.docs, channel, { 'docs': data.docs }); + ifTruePush(data.ttl, channel, { 'ttl': data.ttl }); + ifTruePush(data.hub, channel, { 'atom:link': { _attr: { href: data.hub, rel: 'hub' } } }); if (data.categories) { data.categories.forEach(function(category) { - ifTruePush(category, channel, { category: { _cdata: category } }); + ifTruePush(category, channel, { category: getValue(cdata, category) }); }); } data.items.forEach(function(item) { var item_values = [ - { title: { _cdata: item.title } } + { title: getValue(cdata, item.title) } ]; - ifTruePush(item.description, item_values, { description: { _cdata: item.description } }); + ifTruePush(item.description, item_values, { description: getValue(cdata, item.description) }); 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 ] }); item.categories.forEach(function(category) { - ifTruePush(category, item_values, { category: { _cdata: category } }); + ifTruePush(category, item_values, { category: getValue(cdata, category) }); }); - ifTruePush(item.author || data.author, item_values, { 'dc:creator': { _cdata: item.author || data.author } }); + ifTruePush(item.author || data.author, item_values, { 'dc:creator': getValue(cdata, item.author || data.author) }); ifTruePush(item.date, item_values, { pubDate: new Date(item.date).toGMTString() }); //Set GeoRSS to true if lat and long are set diff --git a/readme.md b/readme.md index de49ca60..c6e8d2c8 100644 --- a/readme.md +++ b/readme.md @@ -36,6 +36,7 @@ var feed = new RSS(feedOptions); * `pubDate` _optional_ **Date object or date string** The publication date for content in the feed * `ttl` _optional_ **integer** Number of minutes feed can be cached before refreshing from source. * `hub` _optional_ **PubSubHubbub hub url** Where is the PubSubHubb hub located. + * `cdata` _optional_ **boolean** Wrap content to CDATA if **true**. Default value is **true**. ### Add items to a feed