Export org-roam backlinks with Gohugo
Since Org-roam export backlinks on Hugo no longer works, I found a solution to handle backlinks in Hugo itself.
The following Hugo partial template will add backlinks to a note if any.
{{ $re := $.File.BaseFileName }}
{{ $backlinks := slice }}
{{ range .Site.AllPages }}
{{ if and (findRE $re .RawContent) (not (eq $re .File.BaseFileName)) }}
{{ $backlinks = $backlinks | append . }}
{{ end }}
{{ end }}
<hr>
{{ if gt (len $backlinks) 0 }}
<div class="bl-section">
<h4>Links to this note</h4>
<div class="backlinks">
<ul>
{{ range $backlinks }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>
</div>
</div>
{{ else }}
<div class="bl-section">
<h4>No notes link to this note</h4>
</div>
{{ end }}
Then, include the previous partial to your single.html
. For example, this page
is created with a single.html
template, with the following content:
{{ define "main" }}
<article class="markdown">
<h1>
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
</h1>
{{ partial "docs/post-meta" . }}
<p>
{{- .Content -}}
</p>
{{ partial "docs/backlinks" . }}
</article>
{{ end }}
{{ define "toc" }}
{{ partial "docs/toc" . }}
{{ end }}