react-native-rss-parser
Advanced tools
Comparing version 1.4.0 to 1.5.0
53
index.js
@@ -1,14 +0,31 @@ | ||
var DOMParser = require('xmldom').DOMParser; | ||
var rssV2Parser = require('./parsers/rssv2'); | ||
var atomV1Parser = require('./parsers/atomv1'); | ||
const DOMParser = require('xmldom').DOMParser; | ||
const rssV2Parser = require('./parsers/rssv2'); | ||
const atomV1Parser = require('./parsers/atomv1'); | ||
exports.parse = function(feed) { | ||
return new Promise((resolve, reject) => { | ||
var document = new DOMParser({ | ||
errorHandler: function(level, msg) { | ||
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'); | ||
let parser = getParser(document); | ||
const parser = getParser(document); | ||
@@ -19,21 +36,5 @@ if (!parser) { | ||
let parsedFeed = parser.parse(document); | ||
const parsedFeed = parser.parse(document); | ||
resolve(parsedFeed); | ||
}); | ||
}; | ||
function getParser(document) { | ||
let isRssSpecification = document.getElementsByTagName('channel')[0] !== undefined; | ||
let isAtomSpecification = document.getElementsByTagName('feed')[0] !== undefined; | ||
if (isRssSpecification) { | ||
return rssV2Parser; | ||
} | ||
if (isAtomSpecification) { | ||
return atomV1Parser; | ||
} | ||
return; | ||
} |
144
model/rss.js
@@ -1,21 +0,25 @@ | ||
const model = module.exports = {}; | ||
model.rss = { | ||
const rss = { | ||
type: undefined, | ||
title: undefined, | ||
links: [{ | ||
url: undefined, | ||
rel: undefined | ||
}], | ||
links: [ | ||
{ | ||
url: undefined, | ||
rel: undefined, | ||
}, | ||
], | ||
description: undefined, | ||
language: undefined, | ||
copyright: undefined, | ||
authors: [{ | ||
name: undefined | ||
}], | ||
authors: [ | ||
{ | ||
name: undefined, | ||
}, | ||
], | ||
lastUpdated: undefined, | ||
lastPublished: undefined, | ||
categories: [{ | ||
name: undefined | ||
}], | ||
categories: [ | ||
{ | ||
name: undefined, | ||
}, | ||
], | ||
image: { | ||
@@ -26,15 +30,21 @@ url: undefined, | ||
width: undefined, | ||
height: undefined | ||
height: undefined, | ||
}, | ||
itunes: { | ||
author: [{ | ||
name: undefined | ||
}], | ||
author: [ | ||
{ | ||
name: undefined, | ||
}, | ||
], | ||
block: undefined, | ||
categories: [{ | ||
name: undefined, | ||
subCategories:[{ | ||
name: undefined | ||
}] | ||
}], | ||
categories: [ | ||
{ | ||
name: undefined, | ||
subCategories: [ | ||
{ | ||
name: undefined, | ||
}, | ||
], | ||
}, | ||
], | ||
image: undefined, | ||
@@ -51,38 +61,54 @@ explicit: undefined, | ||
}, | ||
items: [{ | ||
title: undefined, | ||
links: [{ | ||
url: undefined, | ||
rel: undefined | ||
}], | ||
id: undefined, | ||
imageUrl: undefined, | ||
description: undefined, | ||
content: undefined, | ||
categories: [{ | ||
name: undefined | ||
}], | ||
authors: [{ | ||
name: undefined | ||
}], | ||
published: undefined, | ||
enclosures: [{ | ||
url: undefined, | ||
length: undefined, | ||
mimeType: undefined | ||
}], | ||
itunes: { | ||
authors: [{ | ||
name: undefined, | ||
}], | ||
block: undefined, | ||
duration: undefined, | ||
explicit: undefined, | ||
image: undefined, | ||
isClosedCaptioned: undefined, | ||
order: undefined, | ||
subtitle: undefined, | ||
summary: undefined, | ||
} | ||
}] | ||
items: [ | ||
{ | ||
title: undefined, | ||
links: [ | ||
{ | ||
url: undefined, | ||
rel: undefined, | ||
}, | ||
], | ||
id: undefined, | ||
imageUrl: undefined, | ||
description: undefined, | ||
content: undefined, | ||
categories: [ | ||
{ | ||
name: undefined, | ||
}, | ||
], | ||
authors: [ | ||
{ | ||
name: undefined, | ||
}, | ||
], | ||
published: undefined, | ||
enclosures: [ | ||
{ | ||
url: undefined, | ||
length: undefined, | ||
mimeType: undefined, | ||
}, | ||
], | ||
itunes: { | ||
authors: [ | ||
{ | ||
name: undefined, | ||
}, | ||
], | ||
block: undefined, | ||
duration: undefined, | ||
explicit: undefined, | ||
image: undefined, | ||
isClosedCaptioned: undefined, | ||
order: undefined, | ||
subtitle: undefined, | ||
summary: undefined, | ||
}, | ||
}, | ||
], | ||
}; | ||
module.exports = { | ||
rss, | ||
}; |
{ | ||
"name": "react-native-rss-parser", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "React Native compatible package to parse RSS feeds", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "jest" | ||
}, | ||
@@ -26,7 +26,8 @@ "repository": { | ||
"dependencies": { | ||
"xmldom": "^0.1.27" | ||
"xmldom": "^0.3.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^5.0.0" | ||
"jest": "^26.4.2", | ||
"prettier": "^2.1.2" | ||
} | ||
} |
@@ -1,97 +0,52 @@ | ||
var utils = require('./utils'); | ||
var model = require('../model/rss'); | ||
var itunesParser = require('./itunes'); | ||
const utils = require('./utils'); | ||
const model = require('../model/rss'); | ||
const itunesParser = require('./itunes'); | ||
exports.parse = function(document) { | ||
let parsedFeed = Object.assign({}, model.rss); | ||
parsedFeed = mapChannelFields(document, parsedFeed); | ||
parsedFeed.type = 'atom-v1'; | ||
parsedFeed.items = mapItems(document); | ||
const getChannelTitle = (node) => utils.getElementTextContent(node, 'title'); | ||
return parsedFeed; | ||
}; | ||
function mapChannelFields(document, parsedFeed) { | ||
const channelNodes = utils.getElements(document, 'feed'); | ||
if (!channelNodes || channelNodes.length === 0) { | ||
throw new Error('Could not find channel node'); | ||
} | ||
const channelNode = channelNodes[0]; | ||
parsedFeed.title = getChannelTitle(channelNode); | ||
parsedFeed.links = getChannelLinks(channelNode); | ||
parsedFeed.description = getChannelDescription(channelNode); | ||
parsedFeed.copyright = getChannelCopyright(channelNode); | ||
parsedFeed.authors = getChannelAuthors(channelNode); | ||
parsedFeed.lastUpdated = getChannelLastUpdated(channelNode); | ||
parsedFeed.lastPublished = getChannelLastPublished(channelNode); | ||
parsedFeed.categories = getChannelCategories(channelNode); | ||
parsedFeed.image = getChannelImage(channelNode); | ||
parsedFeed.itunes = itunesParser.parseChannel(channelNode); | ||
return parsedFeed; | ||
} | ||
function getChannelTitle(node) { | ||
return utils.getElementTextContent(node, 'title'); | ||
} | ||
function getChannelLinks(node) { | ||
const getChannelLinks = (node) => { | ||
const links = utils.getChildElements(node, 'link'); | ||
return links.map(function(link) { | ||
return { | ||
url: link.getAttribute('href'), | ||
rel: link.getAttribute('rel') | ||
}; | ||
}); | ||
} | ||
return links.map((link) => ({ | ||
url: link.getAttribute('href'), | ||
rel: link.getAttribute('rel'), | ||
})); | ||
}; | ||
function getChannelDescription(node) { | ||
return utils.getElementTextContent(node, 'subtitle'); | ||
} | ||
const getChannelDescription = (node) => | ||
utils.getElementTextContent(node, 'subtitle'); | ||
function getChannelCopyright(node) { | ||
return utils.getElementTextContent(node, 'rights'); | ||
} | ||
const getChannelCopyright = (node) => | ||
utils.getElementTextContent(node, 'rights'); | ||
function getChannelAuthors(node) { | ||
const getChannelAuthors = (node) => { | ||
const authors = utils.getChildElements(node, 'author'); | ||
return authors.map(function(author) { | ||
return { | ||
name: utils.getElementTextContent(author, 'name') | ||
}; | ||
}); | ||
} | ||
return authors.map((author) => ({ | ||
name: utils.getElementTextContent(author, 'name'), | ||
})); | ||
}; | ||
function getChannelLastUpdated(node) { | ||
return utils.getElementTextContent(node, 'updated'); | ||
} | ||
const getChannelLastUpdated = (node) => | ||
utils.getElementTextContent(node, 'updated'); | ||
function getChannelLastPublished(node) { | ||
return utils.getElementTextContent(node, 'published'); | ||
} | ||
const getChannelLastPublished = (node) => | ||
utils.getElementTextContent(node, 'published'); | ||
function getChannelCategories(node) { | ||
const getChannelCategories = (node) => { | ||
const categories = utils.getChildElements(node, 'category'); | ||
return categories.map(function(category) { | ||
return { | ||
name: category.getAttribute('term') | ||
} | ||
}); | ||
} | ||
return categories.map((category) => ({ | ||
name: category.getAttribute('term'), | ||
})); | ||
}; | ||
function getChannelImage(node) { | ||
var img = utils.getElementTextContent(node, 'image'); | ||
const getChannelImage = (node) => { | ||
let img = utils.getElementTextContent(node, 'image'); | ||
if(img === '' || img === undefined){ | ||
if (img === '' || img === undefined) { | ||
img = utils.getElementTextContent(node, 'logo'); | ||
} | ||
if(img === '' || img === undefined){ | ||
if (img === '' || img === undefined) { | ||
img = utils.getElementTextContent(node, 'icon'); | ||
@@ -107,99 +62,112 @@ } | ||
}; | ||
} | ||
}; | ||
function getItemTitle(node) { | ||
return utils.getElementTextContent(node, 'title'); | ||
} | ||
const getItemTitle = (node) => utils.getElementTextContent(node, 'title'); | ||
function getItemLinks(node) { | ||
const getItemLinks = (node) => { | ||
const links = utils.getChildElements(node, 'link'); | ||
const linksWithoutEnclosures = links.filter(link => | ||
link.getAttribute('rel') !== 'enclosure'); | ||
const linksWithoutEnclosures = links.filter( | ||
(link) => link.getAttribute('rel') !== 'enclosure' | ||
); | ||
return linksWithoutEnclosures.map(function(link) { | ||
return { | ||
url: link.getAttribute('href'), | ||
rel: link.getAttribute('rel') | ||
}; | ||
}); | ||
} | ||
return linksWithoutEnclosures.map((link) => ({ | ||
url: link.getAttribute('href'), | ||
rel: link.getAttribute('rel'), | ||
})); | ||
}; | ||
function getItemDescription(node) { | ||
return utils.getElementTextContent(node, 'summary'); | ||
} | ||
const getItemDescription = (node) => | ||
utils.getElementTextContent(node, 'summary'); | ||
function getItemContent(node) { | ||
return utils.getElementTextContent(node, 'content'); | ||
} | ||
const getItemContent = (node) => utils.getElementTextContent(node, 'content'); | ||
function getItemImage(node) { | ||
return utils.getElementTextContent(node, 'icon'); | ||
} | ||
const getItemImage = (node) => utils.getElementTextContent(node, 'icon'); | ||
function getItemAuthors(node) { | ||
const getItemAuthors = (node) => { | ||
const authors = utils.getChildElements(node, 'author'); | ||
return authors.map(function(author) { | ||
return { | ||
name: utils.getElementTextContent(author, 'name') | ||
}; | ||
}); | ||
} | ||
return authors.map((author) => ({ | ||
name: utils.getElementTextContent(author, 'name'), | ||
})); | ||
}; | ||
function getItemCategories(node) { | ||
const getItemCategories = (node) => { | ||
const categories = utils.getChildElements(node, 'category'); | ||
return categories.map(function(category) { | ||
return { | ||
name: category.getAttribute('term') | ||
} | ||
}); | ||
} | ||
return categories.map((category) => ({ | ||
name: category.getAttribute('term'), | ||
})); | ||
}; | ||
function getItemPublished(node) { | ||
var pub = utils.getElementTextContent(node, 'updated'); | ||
const getItemPublished = (node) => { | ||
let pub = utils.getElementTextContent(node, 'updated'); | ||
if(pub === '' || pub === undefined){ | ||
utils.getElementTextContent(node, 'published'); | ||
if (pub === '' || pub === undefined) { | ||
pub = utils.getElementTextContent(node, 'published'); | ||
} | ||
return pub; | ||
} | ||
}; | ||
function getItemId(node) { | ||
return utils.getElementTextContent(node, 'id'); | ||
} | ||
const getItemId = (node) => utils.getElementTextContent(node, 'id'); | ||
function getItemEnclosures(node) { | ||
const getItemEnclosures = (node) => { | ||
const links = utils.getChildElements(node, 'link'); | ||
const enclosureLinks = links.filter(link => | ||
link.getAttribute('rel') === 'enclosure'); | ||
const enclosureLinks = links.filter( | ||
(link) => link.getAttribute('rel') === 'enclosure' | ||
); | ||
return enclosureLinks.map(function(link) { | ||
return { | ||
url: link.getAttribute('href'), | ||
length: link.getAttribute('length'), | ||
mimeType: link.getAttribute('type') | ||
}; | ||
}); | ||
} | ||
return enclosureLinks.map((link) => ({ | ||
url: link.getAttribute('href'), | ||
length: link.getAttribute('length'), | ||
mimeType: link.getAttribute('type'), | ||
})); | ||
}; | ||
function mapItems(document) { | ||
const mapChannelFields = (document) => { | ||
const channelNodes = utils.getElements(document, 'feed'); | ||
if (!channelNodes || channelNodes.length === 0) { | ||
throw new Error('Could not find channel node'); | ||
} | ||
const channelNode = channelNodes[0]; | ||
return { | ||
title: getChannelTitle(channelNode), | ||
links: getChannelLinks(channelNode), | ||
description: getChannelDescription(channelNode), | ||
copyright: getChannelCopyright(channelNode), | ||
authors: getChannelAuthors(channelNode), | ||
lastUpdated: getChannelLastUpdated(channelNode), | ||
lastPublished: getChannelLastPublished(channelNode), | ||
categories: getChannelCategories(channelNode), | ||
image: getChannelImage(channelNode), | ||
itunes: itunesParser.parseChannel(channelNode), | ||
}; | ||
}; | ||
const mapItems = (document) => { | ||
const itemNodes = utils.getElements(document, 'entry'); | ||
return itemNodes.map(function(item) { | ||
return { | ||
title: getItemTitle(item), | ||
links: getItemLinks(item), | ||
description: getItemDescription(item), | ||
id: getItemId(item), | ||
imageUrl: getItemImage(item), | ||
content: getItemContent(item), | ||
authors: getItemAuthors(item), | ||
categories: getItemCategories(item), | ||
published: getItemPublished(item), | ||
enclosures: getItemEnclosures(item), | ||
itunes: itunesParser.parseItem(item) | ||
}; | ||
}); | ||
} | ||
return itemNodes.map((item) => ({ | ||
title: getItemTitle(item), | ||
links: getItemLinks(item), | ||
description: getItemDescription(item), | ||
id: getItemId(item), | ||
imageUrl: getItemImage(item), | ||
content: getItemContent(item), | ||
authors: getItemAuthors(item), | ||
categories: getItemCategories(item), | ||
published: getItemPublished(item), | ||
enclosures: getItemEnclosures(item), | ||
itunes: itunesParser.parseItem(item), | ||
})); | ||
}; | ||
exports.parse = (document) => ({ | ||
...model.rss, | ||
type: 'atom-v1', | ||
...mapChannelFields(document), | ||
items: mapItems(document), | ||
}); |
@@ -1,61 +0,26 @@ | ||
var utils = require('./utils'); | ||
var namespaces = require('./namespaces'); | ||
const utils = require('./utils'); | ||
const namespaces = require('./namespaces'); | ||
exports.parseChannel = function(node) { | ||
return { | ||
authors: getAuthors(node), | ||
block: getBlock(node), | ||
categories: getCategories(node), | ||
complete: getComplete(node), | ||
explicit: getExplicit(node), | ||
image: getImage(node), | ||
newFeedUrl: getNewFeedUrl(node), | ||
owner: getOwner(node), | ||
subtitle: getSubtitle(node), | ||
summary: getSummary(node) | ||
} | ||
}; | ||
const getAuthors = (node) => { | ||
const authors = utils.getElementTextContentArray( | ||
node, | ||
'author', | ||
namespaces.itunes | ||
); | ||
exports.parseItem = function(node) { | ||
return { | ||
authors: getAuthors(node), | ||
block: getBlock(node), | ||
duration: getDuration(node), | ||
explicit: getExplicit(node), | ||
image: getImage(node), | ||
isClosedCaptioned: getIsClosedCaptioned(node), | ||
order: getOrder(node), | ||
subtitle: getSubtitle(node), | ||
summary: getSummary(node), | ||
}; | ||
return authors.map((author) => ({ | ||
name: author, | ||
})); | ||
}; | ||
function getAuthors(node) { | ||
const authors = utils.getElementTextContentArray(node, 'author', namespaces.itunes); | ||
const getBlock = (node) => | ||
utils.getElementTextContent(node, 'block', namespaces.itunes); | ||
return authors.map(function(author) { | ||
return { | ||
name: author | ||
}; | ||
}); | ||
} | ||
const getSubCategories = (node) => { | ||
const categories = utils.getChildElements( | ||
node, | ||
'category', | ||
namespaces.itunes | ||
); | ||
function getBlock(node) { | ||
return utils.getElementTextContent(node, 'block', namespaces.itunes); | ||
} | ||
function getCategories(node) { | ||
const categories = utils.getChildElements(node, 'category', namespaces.itunes); | ||
return categories.map(function(category) { | ||
return { | ||
name: category.getAttribute('text'), | ||
subCategories: getSubCategories(category) | ||
} | ||
}); | ||
} | ||
function getSubCategories(node) { | ||
const categories = utils.getChildElements(node, 'category', namespaces.itunes); | ||
if (categories.length === 0) { | ||
@@ -65,44 +30,45 @@ return []; | ||
return categories.map(function(category) { | ||
return { | ||
name: category.getAttribute('text') | ||
} | ||
}); | ||
} | ||
return categories.map((category) => ({ | ||
name: category.getAttribute('text'), | ||
})); | ||
}; | ||
function getComplete(node) { | ||
return utils.getElementTextContent(node, 'complete', namespaces.itunes); | ||
} | ||
const getCategories = (node) => { | ||
const categories = utils.getChildElements( | ||
node, | ||
'category', | ||
namespaces.itunes | ||
); | ||
function getDuration(node) { | ||
return utils.getElementTextContent(node, 'duration', namespaces.itunes); | ||
} | ||
return categories.map((category) => ({ | ||
name: category.getAttribute('text'), | ||
subCategories: getSubCategories(category), | ||
})); | ||
}; | ||
function getExplicit(node) { | ||
return utils.getElementTextContent(node, 'explicit', namespaces.itunes); | ||
} | ||
const getComplete = (node) => | ||
utils.getElementTextContent(node, 'complete', namespaces.itunes); | ||
function getImage(node) { | ||
const getDuration = (node) => | ||
utils.getElementTextContent(node, 'duration', namespaces.itunes); | ||
const getExplicit = (node) => | ||
utils.getElementTextContent(node, 'explicit', namespaces.itunes); | ||
const getImage = (node) => { | ||
const images = utils.getChildElements(node, 'image', namespaces.itunes); | ||
if (images.length > 0) { | ||
return images[0].getAttribute('href'); | ||
} | ||
return images.length > 0 ? images[0].getAttribute('href') : undefined; | ||
}; | ||
return undefined; | ||
} | ||
const getIsClosedCaptioned = (node) => | ||
utils.getElementTextContent(node, 'isClosedCaptioned', namespaces.itunes); | ||
function getIsClosedCaptioned(node) { | ||
return utils.getElementTextContent(node, 'isClosedCaptioned', namespaces.itunes); | ||
} | ||
const getNewFeedUrl = (node) => | ||
utils.getElementTextContent(node, 'new-feed-url', namespaces.itunes); | ||
function getNewFeedUrl(node) { | ||
return utils.getElementTextContent(node, 'new-feed-url', namespaces.itunes); | ||
} | ||
const getOrder = (node) => | ||
utils.getElementTextContent(node, 'order', namespaces.itunes); | ||
function getOrder(node) { | ||
return utils.getElementTextContent(node, 'order', namespaces.itunes); | ||
} | ||
function getOwner(node) { | ||
const getOwner = (node) => { | ||
const owners = utils.getChildElements(node, 'owner', namespaces.itunes); | ||
@@ -113,3 +79,3 @@ | ||
name: undefined, | ||
email: undefined | ||
email: undefined, | ||
}; | ||
@@ -122,10 +88,33 @@ } | ||
}; | ||
} | ||
}; | ||
function getSubtitle(node) { | ||
return utils.getElementTextContent(node, 'subtitle', namespaces.itunes); | ||
} | ||
const getSubtitle = (node) => | ||
utils.getElementTextContent(node, 'subtitle', namespaces.itunes); | ||
function getSummary(node) { | ||
return utils.getElementTextContent(node, 'summary', namespaces.itunes); | ||
} | ||
const getSummary = (node) => | ||
utils.getElementTextContent(node, 'summary', namespaces.itunes); | ||
exports.parseChannel = (node) => ({ | ||
authors: getAuthors(node), | ||
block: getBlock(node), | ||
categories: getCategories(node), | ||
complete: getComplete(node), | ||
explicit: getExplicit(node), | ||
image: getImage(node), | ||
newFeedUrl: getNewFeedUrl(node), | ||
owner: getOwner(node), | ||
subtitle: getSubtitle(node), | ||
summary: getSummary(node), | ||
}); | ||
exports.parseItem = (node) => ({ | ||
authors: getAuthors(node), | ||
block: getBlock(node), | ||
duration: getDuration(node), | ||
explicit: getExplicit(node), | ||
image: getImage(node), | ||
isClosedCaptioned: getIsClosedCaptioned(node), | ||
order: getOrder(node), | ||
subtitle: getSubtitle(node), | ||
summary: getSummary(node), | ||
}); |
@@ -1,96 +0,49 @@ | ||
var utils = require('./utils'); | ||
var model = require('../model/rss'); | ||
var namespaces = require('./namespaces'); | ||
var itunesParser = require('./itunes'); | ||
const utils = require('./utils'); | ||
const model = require('../model/rss'); | ||
const namespaces = require('./namespaces'); | ||
const itunesParser = require('./itunes'); | ||
exports.parse = function(document) { | ||
let parsedFeed = Object.assign({}, model.rss); | ||
const getChannelTitle = (node) => utils.getElementTextContent(node, 'title'); | ||
parsedFeed = mapChannelFields(document, parsedFeed); | ||
parsedFeed.type = 'rss-v2'; | ||
parsedFeed.items = mapItems(document); | ||
const getChannelLinks = (node) => { | ||
const links = utils.getChildElements(node, 'link'); | ||
return parsedFeed; | ||
return links.map((link) => ({ | ||
url: link.textContent, | ||
rel: link.getAttribute('rel'), | ||
})); | ||
}; | ||
function mapChannelFields(document, parsedFeed) { | ||
const channelNodes = utils.getElements(document, 'channel'); | ||
const getChannelDescription = (node) => | ||
utils.getElementTextContent(node, 'description'); | ||
if (!channelNodes || channelNodes.length === 0) { | ||
throw new Error('Could not find channel node'); | ||
} | ||
const getChannelLanguage = (node) => | ||
utils.getElementTextContent(node, 'language'); | ||
const channelNode = channelNodes[0]; | ||
const getChannelCopyright = (node) => | ||
utils.getElementTextContent(node, 'copyright'); | ||
parsedFeed.title = getChannelTitle(channelNode); | ||
parsedFeed.links = getChannelLinks(channelNode); | ||
parsedFeed.description = getChannelDescription(channelNode); | ||
parsedFeed.language = getChannelLanguage(channelNode); | ||
parsedFeed.copyright = getChannelCopyright(channelNode); | ||
parsedFeed.authors = getChannelAuthors(channelNode); | ||
parsedFeed.lastUpdated = getChannelLastUpdated(channelNode); | ||
parsedFeed.lastPublished = getChannelLastPublished(channelNode); | ||
parsedFeed.categories = getChannelCategories(channelNode); | ||
parsedFeed.image = getChannelImage(channelNode); | ||
parsedFeed.itunes = itunesParser.parseChannel(channelNode); | ||
return parsedFeed; | ||
} | ||
function getChannelTitle(node) { | ||
return utils.getElementTextContent(node, 'title'); | ||
} | ||
function getChannelLinks(node) { | ||
const links = utils.getChildElements(node, 'link'); | ||
return links.map(function(link) { | ||
return { | ||
url: link.textContent, | ||
rel: link.getAttribute('rel') | ||
}; | ||
}); | ||
} | ||
function getChannelDescription(node) { | ||
return utils.getElementTextContent(node, 'description'); | ||
} | ||
function getChannelLanguage(node) { | ||
return utils.getElementTextContent(node, 'language'); | ||
} | ||
function getChannelCopyright(node) { | ||
return utils.getElementTextContent(node, 'copyright'); | ||
} | ||
function getChannelAuthors(node) { | ||
const getChannelAuthors = (node) => { | ||
const authors = utils.getElementTextContentArray(node, 'managingEditor'); | ||
return authors.map(function(author) { | ||
return { | ||
name: author | ||
}; | ||
}); | ||
} | ||
return authors.map((author) => ({ | ||
name: author, | ||
})); | ||
}; | ||
function getChannelLastUpdated(node) { | ||
return utils.getElementTextContent(node, 'lastBuildDate'); | ||
} | ||
const getChannelLastUpdated = (node) => | ||
utils.getElementTextContent(node, 'lastBuildDate'); | ||
function getChannelLastPublished(node) { | ||
return utils.getElementTextContent(node, 'pubDate'); | ||
} | ||
const getChannelLastPublished = (node) => | ||
utils.getElementTextContent(node, 'pubDate'); | ||
function getChannelCategories(node) { | ||
const getChannelCategories = (node) => { | ||
const categories = utils.getElementTextContentArray(node, 'category'); | ||
return categories.map(function(category) { | ||
return { | ||
name: category | ||
} | ||
}); | ||
} | ||
return categories.map((category) => ({ | ||
name: category, | ||
})); | ||
}; | ||
function getChannelImage(node) { | ||
const getChannelImage = (node) => { | ||
const imageNodes = utils.getChildElements(node, 'image'); | ||
@@ -104,3 +57,3 @@ | ||
width: undefined, | ||
height: undefined | ||
height: undefined, | ||
}; | ||
@@ -118,28 +71,22 @@ } | ||
}; | ||
} | ||
}; | ||
function getItemTitle(node) { | ||
return utils.getElementTextContent(node, 'title'); | ||
} | ||
const getItemTitle = (node) => utils.getElementTextContent(node, 'title'); | ||
function getItemLinks(node) { | ||
const getItemLinks = (node) => { | ||
const links = utils.getChildElements(node, 'link'); | ||
return links.map(function(link) { | ||
return { | ||
url: link.textContent, | ||
rel: link.getAttribute('rel') | ||
}; | ||
}); | ||
} | ||
return links.map((link) => ({ | ||
url: link.textContent, | ||
rel: link.getAttribute('rel'), | ||
})); | ||
}; | ||
function getItemDescription(node) { | ||
return utils.getElementTextContent(node, 'description'); | ||
} | ||
const getItemDescription = (node) => | ||
utils.getElementTextContent(node, 'description'); | ||
function getItemContent(node) { | ||
return utils.getElementTextContent(node, 'encoded', namespaces.content); | ||
} | ||
const getItemContent = (node) => | ||
utils.getElementTextContent(node, 'encoded', namespaces.content); | ||
function getItemAuthors(node) { | ||
const getItemAuthors = (node) => { | ||
let authors = utils.getElementTextContentArray(node, 'author'); | ||
@@ -151,10 +98,8 @@ | ||
return authors.map(function(author) { | ||
return { | ||
name: author | ||
}; | ||
}); | ||
} | ||
return authors.map((author) => ({ | ||
name: author, | ||
})); | ||
}; | ||
function getItemCategories(node) { | ||
const getItemCategories = (node) => { | ||
let categories = utils.getElementTextContentArray(node, 'category'); | ||
@@ -166,46 +111,69 @@ | ||
return categories.map(function(category) { | ||
return { | ||
name: category | ||
} | ||
}); | ||
} | ||
return categories.map((category) => ({ | ||
name: category, | ||
})); | ||
}; | ||
function getItemId(node) { | ||
return utils.getElementTextContent(node, 'guid'); | ||
} | ||
const getItemId = (node) => utils.getElementTextContent(node, 'guid'); | ||
function getItemPublished(node) { | ||
return utils.getElementTextContent(node, 'pubDate') || utils.getElementTextContent(node, 'dc:date'); | ||
} | ||
const getItemPublished = (node) => | ||
utils.getElementTextContent(node, 'pubDate') || | ||
utils.getElementTextContent(node, 'dc:date'); | ||
function getItemEnclosures(node) { | ||
const getItemEnclosures = (node) => { | ||
const enclosures = utils.getChildElements(node, 'enclosure'); | ||
return enclosures.map(function(enclosure) { | ||
return { | ||
url: enclosure.getAttribute('url'), | ||
length: enclosure.getAttribute('length'), | ||
mimeType: enclosure.getAttribute('type') | ||
} | ||
}); | ||
} | ||
return enclosures.map((enclosure) => ({ | ||
url: enclosure.getAttribute('url'), | ||
length: enclosure.getAttribute('length'), | ||
mimeType: enclosure.getAttribute('type'), | ||
})); | ||
}; | ||
function mapItems(document) { | ||
const mapChannelFields = (document) => { | ||
const channelNodes = utils.getElements(document, 'channel'); | ||
if (!channelNodes || channelNodes.length === 0) { | ||
throw new Error('Could not find channel node'); | ||
} | ||
const channelNode = channelNodes[0]; | ||
return { | ||
title: getChannelTitle(channelNode), | ||
links: getChannelLinks(channelNode), | ||
description: getChannelDescription(channelNode), | ||
language: getChannelLanguage(channelNode), | ||
copyright: getChannelCopyright(channelNode), | ||
authors: getChannelAuthors(channelNode), | ||
lastUpdated: getChannelLastUpdated(channelNode), | ||
lastPublished: getChannelLastPublished(channelNode), | ||
categories: getChannelCategories(channelNode), | ||
image: getChannelImage(channelNode), | ||
itunes: itunesParser.parseChannel(channelNode), | ||
}; | ||
}; | ||
const mapItems = (document) => { | ||
const itemNodes = utils.getElements(document, 'item'); | ||
return itemNodes.map(function(item) { | ||
return { | ||
title: getItemTitle(item), | ||
links: getItemLinks(item), | ||
description: getItemDescription(item), | ||
content: getItemContent(item), | ||
id: getItemId(item), | ||
authors: getItemAuthors(item), | ||
categories: getItemCategories(item), | ||
published: getItemPublished(item), | ||
enclosures: getItemEnclosures(item), | ||
itunes: itunesParser.parseItem(item) | ||
}; | ||
}); | ||
} | ||
return itemNodes.map((item) => ({ | ||
title: getItemTitle(item), | ||
links: getItemLinks(item), | ||
description: getItemDescription(item), | ||
content: getItemContent(item), | ||
id: getItemId(item), | ||
authors: getItemAuthors(item), | ||
categories: getItemCategories(item), | ||
published: getItemPublished(item), | ||
enclosures: getItemEnclosures(item), | ||
itunes: itunesParser.parseItem(item), | ||
})); | ||
}; | ||
exports.parse = (document) => ({ | ||
...model.rss, | ||
type: 'rss-v2', | ||
...mapChannelFields(document), | ||
items: mapItems(document), | ||
}); |
@@ -1,2 +0,2 @@ | ||
exports.getElements = function(node, tagName) { | ||
exports.getElements = (node, tagName) => { | ||
if (!node || !node.getElementsByTagName(tagName)) { | ||
@@ -6,8 +6,8 @@ return []; | ||
let elements = node.getElementsByTagName(tagName); | ||
const elements = node.getElementsByTagName(tagName); | ||
return Array.prototype.slice.call(elements); | ||
} | ||
}; | ||
exports.getChildElements = function(node, tagName, namespace) { | ||
exports.getChildElements = (node, tagName, namespace) => { | ||
if (!node) { | ||
@@ -17,5 +17,5 @@ return []; | ||
let elements = namespace ? | ||
node.getElementsByTagNameNS(namespace, tagName) : | ||
node.getElementsByTagName(tagName); | ||
const elements = namespace | ||
? node.getElementsByTagNameNS(namespace, tagName) | ||
: node.getElementsByTagName(tagName); | ||
@@ -26,7 +26,9 @@ if (!elements) { | ||
return Array.prototype.filter.call(elements, element => | ||
element.parentNode.nodeName === node.nodeName); | ||
} | ||
return Array.prototype.filter.call( | ||
elements, | ||
(element) => element.parentNode.nodeName === node.nodeName | ||
); | ||
}; | ||
exports.getElementTextContentArray = function(node, tagName, namespace) { | ||
exports.getElementTextContentArray = (node, tagName, namespace) => { | ||
const nodes = this.getChildElements(node, tagName, namespace); | ||
@@ -38,9 +40,9 @@ | ||
return nodes.map(node => node.textContent); | ||
} | ||
return nodes.map((node) => node.textContent); | ||
}; | ||
exports.getElementTextContent = function(node, tagName, namespace) { | ||
exports.getElementTextContent = (node, tagName, namespace) => { | ||
const array = this.getElementTextContentArray(node, tagName, namespace); | ||
return array.length === 0 ? undefined : array[0]; | ||
} | ||
}; |
# react-native-rss-parser | ||
> React Native compatible RSS parser | ||
@@ -8,6 +9,7 @@ | ||
Parse RSS data into a simple object structure. Currently supports; | ||
* RSS 2.0 specification | ||
* Atom 1.0 specification | ||
* Itunes elements for both RSS 2.0 and Atom 1.0 feeds | ||
- RSS 2.0 specification | ||
- Atom 1.0 specification | ||
- Itunes elements for both RSS 2.0 and Atom 1.0 feeds | ||
## Installation | ||
@@ -126,31 +128,40 @@ | ||
| Parsed Value | RSS v2.0 | Atom v1.0 | | ||
| ------------- | ------------- | ------------- | | ||
| title | title | title | | ||
| links | link | link | | ||
| description | description | subtitle | | ||
| language | language | | | ||
| copyright | copyright | rights | | ||
| authors | managingEditor| author | | ||
| published | pubDate | published | | ||
| updated | lastBuildDate | updated | | ||
| categories | category | category | | ||
| image | image | logo | | ||
| items | item | entry | | ||
| Parsed Value | RSS v2.0 | Atom v1.0 | | ||
| ------------ | -------------- | --------- | | ||
| title | title | title | | ||
| links | link | link | | ||
| description | description | subtitle | | ||
| language | language | | | ||
| copyright | copyright | rights | | ||
| authors | managingEditor | author | | ||
| published | pubDate | published | | ||
| updated | lastBuildDate | updated | | ||
| categories | category | category | | ||
| image | image | logo | | ||
| items | item | entry | | ||
### Item / Entry Level elements | ||
| Parsed Value | RSS v2.0 | Atom v1.0 | | ||
| ------------- | --------------- | ------------- | | ||
| id | guid | id | | ||
| title | title | title | | ||
| imageUrl | | icon | | ||
| links | link | link | | ||
| description | description | summary | | ||
| content | content:encoded | content | | ||
| categories | category / dc:subject| category | | ||
| authors | author / dc:creator | contributor | | ||
| published | pubDate / dc:date | published | | ||
| enclosures | enclosures | link | | ||
| Parsed Value | RSS v2.0 | Atom v1.0 | | ||
| ------------ | --------------------- | ----------- | | ||
| id | guid | id | | ||
| title | title | title | | ||
| imageUrl | | icon | | ||
| links | link | link | | ||
| description | description | summary | | ||
| content | content:encoded | content | | ||
| categories | category / dc:subject | category | | ||
| authors | author / dc:creator | contributor | | ||
| published | pubDate / dc:date | published | | ||
| enclosures | enclosures | link | | ||
## CHANGELOG | ||
### 1.5.0 | ||
- Updated xmldom to version 0.3.0 | ||
- Change tests to use Jest to ensure refactoring did not break anything (using snapshot tests) | ||
- Updated entire codebase to use up-to-date JavaScript syntax (arrow functions, const & let instead of var) | ||
- Bug Fix: Atom v1 should return published date when no updated date available (thanks to Serra19) | ||
## Development setup | ||
@@ -179,2 +190,2 @@ | ||
Distributed under the MIT license. See ``LICENSE`` for more information. | ||
Distributed under the MIT license. See `LICENSE` for more information. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
189
25868
2
10
541
1
+ Addedxmldom@0.3.0(transitive)
- Removedxmldom@0.1.31(transitive)
Updatedxmldom@^0.3.0