summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcrystal <71373843+CrystalCommunication@users.noreply.github.com>2023-01-31 02:23:19 -0700
committerGitHub <noreply@github.com>2023-01-31 17:23:19 +0800
commit6dc16c11542c94dd32d5de06a57f7d9a566f9e15 (patch)
treee0cc71003bf4e497b549f20f4d3bb06b54e64cec
parentfd2c250b525b623f5bc1b925fa42076972ec25d1 (diff)
downloadgitea-6dc16c11542c94dd32d5de06a57f7d9a566f9e15.tar.gz
gitea-6dc16c11542c94dd32d5de06a57f7d9a566f9e15.zip
Fix README TOC links (#22577) (#22677)
Backport #22577 Fixes anchored markup links by adding `user-content-` (which is prepended to IDs) Closes https://codeberg.org/Codeberg/Community/issues/894
-rw-r--r--modules/markup/html.go11
1 files changed, 9 insertions, 2 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go
index ae00c3905f..3838708231 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
}