Motivation
After a 4 year hiatus, I’m finally coming back around to this blog and doing some cleaning before writing again. Part of that cleaning includes making sure the RSS feed works as expected. To do so, it needs to check off the following:
- Provide valid RSS content
- Include the posts’ contents so they can easily be read with RSS readers
- Exclude pages that are not posts
The first box is ticked by default thanks to Hugo’s RSS generator
Task 1: Including the post’s contents
Looking at PaperMod’s RSS layout, I noticed the following code:
{{- if and site.Params.ShowFullTextinRSS .Content }}
<content:encoded>{{ (printf "<![CDATA[%s]]>" .Content) | safeHTML }}</content:encoded>
{{- end }}
This might’ve been added some time in the 4 years since I started this blog and seems to solve all of our problems. One update to our site’s config and voilà, the RSS feed’s XML includes our posts’ content in it!
One task down, one to go!
Task 2: Excluding specific pages
The easy way
As pointed out on Hugo’s Discourse there is a straightforward way to generate an RSS feed only for blog posts:
- Update the
output
section of your site’s config file so it does not include RSS
[...]
[output]
- home = ["HTML", "RSS", "JSON"]
+ home = ["HTML", "JSON"]
[...]
- Add an
_index.md
file to your/posts
subfolder with the following front matter:
+++
title = "Posts"
outputs = ["HTML", "RSS"]
+++
- We now have an RSS feed at
https://sunbro.dev/posts/index.xml
!
… but there’s one problem: this breaks the feed for anyone already subscribed to the current feed at https://sunbro.dev/index.xml
. That URL now 404s.
The “correct” way
Info
This definitely not the only way to do this and there are certainly more “Hugo-ist” ways to do it.
Now that we know we want to preserve the current /index.xml
let’s give ourselves a way to exclude certain pages from it.
Let’s add a new entry on to our pages’ config: RSSExclude
. You should add it to all the pages you want to exclude from the RSS feed.
For example in my projects.md
that I want to exclude I added:
+++
title = "Projects"
[...]
RSSExclude = true
+++
Now, let’s customise PaperMod’s index.xml
layout to take it into account.
Copy the theme’s file into your /layouts/_default/
and edit it as follows:
- {{- if and (ne .Layout `search`) (ne .Layout `archives`) }}
+ {{- if ne .Params.RssExclude true }}
<item>
[...]
</item>
{{- end }}
Instead of only excluding pages with the search
or archives
layouts we now exclude all pages where RSSExclude
is true
.
Task two, check!
Conclusion
I’m happy to see this was much easier than I thought. Although I wish PaperMod natively supported excluding pages from the RSS feed, this has only motivated me to tweak the theme’s layouts even further in the future.