RSS Parser is an advanced library designed to convert RSS XML feeds into JavaScript objects efficiently. This project is a refined fork of the original work found at https://github.com/rbren/rss-parser, featuring enhancements and extended capabilities.
To integrate RSS Parser into your project, execute the following command:
npm install --save @ohshutit/rss-parser
RSS Parser offers flexibility by allowing RSS feeds to be parsed from URLs (parser.parseURL
) or directly from XML strings (parser.parseString
). It supports both callback and Promise-based workflows, catering to a variety of development preferences.
Here is a Node.js example demonstrating asynchronous usage with Promises:
const Parser = require('@ohshutit/rss-parser');
let parser = new Parser();
(async () => {
let feed = await parser.parseURL('https://www.reddit.com/.rss');
console.log(feed.title);
feed.items.forEach(item => {
console.log(`${item.title}: ${item.link}`);
});
})();
For TypeScript users, type definitions can be applied to enhance development experience:
import Parser, { IFeed, IItem } from '@ohshutit/rss-parser';
interface CustomFeed extends IFeed {
foo: string;
}
interface CustomItem extends IItem {
bar: number;
}
const parser = new Parser<CustomFeed, CustomItem>({
customFields: {
feed: ['foo'],
item: ['bar']
}
});
(async () => {
const feed = await parser.parseURL('https://www.reddit.com/.rss');
console.log(feed.foo); // Accessing custom field
feed.items.forEach(item => {
console.log(`${item.title}: ${item.link}`);
});
})();
For browser environments, consider using a module bundler like webpack. We also offer pre-built distributions for direct usage, but remember to include a Promise polyfill for compatibility.
<script src="/node_modules/@ohshutit/rss-parser/dist/rss-parser.min.js"></script>
<script>
const CORS_PROXY = "https://cors-anywhere.herokuapp.com/";
let parser = new RSSParser();
parser.parseURL(CORS_PROXY + 'https://www.reddit.com/.rss', function(err, feed) {
if (err) throw err;
console.log(feed.title);
feed.items.forEach(entry => {
console.log(`${entry.title}: ${entry.link}`);
});
});
</script>
Transitioning from version 2 to version 3 involves several minor adjustments:
- Instantiate
Parser
before usingparseString
orparseURL
. parseFile
method has been removed to improve browser support.- Configuration options are now provided through the
Parser
constructor. - The structure of the parsed output has been simplified for direct access.
Explore the complete format of parsed RSS feeds in our sample output.
Extend parsing capabilities with customFields
to include unique or additional feed and item properties.
Leverage xml2js
options for XML parsing customization and configure HTTP request details, including timeout, headers, and redirects, to fit your application's needs.
We encourage contributions! If you're adding a feature or fixing a bug, please include a test case. Follow the guidelines for running tests and publishing releases to ensure quality and consistency.
- Test your changes with
npm test
. If output files are affected, verify and update them as needed. - Publish releases with a clear process, ensuring changes are built and documented correctly.
For a detailed guide on contributing, running tests, and publishing, refer to the project's GitHub repository.
Join us in enhancing RSS Parser to meet the evolving needs of developers and applications worldwide.