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: diff --git a/modules/markup/markdown/markdown.go b/modules/markup/markdown/markdown.go index fc704243e2..f1e44a8fbc 100644 --- a/modules/markup/markdown/markdown.go +++ b/modules/markup/markdown/markdown.go @@ -146,7 +146,9 @@ const ( func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte { renderer := &Renderer{ Renderer: blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{ - Flags: blackfridayHTMLFlags, + Flags: blackfridayHTMLFlags, + FootnoteAnchorPrefix: "user-content-", + HeadingIDPrefix: "user-content-", }), URLPrefix: urlPrefix, IsWiki: wikiMarkdown, diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index e80173c6cf..e3156a657b 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -116,11 +116,11 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
Wine Staging on website wine-staging.com.
-Here are some links to the most important topics. You can find the full list of pages at the sidebar.
@@ -149,11 +149,11 @@ func testAnswers(baseURLContent, baseURLImages string) []string { -(from https://www.markdownguide.org/extended-syntax/)
-Here is a simple footnote,1 and here is a longer one.2
+Here is a simple footnote,1 and here is a longer one.2
Here is one with multiple paragraphs and code.
+Here is one with multiple paragraphs and code.
Indent paragraphs to include them in the footnote.
diff --git a/routers/api/v1/misc/markdown_test.go b/routers/api/v1/misc/markdown_test.go index 47e99d2f06..716b9e59f7 100644 --- a/routers/api/v1/misc/markdown_test.go +++ b/routers/api/v1/misc/markdown_test.go @@ -87,11 +87,11 @@ Here are some links to the most important topics. You can find the full list of [[images/icon-bug.png]] `, // rendered - `Wine Staging on website wine-staging.com.
-Here are some links to the most important topics. You can find the full list of pages at the sidebar.
diff --git a/web_src/js/index.js b/web_src/js/index.js index 8600acc95c..395a01f059 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -2472,21 +2472,10 @@ $(document).ready(() => { // Set anchor. $('.markdown').each(function () { - const headers = {}; $(this).find('h1, h2, h3, h4, h5, h6').each(function () { let node = $(this); - const val = encodeURIComponent(node.text().toLowerCase().replace(/[^\u00C0-\u1FFF\u2C00-\uD7FF\w\- ]/g, '').replace(/[ ]/g, '-')); - let name = val; - if (headers[val] > 0) { - name = `${val}-${headers[val]}`; - } - if (headers[val] === undefined) { - headers[val] = 1; - } else { - headers[val] += 1; - } - node = node.wrap(``); - node.append(``); + node = node.wrap(''); + node.append(``); }); });