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-wikid>" }}
{{ $baseurl = "https://wiki.natenom.de/" }}
{{ $link = replace .Destination "nat-wikid>" "" 1 }}
{{ else if hasPrefix .Destination "nat-wikic>" }}
{{ $baseurl = "https://www.natenom.com/" }}
{{ $link = replace .Destination "nat-wikic>" "" 1 }}
{{ else if hasPrefix .Destination "nat-wwwc>" }}
{{ $baseurl = "https://www.natenom.com/" }}
{{ $link = replace .Destination "nat-wwwc>" "" 1 }}
{{ else if hasPrefix .Destination "nat-wwwd>"}}
{{ $baseurl = "https://www.natenom.de/" }}
{{ $link = replace .Destination "nat-wwwd>" "" 1 }}
{{ else if hasPrefix .Destination "nat-files>"}}
{{ $baseurl = "https://files.natenom.com/" }}
{{ $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
Jetzt habe ich die folgenden Interwiki-Links, die ich im Blog und im neuen Wiki nutzen kann:
nat-wikid -> https://wiki.natenom.de/$1
nat-wikic -> https://wiki.natenom.com/$1
nat-wwwc -> https://www.natenom.com/$1
nat-wwwd -> https://www.natenom.de/$1
nat-files -> https://files.natenom.com/$1
nat-blog -> /$1
wpde -> https://de.wikipedia.org/wiki/$1
wpen -> https://en.wikipedia.org/wiki/$1
github -> https://github.com/$1
giistvo -> https://www.gesetze-im-internet.de/stvo_2013/__$1.html
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
Anmerkungen?
Anmerkung zum Blogbeitrag per E-Mail
Hier klicken, um mir eine E-Mail zu diesem Beitrag zu schicken
Es gibt keine Kommentare mehr hier im Blog. Du kannst Anmerkungen zu den Inhalten entweder hier per E-Mail schreiben oder auf SocialMedia. Wenn ich hier Hinweise auf Fehler bekomme, werde ich das gerne in den Blogbeitrag einarbeiten. Was du mir schreibst, bleibt ansonsten privat.