From c7053918139f2ea39158a86c293a64ce2970a5d8 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Mon, 23 Dec 2019 13:57:37 -0600 Subject: [PATCH 1/6] Prefix all user-generated IDs in markup Signed-off-by: jolheiser --- modules/markup/html.go | 6 ++++++ web_src/js/index.js | 15 ++------------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index b06b6da7ae707..4dcddecf8d277 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -312,6 +312,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-") { + 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/web_src/js/index.js b/web_src/js/index.js index 8600acc95c7af..36af54e8c89ec 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(``); }); }); From 4d07a1009146bac24f81ca31ed963199204df777 Mon Sep 17 00:00:00 2001 From: John Olheiser <42128690+jolheiser@users.noreply.github.com> Date: Mon, 23 Dec 2019 14:30:39 -0600 Subject: [PATCH 2/6] encode Co-Authored-By: zeripath --- web_src/js/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/js/index.js b/web_src/js/index.js index 36af54e8c89ec..395a01f05939d 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -2475,7 +2475,7 @@ $(document).ready(() => { $(this).find('h1, h2, h3, h4, h5, h6').each(function () { let node = $(this); node = node.wrap('
'); - node.append(``); + node.append(``); }); }); From 5a353450b51188507aa802184582ab1f704344f1 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Mon, 23 Dec 2019 14:52:14 -0600 Subject: [PATCH 3/6] Add user-content- to IDs in unit-tests Signed-off-by: jolheiser --- modules/markup/markdown/markdown_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index e80173c6cf322..560ab0c38d64e 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 {
  • Plot var helper
  • `, - `

    What is Wine Staging?

    + `

    What is Wine Staging?

    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 { images/2.png -

    More tests

    +

    More tests

    (from https://www.markdownguide.org/extended-syntax/)

    -

    Definition list

    +

    Definition list

    First Term
    @@ -163,18 +163,18 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
    This is another definition of the second term.
    -

    Footnotes

    +

    Footnotes

    -

    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


      -
    1. This is the first footnote.
    2. +
    3. This is the first footnote.
    4. -
    5. Here is one with multiple paragraphs and code.

      +
    6. Here is one with multiple paragraphs and code.

      Indent paragraphs to include them in the footnote.

      From ebc5446e116ea4b217cf3174d92f52332a0b86bf Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 23 Dec 2019 21:06:02 +0000 Subject: [PATCH 4/6] fixup markdown_test.go --- routers/api/v1/misc/markdown_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/misc/markdown_test.go b/routers/api/v1/misc/markdown_test.go index 47e99d2f06ac7..716b9e59f71c5 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 - `

      What is Wine Staging?

      + `

      What is Wine Staging?

      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.

      From a128a4dc1c6c4f533cd0496ed812f163ca949c28 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 23 Dec 2019 21:23:41 +0000 Subject: [PATCH 5/6] update the hrefs for the wiki test --- modules/markup/markdown/markdown_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 560ab0c38d64e..32f79874eba8e 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -165,7 +165,7 @@ func testAnswers(baseURLContent, baseURLImages string) []string {

      Footnotes

      -

      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

      From 08dc4c5ef250f387fed4a2ae367e23391af7556b Mon Sep 17 00:00:00 2001 From: jolheiser Date: Mon, 23 Dec 2019 16:10:22 -0600 Subject: [PATCH 6/6] Add blackfriday extension regex Signed-off-by: jolheiser --- modules/markup/html.go | 5 ++++- modules/markup/markdown/markdown.go | 4 +++- modules/markup/markdown/markdown_test.go | 6 +++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index 4dcddecf8d277..b10da40fc1878 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") @@ -314,7 +317,7 @@ 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-") { + if attr.Key == "id" && !(strings.HasPrefix(attr.Val, "user-content-") || blackfridayExtRegex.MatchString(attr.Val)) { node.Attr[idx].Val = "user-content-" + attr.Val } } diff --git a/modules/markup/markdown/markdown.go b/modules/markup/markdown/markdown.go index fc704243e2d7f..f1e44a8fbc144 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 32f79874eba8e..e3156a657bd74 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -165,16 +165,16 @@ func testAnswers(baseURLContent, baseURLImages string) []string {

      Footnotes

      -

      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


        -
      1. This is the first footnote.
      2. +
      3. This is the first footnote.
      4. -
      5. Here is one with multiple paragraphs and code.

        +
      6. Here is one with multiple paragraphs and code.

        Indent paragraphs to include them in the footnote.