]> source.dussan.org Git - gitea.git/commitdiff
Fix README TOC links (#22577)
authorcrystal <71373843+CrystalCommunication@users.noreply.github.com>
Tue, 31 Jan 2023 05:21:29 +0000 (22:21 -0700)
committerGitHub <noreply@github.com>
Tue, 31 Jan 2023 05:21:29 +0000 (13:21 +0800)
Fixes anchored markup links by adding `user-content-` (which is
prepended to IDs)

Closes https://codeberg.org/Codeberg/Community/issues/894

modules/markup/html.go

index 73ae768d76c0affaa01442ea4135c5dae4c613d4..bcb38f99eb4a8bf5a361e0fdd7f9872aa0f94914 100644 (file)
@@ -358,12 +358,19 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
 }
 
 func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node) {
-       // Add user-content- to IDs if they don't already have them
+       // Add user-content- to IDs and "#" links if they don't already have them
        for idx, attr := range node.Attr {
-               if attr.Key == "id" && !(strings.HasPrefix(attr.Val, "user-content-") || blackfridayExtRegex.MatchString(attr.Val)) {
+               val := strings.TrimPrefix(attr.Val, "#")
+               notHasPrefix := !(strings.HasPrefix(val, "user-content-") || blackfridayExtRegex.MatchString(val))
+
+               if attr.Key == "id" && notHasPrefix {
                        node.Attr[idx].Val = "user-content-" + attr.Val
                }
 
+               if attr.Key == "href" && strings.HasPrefix(attr.Val, "#") && notHasPrefix {
+                       node.Attr[idx].Val = "#user-content-" + val
+               }
+
                if attr.Key == "class" && attr.Val == "emoji" {
                        textProcs = nil
                }