Skip to content

Commit a7e3de6

Browse files
committed
v2.9.0
1 parent 54bcbf7 commit a7e3de6

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

dist/rss-parser.js

+30-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! rss-parser 2.8.0 */
1+
/*! rss-parser 2.9.0 */
22

33
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.RSSParser = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
44
var Entities = require("entities");
@@ -11,7 +11,7 @@ var HTTPS = require('https');
1111

1212
var Parser = module.exports = {};
1313

14-
var TOP_FIELDS = [
14+
var FEED_FIELDS = [
1515
'title',
1616
'description',
1717
'author',
@@ -105,17 +105,23 @@ var parseRSS1 = function(xmlObj, callback) {
105105
callback("RSS 1.0 parsing not yet implemented.")
106106
}
107107

108-
var parseRSS2 = function(xmlObj, callback) {
108+
var parseRSS2 = function(xmlObj, options, callback) {
109+
110+
options.customFields = options.customFields || {};
111+
var itemFields = ITEM_FIELDS.concat(options.customFields.item || []);
112+
var feedFields = FEED_FIELDS.concat(options.customFields.feed || []);
113+
109114
var json = {feed: {entries: []}};
110115
var channel = xmlObj.rss.channel[0];
111116
if (channel['atom:link']) json.feed.feedUrl = channel['atom:link'][0].$.href;
112-
TOP_FIELDS.forEach(function(f) {
117+
118+
feedFields.forEach(function(f) {
113119
if (channel[f]) json.feed[f] = channel[f][0];
114120
})
115121
var items = channel.item;
116122
(items || []).forEach(function(item) {
117123
var entry = {};
118-
ITEM_FIELDS.forEach(function(f) {
124+
itemFields.forEach(function(f) {
119125
if (item[f]) entry[f] = item[f][0];
120126
})
121127
if (item.enclosure) {
@@ -189,26 +195,31 @@ var decorateItunes = function decorateItunes(json, channel) {
189195
});
190196
}
191197

192-
Parser.parseString = function(xml, callback) {
198+
Parser.parseString = function(xml, settings, callback) {
199+
if (!callback) {
200+
callback = settings;
201+
settings = {};
202+
}
203+
193204
XML2JS.parseString(xml, function(err, result) {
194205
if (err) return callback(err);
195206
if (result.feed) {
196207
return parseAtomFeed(result, callback)
197208
} else if (result.rss && result.rss.$.version && result.rss.$.version.indexOf('2') === 0) {
198-
return parseRSS2(result, callback);
209+
return parseRSS2(result, settings, callback);
199210
} else {
200211
return parseRSS1(result, callback);
201212
}
202213
});
203214
}
204215

205-
Parser.parseURL = function(feedUrl, settings, callback) {
216+
Parser.parseURL = function(feedUrl, options, callback) {
206217
if (!callback) {
207-
callback = settings;
208-
settings = {};
218+
callback = options;
219+
options = {};
209220
}
210-
settings.__redirectCount = settings.__redirectCount || 0;
211-
if (settings.maxRedirects === undefined) settings.maxRedirects = 1;
221+
options.__redirectCount = options.__redirectCount || 0;
222+
if (options.maxRedirects === undefined) options.maxRedirects = 1;
212223

213224
var xml = '';
214225
var get = feedUrl.indexOf('https') === 0 ? HTTPS.get : HTTP.get;
@@ -221,25 +232,25 @@ Parser.parseURL = function(feedUrl, settings, callback) {
221232
headers: {'User-Agent': 'rss-parser'}
222233
}, function(res) {
223234
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers['location']) {
224-
if (settings.maxRedirects === 0) return callback(new Error("Status code " + res.statusCode));
225-
if (settings.__redirectCount === settings.maxRedirects) return callback(new Error("Too many redirects"));
226-
settings.__redirectCount++;
227-
return Parser.parseURL(res.headers['location'], settings, callback);
235+
if (options.maxRedirects === 0) return callback(new Error("Status code " + res.statusCode));
236+
if (options.__redirectCount === options.maxRedirects) return callback(new Error("Too many redirects"));
237+
options.__redirectCount++;
238+
return Parser.parseURL(res.headers['location'], options, callback);
228239
}
229240
res.setEncoding('utf8');
230241
res.on('data', function(chunk) {
231242
xml += chunk;
232243
});
233244
res.on('end', function() {
234-
return Parser.parseString(xml, callback);
245+
return Parser.parseString(xml, options, callback);
235246
})
236247
})
237248
req.on('error', callback);
238249
}
239250

240-
Parser.parseFile = function(file, callback) {
251+
Parser.parseFile = function(file,options,callback) {
241252
FS.readFile(file, 'utf8', function(err, contents) {
242-
return Parser.parseString(contents, callback);
253+
return Parser.parseString(contents, options, callback);
243254
})
244255
}
245256

0 commit comments

Comments
 (0)