The Updates section
   2 min read

Zdoc has an “Updates” section, but the articles inside it were created by hand. Since I’m lazy (laziest person in the world right here) and I don’t like to do things by hand when they can be automated, I tried to get some way of producing a Site Index without handcrafting each page.
In WordPress and other dynamic platforms it’s dead easy, but on a static site like Hugo, all the proposed solutions involved plugins, NPM, Node.js, things I did not want to mess with (for now).
Fortunately, there’s a way of faking this functionality using an internal Hugo system, Taxonomies. Thankfully, I do not use Taxonomies for anything else, but I don’t know at the moment if doing this will prevent other uses for them.
I added this to config.toml

1
2
3
4
5
6
7
[taxonomies]
year = "year"
month = "month"

[permalinks]
year = "updates/:slug/"
month = "updates/:slug/"

This automatically adds pages site/updates/year e pages site/updates/year/month, filled with the :slug of all pages that have these values in the variables year: and month: that we can now start placing on the frontmatter of each page, like this:

1
2
3
4
5
6
7
---
title: "The Updates section"
description: "Hacking until the tree falls off..."
[...]
year: ["2021"]
month: ["2021/09"]
---

To generate these fields automatically when you create an article or post with hugo new, I added the following to the frontmatter of the archetypes archetypes/blog/post.md and archetypes/default.md:

year: ["{{ now.Format "2006" }}"]
month: ["{{ now.Format "2006/01" }}"]

And to prevent these pages from being formatted like they were part of the Blog section (which is the default configuration for Taxonomies), I changed the following in layouts/_default/taxonomy.html

1
2
3
4
5
6
7
<div class="top">
  <header class="header__wrapper bgcolor__header">
    {{ with .Site.GetPage "section" "blog" }}
      {{ if eq .Params.blogHeaderType "img" }}
        <!-- {{ partial "header/blog-header.html" . }} -->
         {{ partial "header/custom-header.html" . }}
      {{ else }}


Then all I had to do was make static links in the content/<lang>/updates/_index.md page to the auto-generated pages:

1
2
3
Updates
[2021](/en/updates/2021)
- [August](/en/updates/2021/08)

Yes, I still have to create these links by hand, but the pages behind the links are created without forcing me to put each post there one by one.
That’s progress!

Sources:
https://discourse.gohugo.io/t/how-to-generate-chronological-blog-archives-in-hugo/13491/5
https://discourse.gohugo.io/t/strugling-with-chronological-blog-archive/31963/2
https://gohugo.io/templates/lists/

What's on this Page