-- Pandoc filter which turns `[TEXT](URL)` into `TEXT \` -- Save some typing and sidewise space! :-) local p = assert(pandoc, "Cannot find pandoc library") local u = assert(p.utils, "Cannot find pandoc.utils library") -- Classes which Pandoc inserts into links with URL/email as link text local plain_classes = { 'uri', 'email' } -- Get a nice-looking URL/email string local function bare_url (url) -- Remove wrapping angles just in case url = tostring(url):gsub("^%<", "") url = url:gsub("%>$", "") -- Remove the mailto protocol if any url = url:gsub("^mailto%:", "") return url end -- Get a pattern which matches a string literally -- by escaping all punctuation chars in the string local function quote_pat (str) return tostring(str):gsub('%p', '%%%0') end -- The main action function Link (link) -- Get the link text as a list of inlines local ct = link.content local url = link.target -- Return just the link text if the link is local if url:match('^%#') then return ct end -- Get the link text as a string (without formatting) local txt = u.stringify(link) -- See if we got one of Pandoc's automatic classes for _, cls in ipairs(plain_classes) do if link.classes:includes(cls) then -- If yes return just `` or `` return p.Str("<" .. tostring(bare_url(txt)) .. ">") end end -- Else if the link text contains the URL/email -- return just the link text if txt:match(quote_pat(bare_url(url))) then return ct end -- Else append `` or `` to the link text ct:extend({ p.Space(), p.Str("<" .. tostring(bare_url(url)) .. ">") }) return ct end