Generate Backlinks

I think the only way to have Hugo generate backlinks to a page is to include a partial that loops over all of the content files.

Many thanks to whoever wrote Parsing Backlinks in Hugo for a partial to create backlinks. The partial is:

{{ $backlinks := slice }}
{{ $path_base := .page.File.ContentBaseName }}
{{ $path_base_re := printf `["/(]%s["/)]` $path_base }}

{{ range where site.RegularPages "RelPermalink" "ne" .page.RelPermalink }}
    {{ if (findRE $path_base_re .RawContent 1) }}
        {{ $backlinks = $backlinks | append . }}
    {{ end }}
{{ end }}

{{ with $backlinks }}
    <section class="backlinks">
        {{ printf "%s" ($.heading | default "<h2>Backlinks</h2>") | safeHTML }}
        <nav>
            <ul>
                {{ range . }}
                    <li><a href="{{ .RelPermalink }}#top">{{ .Title }}</a></li>
                {{ end }}
            </ul>
        </nav>
    </section>
{{ end }}

I added #top to the .RelPermalink URL. Other than that, it’s unchanged. They warn that this template is inefficient. I’ll revisit it eventually. There’s a discussion in the Hugo forum about it. Perhaps there are clues to a more efficient implementation there.

Other Resources