The static site generator Hugo doesn’t yet support Wikilinks. This is being considered on this github issue, but in the meantime we need to parse each page’s content to replace wikilinks with a relref internal page link.

I use the following code snippet to support wikilinks in Hugo.

It can be invoked when you’d normally use {{ .Content }} by calling this instead: {{- partial "content-with-wikilinks" . -}}.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{{/*
    Prints page content with two types of wikilinks rendered (with and without text).
	
	Based loosely on https://github.com/milafrerichs/hugo-wikilinks with these improvements:
		- Renders shortcodes correctly
		- Handles Links with text
		- Uses safeHTML instead of markdownify (renders <code></code> blocks correctly)

	This is redundant once a solution is developed for https://github.com/gohugoio/hugo/issues/3606

    @author @qcasey

    @context Type Page (.)

    @access public

*/}}
{{ $wikiregexWithText := "\\[\\[([^\\]\\|\\r\\n]+?)\\|([^\\]\\|\\r\\n]+?)\\]\\]" }}
{{ $wikiregex := "\\[\\[([^\\]\\|\\r\\n]+?)\\]\\]" }}

{{ $page := .Page }}
{{ $pageContent := .Content }}

{{ range ($wikilinks := .Content | findRE $wikiregex) }}
	{{ $link := . | replaceRE $wikiregex "$1" }}
	{{ $wikilink :=  printf "\\[\\[%s\\]\\]" $link }}

	{{ with relref $page $link }}
		{{ $link := printf "%s%s%s%s%s" "<a href=\"" . "\">" ($.Site.GetPage $link).Title "</a>"  }}
		{{ $pageContent = $pageContent | replaceRE $wikilink $link }}
	{{ end }}
{{ end }}

{{ range ($pageContent | findRE $wikiregexWithText) }}
	{{ $link := . | replaceRE $wikiregexWithText "$1" }}
	{{ $text := . | replaceRE $wikiregexWithText "$2" }}
	{{ $wikilink :=  printf "\\[\\[%s\\|%s\\]\\]" $link $text }}	

	{{ with relref $page $link }}
		{{ $link := printf "%s%s%s%s%s" "<a href=\"" . "\">" $text "</a>" }}
		{{ $pageContent = $pageContent | replaceRE $wikilink $link }}
	{{ end }}
{{ end }}

{{ $pageContent | safeHTML }}