forked from plaintextaccounting/plaintextaccounting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikilinks.lua
More file actions
47 lines (46 loc) · 1.7 KB
/
Copy pathwikilinks.lua
File metadata and controls
47 lines (46 loc) · 1.7 KB
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
46
47
-- 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 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
-- - these are not case insensitive - link and filename case must match exactly
function Link(el)
if el.title == "wikilink" -- pandoc 3.1ish
or el.classes:includes("wikilink") -- pandoc 3.9ish
then
t = el.target
t = t:gsub("[?']", "")
t = t:gsub("^ +", "")
t = t:gsub(" +$", "")
t = t:gsub(" ", "-")
t = t .. "#"
path, frag = t:match("([^#]*)#([^#]*)")
u = path
if not (
path:len() == 0 or
path:match('%.html$') or
path:match('%.%.$') or
path:match('/$')
) then
u = u .. ".html"
end
if frag:len() > 0 then
frag = pandoc.text.lower(frag)
u = u .. "#" .. frag
end
el.target = u
-- pandoc 3.9ish: must remove the wikilink class to see the new target
-- https://github.com/jgm/pandoc/issues/11663
el.classes = el.classes:filter(function(c) return c ~= "wikilink" end)
end
return el
end