Combining Section Feeds and Filtering the Homepage in Hugo


I’ve been setting up my website, and had things working pretty much exactly how I want them. But, I’d been struggling with how to properly separate content on the website while combining content in the main site feed. The main reason is to be able to have things like /reflections to separate week-in-review reflection posts without overwhelming the homepage with weekly posts that would make all other content that I post on less than a weekly cadence be lost in the noise.

It’s a pattern I’ve used for years - writing the Ephemerator plugin for WordPress to filter categories such as Asides or Photos from the homepage while still allowing all content into the main site feed so people don’t have to jump through hoops to subscribe to multiple feeds if they want to follow various types of content. Who would bother to do that? Not me.

In Hugo, the home.xml feed (that is used to generate /index.xml in the published site) is set to include the same content as the homepage. This is configured through the website’s config.toml file:

[params]
    # determine which sections are displayed on the homepage
    mainSections = ["posts", "podcast", "notes"]

I realized I could create my own paramater, and then use that to control the behaviour of the main feed. So, I copied the mainSections line, and pasted that immediately below it to add a new parameter to control the feed separately:

[params]
    # determine which sections are displayed on the homepage
    mainSections = ["posts", "podcast", "notes"]
    
    # determine which sections are included in the main site feed (in addition to their own separate section feeds, as usual)
    mainFeedSections = ["posts", "podcast", "photos", "notes", "reflections"]

So now I have a new parameter, mainFeedSections, that I can use to define the sections to include in the main RSS feed, separate from the parameter that controls the sections to include on the homepage.

I have a custom home.xml file in my site, separate from the default and the one provided by the theme (so I can change themes without breaking functionality), at: layouts/home.xml. It has a line that filters items to include only those in params.mainSections (as defined in config.toml, shown above): (in my home.xml file, it’s at line 15)

     {{ range first 15 (where .Site.RegularPages "Section" "in" site.Params.mainSections) }}
    <item>
      <title>{{ .Title }}</title>

A trivial modification, changes my line 15 to:

     {{ range first 15 (where .Site.RegularPages "Section" "in" site.Params.mainFeedSections) }}
    <item>
      <title>{{ .Title }}</title>

And now I’ve completely reproduced my Ephemerator WordPress plugin, without having to write any code. Tweaking a couple of text files that are used at runtime to generate the website.


comments powered by Disqus