@astronautlabs/rss
v1.0.0
Published
An implementation of Really Simple Syndication
Downloads
39
Readme
@/rss
Support for Really Simple Syndication (RSS).
npm install @astronautlabs/rssParsing RSS:
import { RssSerializer } from '@astronautlabs/rss';
let feed = RssSerializer.parse(`<?xml version="1.0"?><rss><channel>...`);
console.log(feed.channel.title);
for (let item of feed.items) {
console.log(` - ${item.title}`);
}Rendering RSS:
import { RssSerializer } from '@astronautlabs/rss';
let xmlString = RssSerializer.stringify({
channel: {
title: `My Feed`,
// ...
},
items: [
{
title: `My item`,
// ...
}
]
});Custom Serializers
There are many formats which extend RSS. You can use this library to
parse those formats with createCustomSerializer:
import * as rss from '@astronautlabs/rss';
export interface MyChannel extends rss.Chanenl {
blueskyProfile?: string;
}
export interface MyItem extends rss.Item {
blueskyPost?: string;
}
const MySerializer = rss.createCustomSerializer<MyChannel, MyItem>({
parseChannel: el => ({
...rss.parseRssChannel(el),
blueskyProfile: el.child('blueskyProfile').bind(x => x.text()).optional()
}),
appendChannel: (parent, channel) => {
let element = rss.appendRssChannel(parent, channel);
rss.appendSimpleElement(element, 'blueskyProfile', channel.blueskyProfile);
return element;
},
parseItem: el => ({
...rss.parseRssItem(el),
blueskyPost: el.child('blueskyPost').bind(x => x.text()).optional()
}),
appendItem: (parent, item) => {
let element = rss.appendRssItem(parent, item);
rss.appendSimpleElement(element, 'blueskyPost', item.blueskyPost);
return element;
}
});
let feed = MySerializer.parse(`<?xml version="1.0"?><rss>....`);
// ^- Feed<MyChannel, MyItem>
The el object is a DomNavigator<Element> as provided by @astronautlabs/xml, which provides a convenient way to produce a Javascript object from a DOM tree by leveraging Monads.
