diff options
Diffstat (limited to 'pkgs/simple')
| -rw-r--r-- | pkgs/simple/cgit-about-filter/cgit-about-filter.lua | 27 |
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) |
