MATH201/themes/zettels/layouts/partials/backlinks.html

40 lines
1.2 KiB
HTML

<!--
Search for [[wikilink]] instead of "wikilink" when finding backlinks to prevent false positives: Otherwise, for a note whose filename/title is "now", every note containing the word "now" would be appended as a backlink)
Regex not working for notes with colons on filename because hugo .File.BaseFileName returns a string with colons removed.
-->
{{ $firstBracket := "\\[\\[" }}
{{ $lastBracket := "\\]\\]" }}
{{ $filename := .File.BaseFileName }}
{{ $wikilink := printf "%s%s%s" $firstBracket $filename $lastBracket }}
{{- $.Scratch.Add "backlinks" slice -}}
<h3>{{ i18n "backlinks" }}</h3>
<ul>
{{- range .Site.RegularPages -}}
{{ if (findRE $wikilink .Content) }}
{{ $.Scratch.Set "title" "" }}
<!-- If bear mode is on we'll show the filename instead of the yaml title -->
{{ if eq (.Param "bear") true }}
{{ $.Scratch.Set "title" .File.BaseFileName }}
{{ else }}
{{ $.Scratch.Set "title" .Title }}
{{ end }}
{{ $title := $.Scratch.Get "title" }}
{{ $.Scratch.Add "backlinks" (dict .Permalink $title) }}
<li><a href="{{ .Permalink }}">{{ $title }}</a></li>
{{ end }}
{{- end -}}
{{- $backlinks := $.Scratch.Get "backlinks" -}}
{{ if le (len $backlinks) 0 }}
<li>{{ i18n "no-backlinks" }}</li>
{{ end }}
</ul>