1
0

wikilinks: also remove ? and leading/trailing spaces from urls

This commit is contained in:
Simon Michael
2024-01-18 10:00:51 -10:00
parent e8e5ff5edb
commit 7d573e29c7

View File

@@ -1,10 +1,14 @@
-- Pandoc's wikilinks_title_after_pipe extension does basic hyperlinking -- Pandoc's wikilinks_title_after_pipe extension does basic hyperlinking
-- of [[bracketed wiki links]]. This lua filter does the rest, mimicking -- of [[bracketed wiki links]]. This lua filter does the rest, mimicking
-- Obsidian's wiki linking where possible. It -- Obsidian's wiki linking where possible. It makes these adjustments
-- - replaces spaces with hyphens in the uri path and fragment -- (to both path and fragment):
-- - lower-cases the fragment -- - removes problem characters: ' ?
-- - adds ".html" to the path when appropriate (preserving any fragment). -- - 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. -- This is not needed on hledger.org, but makes links work when rendered locally.
--
-- Current limitations: unlike Obsidian wikilinks, -- Current limitations: unlike Obsidian wikilinks,
-- - these do not find files across folders - correct path is required -- - these do not find files across folders - correct path is required
-- - these are not aware of file existence - targets should exist -- - these are not aware of file existence - targets should exist
@@ -12,24 +16,27 @@
function Link(el) function Link(el)
if el.title == "wikilink" then 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:gsub("'", "") t = t .. "#"
path, frag = t:match("([^#]*)#([^#]*)") path, frag = t:match("([^#]*)#([^#]*)")
t = path u = path
if not ( if not (
path:len() == 0 or path:len() == 0 or
path:match('%.html$') or path:match('%.html$') or
path:match('%.%.$') or path:match('%.%.$') or
path:match('/$') path:match('/$')
) then ) then
t = t .. ".html" u = u .. ".html"
end end
if frag:len() > 0 then if frag:len() > 0 then
frag = pandoc.text.lower(frag) frag = pandoc.text.lower(frag)
t = t .. "#" .. frag u = u .. "#" .. frag
end end
el.target = t el.target = u
end end
return el return el
end end