From 502cab0a2f1196e9755e488b481a363be950d9f6 Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 18 Nov 2026 11:46:32 +0000 Subject: cgit-about-filter: give headings ids md2html renders headings without ids, so links to #fragments go nowhere. Make ids the way GitHub does. Ids cgit uses on the about page get a suffix like repeated ones, so a heading "Summary" gets "summary-1" rather than clashing with
. --- .../simple/cgit-about-filter/cgit-about-filter.lua | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'pkgs') diff --git a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua index 2d2d92a..5333d40 100644 --- a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua +++ b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua @@ -220,6 +220,32 @@ local function layout_tables(output) end)) end +-- A heading's id, the way GitHub makes it: the text in lower case, without +-- punctuation, and with a hyphen for each space. +local function slug(content) + local text = content:gsub("<[^>]*>", ""):gsub("&[^;]*;", "") + return (text:lower():gsub("[^%w%s_%-\128-\255]", ""):gsub("%s", "-")) +end + +-- Give headings ids, so links to #fragments work. A repeated id gets -1, +-- -2, etc., and so do ids cgit uses on the about page itself. +local function heading_ids(output) + local seen = { cgit = 0, header = 0, summary = 0 } + return (output:gsub("(.-)", function(level, content) + local id = slug(content) + if id == "" then + return nil + end + if seen[id] then + seen[id] = seen[id] + 1 + id = id .. "-" .. seen[id] + else + seen[id] = 0 + end + return string.format('%s', level, id, content, level) + end)) +end + -- Render ```mermaid code blocks in the browser. local mermaid_script = [[