Skip to content

Instantly share code, notes, and snippets.

@5t3ph
Created August 11, 2020 13:46
Show Gist options
  • Save 5t3ph/9c37f7e79db4b317bcb148a8dfcf466e to your computer and use it in GitHub Desktop.
Save 5t3ph/9c37f7e79db4b317bcb148a8dfcf466e to your computer and use it in GitHub Desktop.
/* Place in your _data directory to create an Eleventy data source from an RSS feed
* View package docs for full available parsed schema: https://www.npmjs.com/package/rss-parser
*
* TO USE in Nunjucks assuming a filename of `rssposts`
*
* {% for post in rssposts %}
* <article>
* <h3>
* <a href="{{ post.url }}">{{ post.title }}</a>
* </h3>
* <p>
* {{ post.description }}
* </p>
* </article>
* {% endfor %}
*/
const Parser = require("rss-parser");
const parser = new Parser();
module.exports = async () => {
const feed = await parser.parseURL("[FEED_URL]");
const content = feed.items[0].contentSnippet;
return [
{
title: feed.items[0].title,
url: feed.items[0].link,
description:
content.replace(/(<([^>]+)>)/gi, "").substr(0, content.lastIndexOf(" ", 160)) + "...",
},
];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment