2
votes

I am trying to generate a post category-specific RSS feed for a GitHub Pages Jekyll website.

I understand that the jekyll-feed plugin can generate an RSS feed for all posts, but according to this GitHub Issue, category-specific feeds are not yet supported.

Other approaches to generate a category-specific feed (i.e., here and here are not supported by GitHub Pages because it won't support custom plugins.

Is there a way to generate a category-specific RSS feed using Jekyll with GitHub Pages?

3
Please consider "accepting" Joost's answer so he receives credit for it.gerwitz
Catagory-specific feeds are now supported. See github.com/jekyll/jekyll-feed for details.chmaynard

3 Answers

5
votes

You can just create your own XML or RSS file. For this answer I have used this example to build on. I also used Wikipedia for an example RSS feed.

filename: categoryname.rss

---
layout: null
---
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
 <title>{{ site.title }}</title>
 <description>{{ site.description }}</description>
 <link>{{ site.url }}</link>
 <lastBuildDate>{{ site.time | date_to_rfc822 }}</lastBuildDate>
 <pubDate>{{ site.time | date_to_rfc822 }}</pubDate>
 <ttl>1800</ttl>

 {% for post in site.categories.categoryname %}
 <item>
  <title>{{ post.title }}</title>
  <description>{{ post.description }}</description>
  <link>{{ post.url }}</link>
  <guid isPermaLink="true">{{ site.url }}{{ post.url }}</guid>
  <pubDate>{{ post.date | date_to_rfc822 }}</pubDate>
 </item>
 {% endfor %}

</channel>
</rss>

The title should be something like: 'mysitenames categoryname archive'. The description could be your category description. Link is the link to the category. The 'lastBuildDate' value could be the date of your last post and 'pubDate' could be the same.

Please let me know if you have any questions.

0
votes

Following I've published for collections hosted on GitHub Pages, however, with modification it might by useful for categories too.


Downloading within another project is facilitated via Git's submodule features...

cd your-project
git checkout gh-pages

mkdir -vp _layouts/modules

git submodule add -b master --name feed-rss2\
 https://github.com/liquid-utilities/feed-rss2.git\
 _layouts/modules/feed-rss2

Note, https URLs are required for GitHub Pages compatibility.

... which should cause code similar to the following to become available...

_layouts/modules/feed-rss2/feed-rss2.html

---
layout: null
version: 0.0.1
license: AGPL-3.0
author: S0AndS0
---
{% capture workspace__rss2 %}
  {% assign date_format = '%a, %d %b %Y %T %z' %}
  {% assign collection_name = page.collection_name | default: include.collection_name | default: nil %}
  {% assign target_collection = site[collection_name] %}

  {% assign collection_home = page.collection_home | default: collection_name %}
  {% assign collection_description = page.description | default: site.description %}
  {% assign this_time_stamp = page.date | date: '%s' %}
  {% assign latest_time_stamp = page.date | date: '%s' %}

  {% assign rss_items_has_output = false %}
  {% capture workspace__rss2__entries %}
    {% for post in target_collection %}
      {% if page.relative_path == post.relative_path or post.slug == 'feed' %}
        {% continue %}
      {% elsif post.slug == collection_name or post.slug == 'index' %}
        {% continue %}
      {% endif %}

      {% assign rss_items_has_output = true %}
      {% assign post_synopsis = post.description | default: post.excerpt %}
      {% assign post_date_updated = post.date_updated | default: post.date %}

      {% assign this_time_stamp = post_date_updated | date: '%s' %}
      {% if latest_time_stamp < this_time_stamp %}{% comment %}> Syntax highlighting work-around{% endcomment %}
        {% assign latest_time_stamp = this_time_stamp %}
      {% endif %}

      <item>
        <title>{{- post.title | xml_escape -}}</title>
        <link>{{- post.url | absolute_url -}}</link>
        <guid isPermaLink="true">{{- post.url | absolute_url -}}</guid>
        <pubDate>{{- post_date_updated | date: date_format -}}</pubDate>
        <description>{{- post_synopsis | xml_escape -}}</description>
      </item>

      {% assign post_synopsis = nil %}
    {% endfor %}
  {% endcapture %}

  {% assign page_rights = page.copyright | default: collection_home.copyright | default: site.copyright %}
  {% assign page_rights = page_rights | default: site.github.license.name | default: 'All rights reserved' %}

  <?xml version="1.0" encoding="UTF-8" ?>
  <rss version="2.0">
    <channel>
      <title>{{- page.title | default: 'RSS Title' | xml_escape -}}</title>
      <link>{{- page.url | absolute_url -}}</link>
      <description>{{ collection_description | xml_escape }}</description>
      <copyright>Copyright {{ site.time | date: '%Y' }} {{ page_author | xml_escape }}. {{ page_rights | xml_escape }}</copyright>
      <lastBuildDate>{{- site.time | date: date_format -}}</lastBuildDate>
      <pubDate>{{- latest_time_stamp | date: date_format -}}</pubDate>
      <ttl>{{- page.time_to_live | default: '1800' -}}</ttl>
      {% if rss_items_has_output %}
        {{ workspace__rss2__entries | strip }}{% assign workspace__rss2__entries = nil %}
      {% endif %}
    </channel>
  </rss>
{% endcapture %}{{ workspace__rss2 | strip }}{% assign workspace__rss2 = nil %}

... Setting a feed file within a collection directory would then look like...

_example-collection/example-collection.rss

---
layout: modules/feed-rss2/feed-rss2
title: Example Collection
collection_name: example-collection
collection_home: /example-collection/
date: 2019-07-23 21:12:13 -0700
content_type: xhtml
permalink: /:collection/:name:output_ext
---

... and minor additions of FrontMatter of each post/page...

_example-collection/something-about-something.markdown

---
layout: post
title: Something About Something
description: Example collection page about something
date: 2019-07-21 11:42:11 -0300
time_to_live: 1800
---

... Which should render results similar to the live demo hosted by GitHub Pages.

Note, check the documentation for updating and cloning tips and caveats.


Regardless of if ya utilize the above project's code I'll suggest looking into the submodule features (hint git help submodule), because such things allow for code reuse in multiple repositories while keeping things version tracked and easily updated. Plus submodules are compatible with GitHub Pages, meaning that submodules are about as close to third-party plugin support as one can get without exploring Continuous Integration solutions.

The other key take-away would be, to try and utilize layouts for these types of solutions.

Feel free to comment if ya get stuck or something from above is confusing.

0
votes

Expanding on JoostS answer:

---
layout: null
---
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>{{ site.title }}</title>
    <description>{{ site.description }}</description>
    <link>{{ site.url }}</link>
    <lastBuildDate>{{ site.time | date_to_rfc822 }}</lastBuildDate>
    <pubDate>{{ site.time | date_to_rfc822 }}</pubDate>
    <ttl>1800</ttl>
    {% for post in site.pages %}
    {% if post.title %}
    <item>
      <title>{{ post.title }}</title>
      <description>{{ post.description }}</description>
      <link>{{ site.url }}{{ post.url }}</link>
      <guid isPermaLink="true">{{ site.url }}{{ post.url }}</guid>
      <pubDate>{{ post.date | date_to_rfc822 }}</pubDate>
    </item>
    {% endif %}
    {% endfor %}
  </channel>
</rss>