From 6f8d8f7aee84f377f52c8bb58385015f9168a666 Mon Sep 17 00:00:00 2001 From: Matt Sturgeon Date: Mon, 24 Feb 2025 13:39:39 +0000 Subject: [PATCH] docs/fix-links: generalise checks for links targeting `.` - 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). --- docs/fix-links/filter.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/fix-links/filter.lua b/docs/fix-links/filter.lua index 3a320187ba..68f8d832e7 100644 --- a/docs/fix-links/filter.lua +++ b/docs/fix-links/filter.lua @@ -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