Hugo Wikilinks
Wikilinks are a standard of many / most wiki software. However, Hugo does not have support for them. They have a simple form of [[Page Title]]
or [[Page Title|Display Text]]
, which makes them very useful for quick linking. This is how I implemented something like them.
Shortcode Proof of Concept
First, let’s start with a shortcode that gets us most of the logic. The being that shortcode { {% wl "Hugo Wikilinks" %}}
would link to this page.
Some examples
wl "Hugo Wikilinks"
: Hugo Wikilinkswl "Hugo Wikilinks" "Another name"
: Another namewl "hugo wikilinks"
: Hugo Wikilinkswl "invalid"
: invalidwl "FOO" "bar"
: bar
Note
For ease we’ll use the markdown form of calling shortcodes: %
instead of <
.
The contents of layouts/shortcodes/wl.html
:
|
|
- Line 1: Gets the Title of the page for comparison
- 2: Gets the display value override
- 5 - 12: Searches all regular pages (ones with valid titles) for the one that matches
- 8 - 10: Uses the Page’s Link Title if an override is not provided
- 14 - 18: Prints either the wiki link or the display string or the title
This is a 90% solution. However, we can make it the 100% solution with a lot more logic. So let’s leave it here for now.
If you are interested in more of a solution then milafrerichs/hugo-wikilinks has a good start.