Skip to content

Commit

Permalink
docs/fix-links: generalise checks for links targeting .
Browse files Browse the repository at this point in the history
- Strip trailing `?query` and/or `#anchor`
- Strip leading `./` recursively
- Check if what's left is `""` or `"."`

Any link that targets the current page should be left as-is (no-op).
  • Loading branch information
MattSturgeon committed Feb 25, 2025
1 parent 53f9d24 commit 6f8d8f7
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions docs/fix-links/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@ end

function Link(link)
local target = link.target
-- Check for relative links

-- Check for targets on the same page
-- TODO: handle ../
while hasPrefix("./", target) do
-- strip leading ./
target = sub(target, 3)
local bareTarget, _ = target:gsub("[#?].*$", "")
-- strip leading ./
while hasPrefix("./", bareTarget) do
bareTarget = sub(bareTarget, 3)
end
if hasPrefix("#", target) then
-- No-op for anchor targets on the same page
-- No-op for targets on the same page
if bareTarget == "" or bareTarget == "." then
return nil
end

-- Relative links should target the github repo
if not hasPrefix("https://", target) then
link.target = githubUrl .. target
return link
Expand Down

0 comments on commit 6f8d8f7

Please sign in to comment.