Ich hatte hier bereit über die Implementierung von Interwiki-Links mit Hilfe eines Shortcodes geschrieben. Mir war damals schon klar, dass das keine gute Lösung war, da man nicht die Standard-Syntax von Markdown für Links nutzen kann sondern den umständlichen Shortcode.
Lösung via Markdown Render Hooks
Heute habe ich endlich eine richtige Lösung gefunden. Statt über einen Shortcode habe ich einen Hook für den Markdown Renderer von Hugo verwendet.
Dafür habe ich die Datei render-link.html
in hugo/layouts/_default/_markup/
erstellt und das Folgende eingefügt: 1
{{ $link := "" }}
{{ $baseurl := "" }}
{{ $isRemote := false }}
{{ if hasPrefix .Destination "nat-wiki-new>" }}
{{ $baseurl = "https://wiki.natenom.de/" }}
{{ $link = replace .Destination "nat-wiki-new>" "" 1 }}
{{ else if hasPrefix .Destination "nat-files>"}}
{{ $baseurl = "https://f.natenom.de/" }}
{{ $link = replace .Destination "nat-files>" "" 1 }}
{{ else if hasPrefix .Destination "nat-blog>"}}
{{ $baseurl = "/" }}
{{ $link = replace .Destination "nat-blog>" "" 1 }}
{{ else if hasPrefix .Destination "wpde>"}}
{{ $baseurl = "https://de.wikipedia.org/wiki/" }}
{{ $link = replace .Destination "wpde>" "" 1 }}
{{ else if hasPrefix .Destination "wpen>"}}
{{ $baseurl = "https://en.wikipedia.org/wiki/" }}
{{ $link = replace .Destination "wpen>" "" 1 }}
{{ else if hasPrefix .Destination "github>"}}
{{ $baseurl = "https://github.com/" }}
{{ $link = replace .Destination "github>" "" 1 }}
{{ else if hasPrefix .Destination "giistvo>"}}
{{ $baseurl = "https://www.gesetze-im-internet.de/stvo_2013/__" }}
{{ $link = replace .Destination "giistvo>" "" 1 }}
{{ $link = (print $link ".html") }}
{{ else }}
{{ $link = .Destination }}
{{ end }}
{{ if or (strings.HasPrefix $link "http") (strings.HasPrefix $baseurl "http") }}
{{ $isRemote = true }}
{{ end }}
<a {{ if $isRemote }} class='urlextern' {{ end }} href="{{- $baseurl -}}{{- $link | safeURL -}}"{{- with .Title -}} title="{{- . -}}"{{ end }}>{{- .Text | safeHTML -}}</a>
{{- /* This comment removes trailing newlines. */ -}}
Den Code für die Prüfung, ob ein Link nach extern verweist, habe ich von hier.
Interwiki-Links
Eine Liste der Verfügbaren Interwiki-Links gibt es hier.
Ergebnis
Statt des umständlichen Shortcodes:
{{< interwiki dest="nat-blog" title="Linktext Lala" link="/2022/03/umzug-dokuwiki-hugo-2-pandoc/" >}}
Kann ich jetzt die ganz normale Syntax für Links von Markdown verwenden:
[Linktext lala](nat-blog>2022/03/umzug-dokuwiki-hugo-2-pandoc/)
Ergebnis: Linktext lala
Ich kann auch weiterhin normale Links ohne Interwiki-Link verwenden:
[Linktext lala](/2022/03/umzug-dokuwiki-hugo-2-pandoc/)
Ergebnis: Linktext lala
Das kann man sicher schöner machen. Aber wie sonst auch immer: Es funktioniert und das reicht mir. ↩︎
Kommentare
Kommentare sind für diesen Blogbeitrag (noch) nicht aktiviert. Nach der Veröffentlichung eines Blogbeitrags dauert das in der Regel noch ein paar Minuten.
Alternative: Anmerkung per E-Mail
Du kannst mir Anmerkungen zum Blogbeitrag per E-Mail schicken, wenn du hier klickst. Der Inhalt davon bleibt privat und wird nicht hier im Kommentarbereich veröffentlicht.