diff options
author | crystal <71373843+CrystalCommunication@users.noreply.github.com> | 2023-01-30 22:21:29 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-31 13:21:29 +0800 |
commit | 03f37d82fe82493edbed81c92c404043a3d779d6 (patch) | |
tree | 52e6fb23f506e425392080a9d76ad10ee5bdea65 /modules | |
parent | 4011821c946e8db032be86266dd9364ccb204118 (diff) | |
download | gitea-03f37d82fe82493edbed81c92c404043a3d779d6.tar.gz gitea-03f37d82fe82493edbed81c92c404043a3d779d6.zip |
Fix README TOC links (#22577)
Fixes anchored markup links by adding `user-content-` (which is
prepended to IDs)
Closes https://codeberg.org/Codeberg/Community/issues/894
Diffstat (limited to 'modules')
-rw-r--r-- | modules/markup/html.go | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go index 73ae768d76..bcb38f99eb 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -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 } |