Adding anchor links to Hugo headings (tentatively)
I’m considering heading sections with the site’s notes to be somewhat independent of their parent document. With that in mind, it makes sense to add anchor links to the headings.
I’ll try this for a while:
layouts/_markup/render-heading.html:
<h{{ .Level }} id="{{ .Anchor }}" {{- with .Attributes.class }} class="{{ . }}" {{- end }}>
{{- .Text -}}
{{- if ne hugo.Environment "minimal" }}
{{ " " -}}
<a class="anchor" href="#{{.Anchor}}">#</a>
{{- end -}}
</h{{ .Level }}>
The {{- if ne hugo.Environment "minimal" }} condition prevents the links from being added to the
HTML that’s used for generating embeddings and HTML fragments.
References #
Hugo docs. “Render hooks > headings.” Accessed on January 29, 2026. https://gohugo.io/render-hooks/headings/ .