From 876626098bba649cb09bdaf59e2926dc5343b99d 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 26f8297..50c45ba 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 0000000..1f7acd2
--- /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 c63a140..09e3322 100644
--- a/testdata/header-droph1.html
+++ b/testdata/header-droph1.html
@@ -4,3 +4,4 @@
e
f
g
+This/is some_Heading.yml
diff --git a/testdata/header.html b/testdata/header.html
index 566c4ae..4424233 100644
--- a/testdata/header.html
+++ b/testdata/header.html
@@ -5,3 +5,4 @@
e
f
g
+This/is some_Heading.yml
diff --git a/testdata/header.md b/testdata/header.md
index 81a9738..4dbdc53 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 4fdba68..3d7d2de 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 @@ b
Warn (Should not be picked as blockquote type)
-Second Heading
+Second Heading
true
Warn
@@ -23,12 +23,12 @@ b
- 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 @@ b
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 @@ b
- Note bullet 2
-Tip Type Alert Heading
+Tip Type Alert Heading
true
[!TIP]
@@ -62,7 +62,7 @@ b
- Tip bullet 2
-Warning Type Alert Heading
+Warning Type Alert Heading
true
[!WARNING]
@@ -70,7 +70,7 @@ b
- Warning bullet 2
-Important/Caution Type Alert Heading
+Important/Caution Type Alert Heading
true
[!IMPORTANT]
@@ -85,7 +85,7 @@ b
- 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 e07a047..28e101a 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 @@
Warn (Should not be picked as blockquote type)
-Second Heading
+Second Heading
true
Warn