diff options
| author | tv <tv@krebsco.de> | 2026-11-18 11:56:32 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-18 13:08:24 +0100 |
| commit | c1862d78f704f9c7f7cec19c8f0d03d632c11115 (patch) | |
| tree | ac62aaab192970acf39697c717d64dc49f3dc585 /pkgs/simple/cgit-about-filter | |
| parent | 3d57242810feab10c11cd1df66a7c2958869497f (diff) | |
cgit-about-filter: link headings instead of a link symbol
The heading itself links to itself, underlined on hover. Clicking the
heading the #fragment points to removes the fragment again, and with it
the highlight.
Diffstat (limited to 'pkgs/simple/cgit-about-filter')
| -rw-r--r-- | pkgs/simple/cgit-about-filter/cgit-about-filter.lua | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua index f317c54..7b36226 100644 --- a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua +++ b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua @@ -227,15 +227,27 @@ local function slug(content) return (text:lower():gsub("[^%w%s_%-\128-\255]", ""):gsub("%s", "-")) end --- A chain link, after each heading, linking to it. -local anchor = [[<a class="anchor" href="#%s" aria-label="Link to this section"><svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><g fill="none" stroke="currentColor" stroke-width="1.5" transform="rotate(-45 8 8)"><rect x="1" y="5.5" width="8" height="5" rx="2.5"/><rect x="7" y="5.5" width="8" height="5" rx="2.5"/></g></svg></a>]] - -local anchor_style = [[ +-- Headings link to themselves, underlined on hover by cgit.css. Clicking +-- the heading that is the #fragment removes the fragment again. Setting +-- location.hash is what makes :target forget the heading; +-- history.replaceState alone doesn't. +local heading_links = [[ <style> -div#summary a.anchor { color: #888; margin-left: 0.3em; opacity: 0; } -div#summary :is(h1, h2, h3, h4, h5, h6):is(:hover, :focus-within) a.anchor { opacity: 1; } -@media (hover: none) { div#summary a.anchor { opacity: 1; } } +div#summary a.anchor { color: inherit; } </style> +<script> + document.addEventListener("click", event => { + const link = event.target.closest("a.anchor"); + if (link && link.hash === location.hash && event.button === 0 && + !(event.ctrlKey || event.metaKey || event.shiftKey || event.altKey)) { + event.preventDefault(); + const x = scrollX, y = scrollY; + location.hash = ""; + scrollTo(x, y); + history.replaceState(null, "", location.pathname + location.search); + } + }); +</script> ]] -- Give headings ids, so links to #fragments work. A repeated id gets -1, @@ -253,7 +265,10 @@ local function heading_ids(output) else seen[id] = 0 end - return string.format('<h%s id="%s">%s' .. anchor .. '</h%s>', level, id, content, id, level) + if not content:find("<a ", 1, true) then + content = string.format('<a class="anchor" href="#%s">%s</a>', id, content) + end + return string.format('<h%s id="%s">%s</h%s>', level, id, content, level) end)) end @@ -421,7 +436,7 @@ local function close() output = heading_ids(output) if output:find('<h%d id="') then emit(target_style) - emit(anchor_style) + emit(heading_links) end if output:find("<table>", 1, true) then emit(table_style) |
