Bytes & Bobs

Hugo Tips

, updated on

Like my previous post of Zola tips, having migrated to Hugo, I’ll document here anything I find that I’ve needed to research or do to make Hugo do something I’ve wanted.

Literal Hugo shortcodes in markdown

Inevitably I will write as much about Hugo as I write about anything else. I will therefore, as in this post, want to show literal shortcodes and other things that Hugo will try to interpret. To write something like {{< some_short_code ... >}} you need to add additional markup so Hugo’s build doesn’t try to actually run the shortcode. This post is even more awkward, because I now need to write the escaped version of the shortcode verbatim…

What you need to write in your markdown to show {{< some_short_code ... >}} is:

{{</* some_short_code ... */>}}

Incidentally, what I wrote to be able to show the comment syntax just above is:

{{</*/* some_short_code ... */*/>}}

Effectively, in your markdown you need one more set of /*, */ than you want displayed in your post.

Linking to other posts isn’t difficult. E.g. to link to this post I could use the following markdown, [Hugo tips](/posts/hugo-tips). However, what if I wrote /posts/huggo-tips (typo)? That’s a broken link and I wouldn’t notice. You can use the relref shortcode instead, and that will verify the link is valid at build time, preventing the build if it is not.

[Hugo tips]({{< relref "posts/hugo-tips" >}})

Now, if you get it wrong, you’ll see an error like the following in the console.

ERROR [en] REF_NOT_FOUND: Ref “posts/huggo-tips”

Be aware that if you link to a draft, using hugo serve for example will error, because the drafts aren’t being built.

Full content in RSS feed

By default Hugo’s RSS feed only includes article summaries. That’s fine if that’s your thing, but I want the full content in there. Fortunately, I’m not the only one and the answer I found was at Junian.Dev.

The short version is, grab the default Hugo RSS template, save it in layouts/_default/rss.xml and change the single reference of .Summary to .Content.

Render HTML in markdown

I think this was the default behaviour in Zola, but isn’t in Hugo. I have no concerns about it being unsafe, as it’s only me in control of the markdown content, so let’s get it working. In hugo.toml add the following (docs).

[markup]
  [markup.goldmark]
    [markup.goldmark.renderer]
      unsafe = true

Configurable blockquotes

A markdown extension I’ve not seen before. Previously, the tl;dr blockquotes at the beginning of some posts were literally:

> **tl;dr** The description here

Which is fine, but makes changing how they’re displayed more difficult in future as I’d have to change something like 4 posts… Goldmark in Hugo supports the following syntax:

> [!tldr]
> The description here

If you enable it in hugo.toml (docs-blockquote-alert).

[markup]
  [markup.goldmark]
    [markup.goldmark.parser]
      [markup.goldmark.parser.attribute]
        block = true

Note, before you enable the feature, the [!tldr] will be rendered literally.

The default Hugo blockquote template is however the following, so the annotation will be ignored.

<blockquote>
  {{ .Text }}
</blockquote>

So define layouts/_default/_markup/render-blockquote.html with something like this:

{{ if eq .Type "alert" }}
<blockquote class="alert alert-{{ .AlertType }}">
  {{ .Text }}
</blockquote>
{{ else }}
<blockquote>
  {{ .Text }}
</blockquote>
{{ end }}

Now, a normal > blockquote will render as before, but one with the > [!tldr] annotation will render as:

<blockquote class="alert alert-tldr">
  {{ .Text }}
</blockquote>

All that’s left then is to add some CSS to display the tl;dr in a :before.

.alert-tldr p:before {
  content: 'tl;dr ';
  font-weight: bold;
  font-style: italic;
}

This lets you define a number of different “alert” styles. There are also additional attributes you can use via using curlies after your quote (docs-blockquote-example in the docs), used for e.g. citations, but I’m not using them yet.