diff --git a/Makefile b/Makefile index 062d367..8d7d09f 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,9 @@ all: html # (including README, maybe useful for local preview. $(filter-out README.md, ...) to exclude). html: $(patsubst src/%,out/%,$(patsubst %.md,%.html,$(wildcard src/*.md src/quickref/*.md))) Makefile -PANDOC=pandoc -f markdown-smart-tex_math_dollars+autolink_bare_uris +PANDOC=pandoc \ + -f markdown-smart-tex_math_dollars+autolink_bare_uris+wikilinks_title_after_pipe \ + --lua-filter=fixwikilinks.lua # generate html from a md file out/%.html: src/%.md page.tmpl diff --git a/fixwikilinks.lua b/fixwikilinks.lua new file mode 100644 index 0000000..9323ce8 --- /dev/null +++ b/fixwikilinks.lua @@ -0,0 +1,26 @@ +-- Pandoc's wikilinks_title_after_pipe extension makes a start at linking +-- [[bracketed wiki links]]. This lua filter finishes the job and tries to +-- mimic Obsidian's wiki linking, by +-- - either hyphen-replacing or dropping all problem characters in the uri +-- - adding ".html" to the uri path +-- - preserving the uri fragment if any +-- - lower-casing the uri fragment + +function Link(elem) + if elem.title == "wikilink" then + t = elem.target .. "#" + t = string.gsub(t, " ", "-") + t = string.gsub(t, "'", "") + path, frag = string.match(t, "([^#]*)#([^#]*)") + t = path + if string.len(path) > 0 then + t = t .. ".html" + end + if string.len(frag) > 0 then + frag = pandoc.text.lower(frag) + t = t .. "#" .. frag + end + elem.target = t + end + return elem +end