Fixing Hugo's RSS Feed
RSS feeds are awesome, so let's make Hugo's feed more awesome.I’ve built this blog using Hugo, which I love for its sheer simplicity. However, I noticed that the autogenerated RSS feed
wasn’t showing any author information. It turns out that I copy-pasted an outdated hugo.toml
template that used an older method of specifying author information. This led to
the following changes in my hugo.toml
:
[params]
# before
author = "Loren Burkholder"
# after
author.name = "Loren Burkholder"
author.link = "https://lorendb.dev"
author.email = "computersemiexpert@outlook.com"
However, the feed still had some problems. The author for each post was formatted as computersemiexpert@outlook.com (Loren Burkholder)
, which is pointlessly verbose. Furthermore,
while I try to add tags to each post I make, Hugo was blissfully ignoring them in the RSS feed.
The author name formatting was fairly easy to fix; I just needed to copy the default RSS template
to layouts/_default/rss.xml
and change the author field from this:
{{- with $authorEmail }}<author>{{ . }}{{ with $authorName }} ({{ . }}){{ end }}</author>{{ end }}
to this:
{{- with $authorName }}<dc:creator>{{ . }}</dc:creator>{{ end }}
This will render each post with the author set to Loren Burkholder
. Much better! You will note that I changed the <author>
tag to <dc:creator>
. This is because the
RSS spec requires the <author>
tag to use the me@example.com (My Name)
format. <dc:creator>
, however,
allows you to use just a name without violating the spec. Unfortunately, it looks like Akregator can’t handle a feed without <author>
,
and Tiny Tiny RSS doesn’t observe the <dc:creator>
field, so I had to put back the original author field as well.
The tags were slightly trickier. Since I’m a total Hugo scripting noob, I ended up copying a general tag rendering snippet from the Hugo forums and adapting it to my RSS template:
{{ if isset .Params "tags" -}}
{{ range $index, $name := sort .Params.tags -}}
<category>{{ $name }}</category>
{{- end}}
{{- end }}
You can find the entire RSS template here if you want to use it in your own Hugo site.
An update from the next day
The morning after I published this post, I noticed that Hugo is also truncating the contents of RSS feeds, so anybody using a feed reader on this blog would only see the first
paragraph or so of each post. This is an absolute crime in the realm of feed readers; it forces people to leave their feed reader for an external site to read the post. Being
lazy, I copied somebody else’s approach of telling Hugo to use the post .Content
instead of the .Summary
. However, I
also found out (the hard way) that you must keep the call to transform.XMLEscape
; otherwise, your RSS feed will probably break when others try to add it to their feed readers.
I also realized that my original <author>
implementation didn’t meet RSS spec, so I modified it to come back in line with the spec.
Update (2/24/2024)
I’ve realized from this HN thread that using <dc:creator>
requires a modification to the <rss>
element:
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
This will actually import the appropriate RSS namespace to provide <dc:creator>
. Without this, some readers like Thunderbird would fail.