summaryrefslogtreecommitdiffstats
path: root/modules/markup/html.go
diff options
context:
space:
mode:
authorJohn Olheiser <42128690+jolheiser@users.noreply.github.com>2019-12-23 16:38:50 -0600
committerzeripath <art27@cantab.net>2019-12-23 22:38:50 +0000
commite6ceb6880a8615ad5b8b6a88a0b1bfbd85f4033c (patch)
tree5ffdd08dbf265b35f1405683242a4c8c80ff41eb /modules/markup/html.go
parent071e7c4f0a31ee8b2039b90bb478f13cac62abae (diff)
downloadgitea-e6ceb6880a8615ad5b8b6a88a0b1bfbd85f4033c.tar.gz
gitea-e6ceb6880a8615ad5b8b6a88a0b1bfbd85f4033c.zip
Prefix all user-generated IDs in markup (#9477)
* Prefix all user-generated IDs in markup * Add user-content- to IDs in unit-tests * fixup markdown_test.go * update the hrefs for the wiki test * Add blackfriday extension regex Signed-off-by: jolheiser <john.olheiser@gmail.com>
Diffstat (limited to 'modules/markup/html.go')
-rw-r--r--modules/markup/html.go9
1 files changed, 9 insertions, 0 deletions
diff --git a/modules/markup/html.go b/modules/markup/html.go
index b06b6da7ae..b10da40fc1 100644
--- a/modules/markup/html.go
+++ b/modules/markup/html.go
@@ -58,6 +58,9 @@ var (
emailRegex = regexp.MustCompile("(?:\\s|^|\\(|\\[)([a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9]{2,}(?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+)(?:\\s|$|\\)|\\]|\\.(\\s|$))")
linkRegex, _ = xurls.StrictMatchingScheme("https?://")
+
+ // blackfriday extensions create IDs like fn:user-content-footnote
+ blackfridayExtRegex = regexp.MustCompile(`[^:]*:user-content-`)
)
// CSS class for action keywords (e.g. "closes: #1")
@@ -312,6 +315,12 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
}
func (ctx *postProcessCtx) visitNode(node *html.Node) {
+ // Add user-content- to IDs 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)) {
+ node.Attr[idx].Val = "user-content-" + attr.Val
+ }
+ }
// We ignore code, pre and already generated links.
switch node.Type {
case html.TextNode: