Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dateFormatter to RSS constructor options #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 9 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ function getSize(filename) {
return fs.statSync(filename).size;
}

function generateXML (data){
function defaultDateFormatter(date) {
return new Date(date).toUTCString()
}

function generateXML (data) {
const dateFormatter = data.dateFormatter

var channel = [];
channel.push({ title: { _cdata: data.title } });
Expand All @@ -43,7 +48,7 @@ function generateXML (data){

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': dateFormatter(data.pubDate) });
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 +78,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: dateFormatter(item.date) });

//Set GeoRSS to true if lat and long are set
data.geoRSS = data.geoRSS || (item.lat && item.long);
Expand Down Expand Up @@ -159,6 +164,7 @@ function RSS (options, items) {
this.custom_namespaces = options.custom_namespaces || {};
this.custom_elements = options.custom_elements || [];
this.items = items || [];
this.dateFormatter = options.dateFormatter || defaultDateFormatter

this.item = function (options) {
options = options || {};
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var feed = new RSS(feedOptions);
* `hub` _optional_ **PubSubHubbub hub url** Where is the PubSubHub hub located.
* `custom_namespaces` _optional_ **object** Put additional namespaces in <rss> element (without 'xmlns:' prefix)
* `custom_elements` _optional_ **array** Put additional elements in the feed (node-xml syntax)
* `dateFormatter` _optional_ **function** A function that receives the feed `pubDate` field and each item's `date` field. Allows for using a custom date format. Defaults to `new Date(date).toUTCString()`.

#### Add items to a feed

Expand Down