summaryrefslogtreecommitdiffstats
path: root/pkgs/simple
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-18 11:46:32 +0000
committertv <tv@krebsco.de>2026-11-18 12:49:06 +0100
commit502cab0a2f1196e9755e488b481a363be950d9f6 (patch)
tree516a19a6472f107c204c95f99e8e32f20330af23 /pkgs/simple
parentc0e8ccf00d0cf09ae7f77f057193a5c4cf5d2ef2 (diff)
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 <div id='summary'>.
Diffstat (limited to 'pkgs/simple')
-rw-r--r--pkgs/simple/cgit-about-filter/cgit-about-filter.lua27
1 files changed, 27 insertions, 0 deletions
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("<h([1-6])>(.-)</h%1>", 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('<h%s id="%s">%s</h%s>', level, id, content, level)
+ end))
+end
+
-- Render ```mermaid code blocks in the browser.
local mermaid_script = [[
<script type="module">
@@ -374,6 +400,7 @@ local function close()
local output = md2html(processed)
if output then
+ output = heading_ids(output)
if output:find("<table>", 1, true) then
emit(table_style)
output = layout_tables(output)