RSS Feeds by Collection in Jekyll
Yesterday I ran into an interesting limitation of the jekyll-feed gem while working on my StarbornHQ collaboration. As it turns out there is no support for creating a RSS feed of just a certain collection or group of collections. Since Starborn is a serial novel I want to post new scenes and lore pieces to the feed but not necessarily the larger structural elements like episodes and seasons.
There has been some movement on the issue of collection specific feeds but so far no pull request submitted. Since I’m not a great hand at Ruby I decided to see if I could fix the issue without actually opening up the gem and mucking about in things…and you know what? You can!
The first order of business is to crib the feed.xml
template directly from the
jekyll-feed gem because it’s already well structured and tested so why write one
from scratch. I copied it into my site _layouts
directory and opened it up to
see if I could change the content that it displayed dynamically. The original
template section is shown below and you can see that it specifically targets the
hardcoded posts
collection and prints them out to the feed.
{% assign posts = site.posts | where_exp: "post", "post.draft != true" %}
This layout actually makes switching up the targeting pretty easy. First we
will want to start with all documents in the site instead of just the posts
collection and then we filter down from there. We can also create multiple feeds
with this template by creating multiple top level pages and changing the
collections
key we list in their YAML front matter.
{% assign posts = site.documents | where_exp: "post", "post.draft != true" | where_exp: "post", "page.collections contains post.collection" | sort "post.date" | reverse %}
To actually display the feed we need to create a new top level document named
feed.xml
and fill in the following YAML front matter to target just the
collections we want to show. Additional, more targeted, feeds can also be created
by just adding more XML files and changing up the front matter.
---
layout: feed
collections: scenes, lore
---
Last step is to make sure that jekyll-feed
isn’t listed anymore in your _config.yml
file so we don’t have it fighting with our custom implementation. Well, there
you have it! Have fun creating custom RSS feeds and while you’re at it, give
Starborn a look and maybe subscribe there too!