Skip to content

Commit ed34d7c

Browse files
Fix README TOC links (go-gitea#22577)
Fixes anchored markup links by adding `user-content-` (which is prepended to IDs) Closes https://codeberg.org/Codeberg/Community/issues/894
1 parent fd2c250 commit ed34d7c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

modules/markup/html.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,19 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
358358
}
359359

360360
func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node) {
361-
// Add user-content- to IDs if they don't already have them
361+
// Add user-content- to IDs and "#" links if they don't already have them
362362
for idx, attr := range node.Attr {
363-
if attr.Key == "id" && !(strings.HasPrefix(attr.Val, "user-content-") || blackfridayExtRegex.MatchString(attr.Val)) {
363+
val := strings.TrimPrefix(attr.Val, "#")
364+
notHasPrefix := !(strings.HasPrefix(val, "user-content-") || blackfridayExtRegex.MatchString(val))
365+
366+
if attr.Key == "id" && notHasPrefix {
364367
node.Attr[idx].Val = "user-content-" + attr.Val
365368
}
366369

370+
if attr.Key == "href" && strings.HasPrefix(attr.Val, "#") && notHasPrefix {
371+
node.Attr[idx].Val = "#user-content-" + val
372+
}
373+
367374
if attr.Key == "class" && attr.Val == "emoji" {
368375
textProcs = nil
369376
}

0 commit comments

Comments
 (0)