Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var mime = require('mime-types');
var xml = require('xml');
var fs = require('fs');

var moment = require('moment-timezone');

function ifTruePush(bool, array, data) {
if (bool) {
Expand All @@ -28,7 +28,21 @@ function getSize(filename) {
return fs.statSync(filename).size;
}

function generateXML (data){
function formatDate(dateStr, format, timeZone) {
var date = new Date(dateStr);
if (format && timeZone) {
return moment(date).tz(timeZone).format(format);
}
else if (format) {
return moment(date).utc().format(format);
}
else {
return new Date(date).toUTCString();
}
}

function generateXML (data, options){
options = options || {};

var channel = [];
channel.push({ title: { _cdata: data.title } });
Expand All @@ -39,11 +53,11 @@ function generateXML (data){
channel.push({ image: [ {url: data.image_url}, {title: data.title}, {link: data.site_url} ] });
}
channel.push({ generator: data.generator });
channel.push({ lastBuildDate: new Date().toUTCString() });
channel.push({ lastBuildDate: formatDate(new Date().toUTCString(), options.dateFormat, options.timeZone) });

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.pubDate, channel, { 'pubDate': formatDate(data.pubDate, options.dateFormat, options.timeZone) });
ifTruePush(data.copyright, channel, { 'copyright': { _cdata: data.copyright } });
ifTruePush(data.language, channel, { 'language': { _cdata: data.language } });
ifTruePush(data.managingEditor, channel, { 'managingEditor': { _cdata: data.managingEditor } });
Expand Down Expand Up @@ -73,7 +87,7 @@ function generateXML (data){
});

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() });
ifTruePush(item.date, item_values, { pubDate: formatDate(item.date, options.dateFormat, options.timeZone) });

//Set GeoRSS to true if lat and long are set
data.geoRSS = data.geoRSS || (item.lat && item.long);
Expand Down Expand Up @@ -180,9 +194,10 @@ function RSS (options, items) {
return this;
};

this.xml = function(indent) {
this.xml = function(options) {
options = options || {};
return '<?xml version="1.0" encoding="UTF-8"?>' +
xml(generateXML(this), indent);
xml(generateXML(this, options), {indent: options.indent});
};
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "rss",
"name": "jpackard-rss",
"version": "1.2.1",
"description": "RSS feed generator. Add RSS feeds to any project. Supports enclosures and GeoRSS.",
"keywords": [
Expand Down Expand Up @@ -57,6 +57,7 @@
},
"dependencies": {
"mime-types": "^2.1.7",
"moment-timezone": "^0.5.5",
"xml": "^1.0.0"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions test/expectedOutput/timeZoneAndFormat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title><![CDATA[title]]></title>
<description><![CDATA[description]]></description>
<link>http://example.com</link>
<generator>RSS for Node</generator>
<lastBuildDate>Thu, 11 Dec 2014 04:04:57 +0900</lastBuildDate>
<atom:link href="http://example.com/rss.xml" rel="self" type="application/rss+xml"/>
</channel>
</rss>
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ test('PubSubHubbub hub', function(t) {
t.equal(feed.xml({indent: true}), expectedOutput.pubSubHubbub);
});

test('Timezone and format override', function(t) {
t.plan(1);

var feed = new RSS({
title: 'title',
description: 'description',
feed_url: 'http://example.com/rss.xml',
site_url: 'http://example.com'
});

t.equal(feed.xml({indent: true, timeZone: 'Asia/Tokyo', dateFormat: 'ddd, DD MMM YYYY HH:mm:ss ZZ'}), expectedOutput.timeZoneAndFormat);
});

test('custom elements', function(t) {
t.plan(1);
Expand Down