Diff options
| -rw-r--r-- | custom/extensions/about-render.lua | 19 | |
| -rw-r--r-- | tests/extensions/test-about-render.lua | 10 |
2 files changed, 23 insertions, 6 deletions
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) diff --git a/tests/extensions/test-about-render.lua b/tests/extensions/test-about-render.lua index b9284c4..6f54805 100644 --- a/tests/extensions/test-about-render.lua +++ b/tests/extensions/test-about-render.lua @@ -86,6 +86,16 @@ h.contains("a repeated anchor is counted", out, "<h1 id='twice'>Twice</h1><h1 id out = render("README.md", "# About") h.contains("no heading takes an id of the page", out, "<h1 id='about-1'>") +out = render("README.md", "# Cgit\n# Header") +h.contains("nor any other id of the page", out, "<h1 id='cgit-1'>Cgit</h1><h1 id='header-1'>Header</h1>") + +out = render("README.md", "# Twice\n# Twice\n# Twice 1") +h.contains("a count never gives an anchor a heading already took", out, + "<h1 id='twice-1'>Twice</h1><h1 id='twice-1-1'>Twice 1</h1>") + +out = render("README.md", "# About\n# About 1") +h.contains("and the same holds past an id of the page", out, "<h1 id='about-1'>About</h1><h1 id='about-1-1'>About 1</h1>") + out = render("README.md", "# ???") h.contains("a heading of punctuation gets no anchor", out, "<h1>???</h1>") |
