1
0
Fork 0
mirror of https://github.com/nix-community/nixvim.git synced 2025-11-08 19:46:06 +01:00
nixvim/docs/fix-links/filter.lua

44 lines
949 B
Lua

local len = pandoc.text.len
local sub = pandoc.text.sub
-- True if str starts with prefix
local function hasPrefix(prefix, str)
local pfxLen = len(prefix)
local strLen = len(str)
if pfxLen == strLen then
return prefix == str
end
if pfxLen < strLen then
return prefix == sub(str, 1, pfxLen)
end
return false
end
function Link(link)
local target = link.target
-- Check for relative links
-- TODO: handle ../
while hasPrefix("./", target) do
-- strip leading ./
target = sub(target, 3)
end
if hasPrefix("#", target) then
-- No-op for anchor targets on the same page
return nil
end
if not hasPrefix("https://", target) then
link.target = githubUrl .. target
return link
end
-- Check for absolute links, pointing to the docs website
if docsUrl == target then
link.target = "."
return link
end
if hasPrefix(docsUrl, target) then
local i = len(docsUrl) + 1
link.target = sub(target, i)
return link
end
end