From c80a0b7181167eee4d9dd2a441947f909304c596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Tue, 22 Oct 2024 10:50:06 +0200 Subject: [PATCH] Feat: Use custom heading anchors Confluence Anchors are case-sensitive. --- markdown/markdown.go | 4 ++- parser/confluenceids.go | 56 ++++++++++++++++++++++++++++++ testdata/header-droph1.html | 1 + testdata/header.html | 1 + testdata/header.md | 2 ++ testdata/quotes-droph1.html | 22 ++++++------ testdata/quotes-stripnewlines.html | 24 ++++++------- testdata/quotes.html | 24 ++++++------- 8 files changed, 98 insertions(+), 36 deletions(-) create mode 100644 parser/confluenceids.go diff --git a/markdown/markdown.go b/markdown/markdown.go index 26f8297d..50c45bad 100644 --- a/markdown/markdown.go +++ b/markdown/markdown.go @@ -91,8 +91,10 @@ func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, mermaidPr html.WithXHTML(), )) + ctx := parser.NewContext(parser.WithIDs(&cparser.ConfluenceIDs{Values: map[string]bool{}})) + var buf bytes.Buffer - err := converter.Convert(markdown, &buf) + err := converter.Convert(markdown, &buf, parser.WithContext(ctx)) if err != nil { panic(err) diff --git a/parser/confluenceids.go b/parser/confluenceids.go new file mode 100644 index 00000000..1f7acd22 --- /dev/null +++ b/parser/confluenceids.go @@ -0,0 +1,56 @@ +package parser + +import ( + "fmt" + + "github.com/yuin/goldmark/ast" + "github.com/yuin/goldmark/util" +) + +type ConfluenceIDs struct { + Values map[string]bool +} + + +// https://github.com/yuin/goldmark/blob/d9c03f07f08c2d36f23afe52dda865f05320ac86/parser/parser.go#L75 +func (s *ConfluenceIDs) Generate(value []byte, kind ast.NodeKind) []byte { + value = util.TrimLeftSpace(value) + value = util.TrimRightSpace(value) + result := []byte{} + for i := 0; i < len(value); { + v := value[i] + l := util.UTF8Len(v) + i += int(l) + if l != 1 { + continue + } + if util.IsAlphaNumeric(v) || v == '/' || v == '_' || v == '.' { + result = append(result, v) + } else if util.IsSpace(v) || v == '-' { + result = append(result, '-') + } + } + if len(result) == 0 { + if kind == ast.KindHeading { + result = []byte("heading") + } else { + result = []byte("id") + } + } + if _, ok := s.Values[util.BytesToReadOnlyString(result)]; !ok { + s.Values[util.BytesToReadOnlyString(result)] = true + return result + } + for i := 1; ; i++ { + newResult := fmt.Sprintf("%s-%d", result, i) + if _, ok := s.Values[newResult]; !ok { + s.Values[newResult] = true + return []byte(newResult) + } + + } +} + +func (s *ConfluenceIDs) Put(value []byte) { + s.Values[util.BytesToReadOnlyString(value)] = true +} diff --git a/testdata/header-droph1.html b/testdata/header-droph1.html index c63a1409..09e33224 100644 --- a/testdata/header-droph1.html +++ b/testdata/header-droph1.html @@ -4,3 +4,4 @@

d

e

f

g

+

This/is some_Heading.yml

diff --git a/testdata/header.html b/testdata/header.html index 566c4ae1..44242336 100644 --- a/testdata/header.html +++ b/testdata/header.html @@ -5,3 +5,4 @@

d

e

f

g

+

This/is some_Heading.yml

diff --git a/testdata/header.md b/testdata/header.md index 81a9738f..4dbdc53e 100644 --- a/testdata/header.md +++ b/testdata/header.md @@ -8,3 +8,5 @@ f = g - + +# This/is some_Heading.yml diff --git a/testdata/quotes-droph1.html b/testdata/quotes-droph1.html index 4fdba685..3d7d2de4 100644 --- a/testdata/quotes-droph1.html +++ b/testdata/quotes-droph1.html @@ -1,4 +1,4 @@ -

First Heading

+

First Heading

true

NOTES:

    @@ -11,7 +11,7 @@

    First Heading

    Warn (Should not be picked as blockquote type)

    -

    Second Heading

    +

    Second Heading

    true

    Warn

      @@ -23,12 +23,12 @@

      Second Heading

    • Regular list that runs long
    -

    Third Heading

    +

    Third Heading

    true

    Test

    -

    Fourth Heading - Warn should not get picked as block quote

    +

    Fourth Heading - Warn should not get picked as block quote

    true

    TIP:

      @@ -41,12 +41,12 @@

      Fourth Headi

      Warn (Should not be picked as blockquote type)

      -

      Simple Blockquote

      +

      Simple Blockquote

      This paragraph is a simple blockquote

      -

      GH Alerts Heading

      -

      Note Type Alert Heading

      +

      GH Alerts Heading

      +

      Note Type Alert Heading

      true

      [!NOTE]

        @@ -54,7 +54,7 @@

        Note Type Alert Heading

      • Note bullet 2
      -

      Tip Type Alert Heading

      +

      Tip Type Alert Heading

      true

      [!TIP]

        @@ -62,7 +62,7 @@

        Tip Type Alert Heading

      • Tip bullet 2
      -

      Warning Type Alert Heading

      +

      Warning Type Alert Heading

      true

      [!WARNING]

        @@ -70,7 +70,7 @@

        Warning Type Alert Heading

      • Warning bullet 2
      -

      Important/Caution Type Alert Heading

      +

      Important/Caution Type Alert Heading

      true

      [!IMPORTANT]

        @@ -85,7 +85,7 @@

        Important/Caution Type Alert Headin
      • Important bullet 2
      -

      Should not be picked up and converted into blockquote macro

      +

      Should not be picked up and converted into blockquote macro

      [[!NOTE]

      diff --git a/testdata/quotes-stripnewlines.html b/testdata/quotes-stripnewlines.html index e07a0474..28e101ac 100644 --- a/testdata/quotes-stripnewlines.html +++ b/testdata/quotes-stripnewlines.html @@ -1,5 +1,5 @@ -

      Main Heading

      -

      First Heading

      +

      Main Heading

      +

      First Heading

      true

      NOTES:

        @@ -11,7 +11,7 @@

        First Heading

        Warn (Should not be picked as blockquote type)

        -

        Second Heading

        +

        Second Heading

        true

        Warn

          @@ -22,12 +22,12 @@

          Second Heading

          • Regular list that runs long
          -

          Third Heading

          +

          Third Heading

          true

          Test

          -

          Fourth Heading - Warn should not get picked as block quote

          +

          Fourth Heading - Warn should not get picked as block quote

          true

          TIP:

            @@ -39,12 +39,12 @@

            Fourth Headi

            Warn (Should not be picked as blockquote type)

            -

            Simple Blockquote

            +

            Simple Blockquote

            This paragraph is a simple blockquote

            -

            GH Alerts Heading

            -

            Note Type Alert Heading

            +

            GH Alerts Heading

            +

            Note Type Alert Heading

            true

            [!NOTE]

              @@ -52,7 +52,7 @@

              Note Type Alert Heading

            • Note bullet 2
            -

            Tip Type Alert Heading

            +

            Tip Type Alert Heading

            true

            [!TIP]

              @@ -60,7 +60,7 @@

              Tip Type Alert Heading

            • Tip bullet 2
            -

            Warning Type Alert Heading

            +

            Warning Type Alert Heading

            true

            [!WARNING]

              @@ -68,7 +68,7 @@

              Warning Type Alert Heading

            • Warning bullet 2
            -

            Important/Caution Type Alert Heading

            +

            Important/Caution Type Alert Heading

            true

            [!IMPORTANT]

              @@ -83,7 +83,7 @@

              Important/Caution Type Alert Headin
            • Important bullet 2
            -

            Should not be picked up and converted into blockquote macro

            +

            Should not be picked up and converted into blockquote macro

            [[!NOTE]

            diff --git a/testdata/quotes.html b/testdata/quotes.html index 15a23176..28ab52f0 100644 --- a/testdata/quotes.html +++ b/testdata/quotes.html @@ -1,5 +1,5 @@ -

            Main Heading

            -

            First Heading

            +

            Main Heading

            +

            First Heading

            true

            NOTES:

              @@ -12,7 +12,7 @@

              First Heading

              Warn (Should not be picked as blockquote type)

              -

              Second Heading

              +

              Second Heading

              true

              Warn

                @@ -24,12 +24,12 @@

                Second Heading

              • Regular list that runs long
              -

              Third Heading

              +

              Third Heading

              true

              Test

              -

              Fourth Heading - Warn should not get picked as block quote

              +

              Fourth Heading - Warn should not get picked as block quote

              true

              TIP:

                @@ -42,12 +42,12 @@

                Fourth Headi

                Warn (Should not be picked as blockquote type)

                -

                Simple Blockquote

                +

                Simple Blockquote

                This paragraph is a simple blockquote

                -

                GH Alerts Heading

                -

                Note Type Alert Heading

                +

                GH Alerts Heading

                +

                Note Type Alert Heading

                true

                [!NOTE]

                  @@ -55,7 +55,7 @@

                  Note Type Alert Heading

                • Note bullet 2
                -

                Tip Type Alert Heading

                +

                Tip Type Alert Heading

                true

                [!TIP]

                  @@ -63,7 +63,7 @@

                  Tip Type Alert Heading

                • Tip bullet 2
                -

                Warning Type Alert Heading

                +

                Warning Type Alert Heading

                true

                [!WARNING]

                  @@ -71,7 +71,7 @@

                  Warning Type Alert Heading

                • Warning bullet 2
                -

                Important/Caution Type Alert Heading

                +

                Important/Caution Type Alert Heading

                true

                [!IMPORTANT]

                  @@ -86,7 +86,7 @@

                  Important/Caution Type Alert Headin
                • Important bullet 2
                -

                Should not be picked up and converted into blockquote macro

                +

                Should not be picked up and converted into blockquote macro

                [[!NOTE]