react-rss
Version:
React component capable of reading and rendering any RSS feed
76 lines (75 loc) • 5.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseXml = void 0;
var parseToJson_1 = require("./parseToJson");
exports.parseXml = function (xml, enhancer, itemEnhancer) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
var parseJsonItem = function (item) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
var enclosure = ((_a = item.children) === null || _a === void 0 ? void 0 : _a.enclosure) || '';
var source = ((_b = item.children) === null || _b === void 0 ? void 0 : _b.source) || '';
var result = {
author: ((_d = (_c = item.children) === null || _c === void 0 ? void 0 : _c.author) === null || _d === void 0 ? void 0 : _d.text) || '',
category: ((_f = (_e = item.children) === null || _e === void 0 ? void 0 : _e.category) === null || _f === void 0 ? void 0 : _f.text) || '',
comments: ((_h = (_g = item.children) === null || _g === void 0 ? void 0 : _g.comments) === null || _h === void 0 ? void 0 : _h.text) || '',
description: ((_k = (_j = item.children) === null || _j === void 0 ? void 0 : _j.description) === null || _k === void 0 ? void 0 : _k.text) || '',
guid: ((_m = (_l = item.children) === null || _l === void 0 ? void 0 : _l.guid) === null || _m === void 0 ? void 0 : _m.text) || '',
link: ((_p = (_o = item.children) === null || _o === void 0 ? void 0 : _o.link) === null || _p === void 0 ? void 0 : _p.text) || '',
pubDate: ((_r = (_q = item.children) === null || _q === void 0 ? void 0 : _q.pubDate) === null || _r === void 0 ? void 0 : _r.text) || '',
title: ((_t = (_s = item.children) === null || _s === void 0 ? void 0 : _s.title) === null || _t === void 0 ? void 0 : _t.text) || '',
enclosure: enclosure ? {
length: ((_u = enclosure.attributes) === null || _u === void 0 ? void 0 : _u.length) || '',
type: ((_v = enclosure.attributes) === null || _v === void 0 ? void 0 : _v.type) || '',
url: ((_w = enclosure.attributes) === null || _w === void 0 ? void 0 : _w.url) || '',
} : undefined,
source: source ? {
source: source.text || '',
url: ((_x = source.attributes) === null || _x === void 0 ? void 0 : _x.url) || '',
} : undefined,
};
if (itemEnhancer) {
return itemEnhancer(item.children, result);
}
return result;
};
var doc = new DOMParser().parseFromString(xml, 'text/xml');
try {
var channel = doc.querySelector('rss').querySelector('channel');
var json = parseToJson_1.parseToJson(channel);
var rss = {
header: {
title: ((_a = json.title) === null || _a === void 0 ? void 0 : _a.text) || '',
category: {
category: ((_b = json.category) === null || _b === void 0 ? void 0 : _b.text) || '',
domain: ((_d = (_c = json.category) === null || _c === void 0 ? void 0 : _c.attributes) === null || _d === void 0 ? void 0 : _d.domain) || '',
},
copyright: ((_e = json.copyright) === null || _e === void 0 ? void 0 : _e.text) || '',
description: ((_f = json.description) === null || _f === void 0 ? void 0 : _f.text) || '',
docs: ((_g = json.docs) === null || _g === void 0 ? void 0 : _g.text) || '',
generator: ((_h = json.generator) === null || _h === void 0 ? void 0 : _h.text) || '',
language: ((_j = json.language) === null || _j === void 0 ? void 0 : _j.text) || '',
lastBuildDate: ((_k = json.lastBuildDate) === null || _k === void 0 ? void 0 : _k.text) || '',
link: ((_l = json.link) === null || _l === void 0 ? void 0 : _l.text) || '',
managingEditor: ((_m = json.managingEditor) === null || _m === void 0 ? void 0 : _m.text) || '',
ttl: ((_o = json.ttl) === null || _o === void 0 ? void 0 : _o.text) || '',
webMaster: ((_p = json.webMaster) === null || _p === void 0 ? void 0 : _p.text) || '',
image: json.image ? {
height: ((_q = json.image.children) === null || _q === void 0 ? void 0 : _q.height) || '',
link: ((_r = json.image.children) === null || _r === void 0 ? void 0 : _r.link) || '',
title: ((_s = json.image.children) === null || _s === void 0 ? void 0 : _s.title) || '',
url: ((_t = json.image.children) === null || _t === void 0 ? void 0 : _t.url) || '',
width: ((_u = json.image.children) === null || _u === void 0 ? void 0 : _u.width) || '',
} : undefined,
},
items: Array.isArray(json.item) ? json.item.map(parseJsonItem) : [parseJsonItem(json.item)],
};
if (enhancer) {
return { header: enhancer(json, rss.header), items: rss.items };
}
return rss;
}
catch (e) {
console.error(e.message);
throw 'Failed to parse RSS! Most likely a malformed field.';
}
};