-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathindex.js
39 lines (30 loc) · 951 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const DOMParser = require('xmldom').DOMParser;
const rssV2Parser = require('./parsers/rssv2');
const atomV1Parser = require('./parsers/atomv1');
const getParser = (document) => {
const isRssSpecification =
document.getElementsByTagName('channel')[0] !== undefined;
const isAtomSpecification =
document.getElementsByTagName('feed')[0] !== undefined;
if (isRssSpecification) {
return rssV2Parser;
}
if (isAtomSpecification) {
return atomV1Parser;
}
return null;
};
exports.parse = (feed) =>
new Promise((resolve, reject) => {
const document = new DOMParser({
errorHandler: (_level, msg) => {
reject(msg);
},
}).parseFromString(feed, 'text/xml');
const parser = getParser(document);
if (!parser) {
reject('Unable to find any RSS element in feed');
}
const parsedFeed = parser.parse(document);
resolve(parsedFeed);
});