From fdb0633ea66879ffed8b5f3a9a37cf42e27a2113 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 7 Nov 2026 10:36:39 -1000 Subject: Never give two headings the same anchor A repeat's count was keyed on the bare text, so a heading whose text already ended in that count took the same anchor, as "Twice 1" did after two of "Twice", and "About 1" after "About". A link to either then landed on the first. The count now goes on up past any anchor already handed out, the way GitHub's slugger does. --- custom/extensions/about-render.lua | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'custom/extensions') diff --git a/custom/extensions/about-render.lua b/custom/extensions/about-render.lua index 03c17d8..af4f0df 100644 --- a/custom/extensions/about-render.lua +++ b/custom/extensions/about-render.lua @@ -38,8 +38,9 @@ local max_depth = 24 local filename, tree_base, tree_query, chunks = "", "", "", {} --- How often each heading anchor has been handed out. The ids already on the --- page around the readme start at one, so no heading takes them. +-- The heading anchors handed out so far, each with the count its next +-- repeat takes. The ids already on the page around the readme are in it +-- from the start, so no heading takes them. local id_counts = {} @@ -259,10 +260,16 @@ local function emit_id(nodes) if base == "" then return end - local count = id_counts[base] or 0 - id_counts[base] = count + 1 + -- A count can land on an anchor a heading already took, as "Twice 1" + -- does after two of "Twice", so it goes on up until the anchor is free. + local id = base + while id_counts[id] do + id_counts[base] = id_counts[base] + 1 + id = base .. "-" .. id_counts[base] + end + id_counts[id] = 0 html(" id='") - html_attr(count > 0 and base .. "-" .. count or base) + html_attr(id) html("'") end @@ -972,7 +979,7 @@ function filter_open(name, base, query) tree_base = base or "" tree_query = query or "" chunks = {} - id_counts = { cgit = 1, header = 1, about = 1 } + id_counts = { cgit = 0, header = 0, about = 0 } end function filter_write(str) -- cgit 2.12.1