While I was playing around with it, I took some time to build a simple, personal RSS reader for fun. Here's what I came up with!
We're using just one component here, with an external setup function to initialise the scope. Our scope contains a feed to hold each item in our (soon to be) list of resolved feed items, a sources array containing the hardcoded list of RSS feeds we want to pull from, and an init function to kick the process off.
Now we'll iterate over each of the feeds and read them. Update your setup function with a get function and updated init function so it looks like this. Don't forget to instantiate the RSSParser at the top.
For each feed we iterate on, we use the RSS parser to fetch and parse the document. Then we iterate over each entry and log it. We're silently failing if a feed can't be loaded because ain't nobody got time for that.
Check your console and you should see a list of entries as objects, with properties like title and url.
Time to add them to our feed.
Adding parsed entries to our feed
First, create an addToFeed function. This takes the parsed entry and returns an object with just the data we need. You can always add additional properties if you fancy.
This one's easy. Just update the component to iterate over each feed item.
<divx-data="setup()"x-init="init"> <templatex-for="entry in feed"> <div> <spanx-text="entry.title"></span> <ax-bind:href="entry.link"x-text="entry.link"></a> <spanx-text="entry.date.toDateString()"></span> </div> </template></div>
As we iterate over each feed item (which we're calling entry) we output the title, link and formatted date.
If you're new to Alpine.js, x-for iterates over an array and renders the content inside the template for each item. x-text sets the innerText of the element you use it on and x-bind:href sets the href attribute of the anchor we're using to the given value.
In your browser, you should now see a list of feed items!
Sorting by published date
Right now the items are grouped by publication and not sorted according to the date they were published.