Hugo and Fixing RSS Errors

Posted on 2nd of February 2024 | 611 words

Somebody contacted me and mentioned that there is something off with my RSS feeds, thanks for that. I don’t know what had happened and when, but for some reason my normal RSS link. Didn’t contain the correct information. It seemed to only contain the summary of the post. I recently did some overhaul in my own Hugo theme and its layouts so most likely I had put something to wrong place while doing that causing this.

When I started looking at what Hugo generated, it seemed that the correct feed was actually located at the root of my site, so topikettunen.com/feed.xml but this isn’t right. I remember making it so at one point that I only generate the feed in the blog section since - personally speaking - I don’t really see the benefit of generating the feed elsewhere.

This seemed to be caused by error in my Hugo configs, which most likely I had accidentally changed at some point. I had to add the following to my config.toml:

[outputs]
  home = ["HTML"]
  section = ["HTML", "RSS"]
  taxonomy = ["HTML"]

This makes it so that Hugo generates only HTML when it comes to home and taxonomy page (so archive and tag for me). In section, like blog , it’ll generate HTML and RSS. So that was fixed.

Like I mentioned above, the RSS feed generated under section was still wrong since it only contained the summary. In my Hugo theme, I had already created a custom layouts/index.rss.xml in there with the change:

-<description>{{ .Summary | html }}</description>
+<description>{{ .Content | safeHTML }}</description>

So that it’ll generate the whole content in there instead of summary, which Hugo does by default. Unfortunately, this was one was in the wrong place for section RSS. It should be in layouts/section/section.rss.xml, and then it works fine!

Also having full content under description in the RSS XML seems quite odd so I fixed it to look like:

<description>{{ .Summary | html }}</description>
<content:encoded>{{ .Content | html }}</content:encoded>

To have both in their own correct fields.

I also noticed that my custom notice shortcode looked quite ugly in the RSS feed. You might’ve seen these notices e.g. when reading my Sila dev log. The way I had done this shortcode was the having the following in layouts/shortcodes/notice.html:

<table class="notice">
  <tbody>
    <tr>
      <td>
        {{ .Inner }}
      </td>
    </tr>
  </tbody>
</table>

I know, this probably could be done with something else than table, but hey, I’m not a designer.

Which I then would call in my Markdown files like:

{{< notice >}}
<strong>Plug</strong>: <a href="https://github.com/topikettunen/sila" target="_blank">Follow the Sila development here.</a>
{{< /notice >}}

Also while writing this, I learned how to escape shortcodes in Hugo in case you want to use them in write them in your Markdown, but not actually use them. You need wrap the short code inside {{</* notice */>}}.

But unfortunately it would just generate that in the beginning of the RSS content without proper HTML tags etc. So instead of using the notice as a shortcode, I removed the shortcodes from Markdown and just added the following to my layouts/_default/single.html:

{{ if in .Params.tag "sila"}}
<table class="notice">
  <tbody>
    <tr>
      <td>
        <strong>Plug</strong>: <a href="https://github.com/topikettunen/sila" target="_blank">Follow the Sila development here.</a>
      </td>
    </tr>
  </tbody>
</table>
{{ end }}

This only works when blog has a tag sila in it. Of course, if I would like to use such a notice in some other posts, this wouldn’t work, but for now, it’s good enough for me. This way also Hugo doesn’t include the notice in the generated RSS, making the feed look little bit neater!

But hey, at least now the RSS feed should be working how it should!