Dennis Lee


Hugo: change the automatic title and slug!

Archetypes in Hugo

2020-06-20


Update 2020-05-24: This post has been superseded by a newer post. The newer post can be found here.


In a previous post I complained about the format to automatically generated titles and slugs in Hugo.

Hugo makes me specify title and date manually, even when using the aforementioned format. And if I want punctuation in my title (like in this post), I even have to specify the slug manually, because otherwise it auto-generates the slug with the punctuation intact!

But what I didn’t know was that you can actually customize this! There is this optional folder in either your theme’s or your site’s root folder called /archetypes/ (the latter takes precedence over the former) where the default front matter is stored. Here’s what my /archetypes/default.md looks like now:

---
title: "{{ replaceRE "[[:^alpha:]]" " " .Name | humanize }}"
date: {{ .Date }}
slug: "{{ replaceRE "[[:^alpha:]]" "-" .Name | replaceRE "-{2,}" "-" | replaceRE "^-" "" | lower }}"
---

The title: template (using regexp) replaces any non-alphabetic character in the filename with a space, then pipes the result into the humanize function to use sentence case. Hugo seems to trim extra leading whitespace automatically, so that is not accounted for here.

For the slug, every non-alphabetic character is replaced with -. Then, sequences of 2 or more - are replaced with a single -. Then, a leading - is replaced with nothing. Finally, all letters are converted to lowercase.

That’s all for now. Don’t worry, the next post will actually be about something other than Hugo!