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 @@
NOTES:
Warn (Should not be picked as blockquote type)
Warn
Test
TIP:
Warn (Should not be picked as blockquote type)
-This paragraph is a simple blockquote
[!NOTE]
[!TIP]
[!WARNING]
[!IMPORTANT]
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 @@ -[[!NOTE]
NOTES:
Warn (Should not be picked as blockquote type)
Warn
Test
TIP:
Warn (Should not be picked as blockquote type)
-This paragraph is a simple blockquote
[!NOTE]
[!TIP]
[!WARNING]
[!IMPORTANT]
diff --git a/testdata/quotes.html b/testdata/quotes.html index 15a2317..28ab52f 100644 --- a/testdata/quotes.html +++ b/testdata/quotes.html @@ -1,5 +1,5 @@ -[[!NOTE]
NOTES:
Warn (Should not be picked as blockquote type)
Warn
Test
TIP:
Warn (Should not be picked as blockquote type)
-This paragraph is a simple blockquote
[!NOTE]
[!TIP]
[!WARNING]
[!IMPORTANT]
[[!NOTE]