From 7d573e29c76991cc3ed3fd2f4b8d6ad40ae5d49b Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 18 Jan 2024 10:00:51 -1000 Subject: [PATCH] wikilinks: also remove ? and leading/trailing spaces from urls --- wikilinks.lua | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/wikilinks.lua b/wikilinks.lua index c611d2a..dd0a9dd 100644 --- a/wikilinks.lua +++ b/wikilinks.lua @@ -1,10 +1,14 @@ -- Pandoc's wikilinks_title_after_pipe extension does basic hyperlinking -- of [[bracketed wiki links]]. This lua filter does the rest, mimicking --- Obsidian's wiki linking where possible. It --- - replaces spaces with hyphens in the uri path and fragment --- - lower-cases the fragment --- - adds ".html" to the path when appropriate (preserving any fragment). +-- Obsidian's wiki linking where possible. It makes these adjustments +-- (to both path and fragment): +-- - removes problem characters: ' ? +-- - then removes any leading/trailing spaces +-- - replaces internal spaces with hyphens +-- - lower-cases the fragment if any +-- - adds ".html" to the path when appropriate, while preserving the fragment. -- This is not needed on hledger.org, but makes links work when rendered locally. +-- -- Current limitations: unlike Obsidian wikilinks, -- - these do not find files across folders - correct path is required -- - these are not aware of file existence - targets should exist @@ -12,24 +16,27 @@ function Link(el) if el.title == "wikilink" then - t = el.target .. "#" + t = el.target + t = t:gsub("[?']", "") + t = t:gsub("^ +", "") + t = t:gsub(" +$", "") t = t:gsub(" ", "-") - t = t:gsub("'", "") + t = t .. "#" path, frag = t:match("([^#]*)#([^#]*)") - t = path + u = path if not ( path:len() == 0 or path:match('%.html$') or path:match('%.%.$') or path:match('/$') ) then - t = t .. ".html" + u = u .. ".html" end if frag:len() > 0 then frag = pandoc.text.lower(frag) - t = t .. "#" .. frag + u = u .. "#" .. frag end - el.target = t + el.target = u end return el end