Created
March 5, 2026 09:45
-
-
Save sspaeti/37d63fcea3feebae2eff3a83e201c47f to your computer and use it in GitHub Desktop.
Process Obsidian Wikilink and convert them to actual links for my GoHugo Blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {{- $content := .content | default .Page.RawContent -}} | |
| {{- $brainBaseURL := .Site.Language.Params.brainBaseURL | default "https://ssp.sh/brain/" -}} | |
| {{- /* Process wikilinks with proper URL formatting and alias support */ -}} | |
| {{- $content = replaceRE `\[\[([^\]]+)\]\]` `<WIKILINK_TEMP>$1</WIKILINK_TEMP>` $content -}} | |
| {{- /* Now process each wikilink individually */ -}} | |
| {{- $wikilinks := findRE `<WIKILINK_TEMP>([^<]+)</WIKILINK_TEMP>` $content -}} | |
| {{- range $wikilinks -}} | |
| {{- $linkContent := replaceRE `<WIKILINK_TEMP>([^<]+)</WIKILINK_TEMP>` "$1" . -}} | |
| {{- /* Check if this is an alias link (contains |) */ -}} | |
| {{- if strings.Contains $linkContent "|" -}} | |
| {{- /* Split on the pipe character */ -}} | |
| {{- $parts := split $linkContent "|" -}} | |
| {{- $linkTarget := index $parts 0 | strings.TrimSpace -}} | |
| {{- $displayText := index $parts 1 | strings.TrimSpace -}} | |
| {{- $linkURL := $linkTarget | lower | replaceRE `’|‘|'|['']` "" | replaceRE `[^a-z0-9\s-]` "" | replaceRE `\s+` "-" | replaceRE `-+` "-" | strings.TrimSuffix "-" | strings.TrimPrefix "-" -}} | |
| {{- $fullURL := printf "%s%s/" $brainBaseURL $linkURL -}} | |
| {{- $replacement := printf `<a href="%s">%s</a>` $fullURL $displayText -}} | |
| {{- $content = replace $content . $replacement -}} | |
| {{- else -}} | |
| {{- /* Regular wikilink without alias */ -}} | |
| {{- $linkText := $linkContent -}} | |
| {{- $linkURL := $linkText | lower | replaceRE `’|‘|'|['']` "" | replaceRE `[^a-z0-9\s-]` "" | replaceRE `\s+` "-" | replaceRE `-+` "-" | strings.TrimSuffix "-" | strings.TrimPrefix "-" -}} | |
| {{- $fullURL := printf "%s%s/" $brainBaseURL $linkURL -}} | |
| {{- $replacement := printf `<a href="%s">%s</a>` $fullURL $linkText -}} | |
| {{- $content = replace $content . $replacement -}} | |
| {{- end -}} | |
| {{- end -}} | |
| {{- return $content -}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment