Repository: kovetskiy/mark
Pull request #473 (2024-07-25 21:41:13)

Fix links containing an underscore
@Skeeve this should fix https://github.com/kovetskiy/mark/issues/394


Patch:
diff --git a/pkg/mark/renderer/link.go b/pkg/mark/renderer/link.go
index 9c77b36..838ac59 100644
--- a/pkg/mark/renderer/link.go
+++ b/pkg/mark/renderer/link.go
@@ -25,20 +25,21 @@ func (r *ConfluenceLinkRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegi
 
 // renderLink renders links specifically for confluence
 func (r *ConfluenceLinkRenderer) renderLink(writer util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
-	if string(node.(*ast.Link).Destination[0:3]) == "ac:" {
+	n := node.(*ast.Link)
+	if string(n.Destination[0:3]) == "ac:" {
 		if entering {
 			_, err := writer.Write([]byte("<ac:link><ri:page ri:content-title=\""))
 			if err != nil {
 				return ast.WalkStop, err
 			}
 
-			if len(node.(*ast.Link).Destination) < 4 {
-				_, err := writer.Write(node.FirstChild().Text(source))
+			if len(n.Destination) < 4 {
+				_, err := writer.Write(node.Text(source))
 				if err != nil {
 					return ast.WalkStop, err
 				}
 			} else {
-				_, err := writer.Write(node.(*ast.Link).Destination[3:])
+				_, err := writer.Write(n.Destination[3:])
 				if err != nil {
 					return ast.WalkStop, err
 				}
@@ -49,7 +50,7 @@ func (r *ConfluenceLinkRenderer) renderLink(writer util.BufWriter, source []byte
 				return ast.WalkStop, err
 			}
 
-			_, err = writer.Write(node.FirstChild().Text(source))
+			_, err = writer.Write(node.Text(source))
 			if err != nil {
 				return ast.WalkStop, err
 			}


Test patch:
diff --git a/pkg/mark/testdata/links-droph1.html b/pkg/mark/testdata/links-droph1.html
index 9a35df0..6160135 100644
--- a/pkg/mark/testdata/links-droph1.html
+++ b/pkg/mark/testdata/links-droph1.html
@@ -3,9 +3,12 @@ Use <ac:rich-text-body>aaa</ac:rich-text-body>
 Use <ac:link><ri:page ri:content-title="Page"/><ac:plain-text-link-body><![CDATA[page link]]></ac:plain-text-link-body></ac:link>
 Use <ac:link><ri:page ri:content-title="AnotherPage"/><ac:plain-text-link-body><![CDATA[AnotherPage]]></ac:plain-text-link-body></ac:link>
 Use <ac:link><ri:page ri:content-title="Another Page"/><ac:plain-text-link-body><![CDATA[Another Page]]></ac:plain-text-link-body></ac:link>
+Use <ac:link><ri:page ri:content-title="test_link"/><ac:plain-text-link-body><![CDATA[Another Page]]></ac:plain-text-link-body></ac:link>
 Use <ac:link><ri:page ri:content-title="Page With Space"/><ac:plain-text-link-body><![CDATA[page link with spaces]]></ac:plain-text-link-body></ac:link>
 <ac:image ac:alt="My Image"><ri:attachment ri:filename="test.png"/></ac:image>
 <ac:image ac:alt="My External Image"><ri:url ri:value="http://confluence.atlassian.com/images/logo/confluence_48_trans.png?key1=value1&amp;key2=value2"/></ac:image>
+<ac:link><ri:page ri:content-title="test_link"/><ac:plain-text-link-body><![CDATA[My test_link]]></ac:plain-text-link-body></ac:link>
+<ac:link><ri:page ri:content-title="test_link_link"/><ac:plain-text-link-body><![CDATA[Another [Link]]]></ac:plain-text-link-body></ac:link>
 Use footnotes link <sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>
 Use <a href="foo">Link [Text]</a>
 <div class="footnotes" role="doc-endnotes">
diff --git a/pkg/mark/testdata/links-stripnewlines.html b/pkg/mark/testdata/links-stripnewlines.html
index 9a35df0..6160135 100644
--- a/pkg/mark/testdata/links-stripnewlines.html
+++ b/pkg/mark/testdata/links-stripnewlines.html
@@ -3,9 +3,12 @@ Use <ac:rich-text-body>aaa</ac:rich-text-body>
 Use <ac:link><ri:page ri:content-title="Page"/><ac:plain-text-link-body><![CDATA[page link]]></ac:plain-text-link-body></ac:link>
 Use <ac:link><ri:page ri:content-title="AnotherPage"/><ac:plain-text-link-body><![CDATA[AnotherPage]]></ac:plain-text-link-body></ac:link>
 Use <ac:link><ri:page ri:content-title="Another Page"/><ac:plain-text-link-body><![CDATA[Another Page]]></ac:plain-text-link-body></ac:link>
+Use <ac:link><ri:page ri:content-title="test_link"/><ac:plain-text-link-body><![CDATA[Another Page]]></ac:plain-text-link-body></ac:link>
 Use <ac:link><ri:page ri:content-title="Page With Space"/><ac:plain-text-link-body><![CDATA[page link with spaces]]></ac:plain-text-link-body></ac:link>
 <ac:image ac:alt="My Image"><ri:attachment ri:filename="test.png"/></ac:image>
 <ac:image ac:alt="My External Image"><ri:url ri:value="http://confluence.atlassian.com/images/logo/confluence_48_trans.png?key1=value1&amp;key2=value2"/></ac:image>
+<ac:link><ri:page ri:content-title="test_link"/><ac:plain-text-link-body><![CDATA[My test_link]]></ac:plain-text-link-body></ac:link>
+<ac:link><ri:page ri:content-title="test_link_link"/><ac:plain-text-link-body><![CDATA[Another [Link]]]></ac:plain-text-link-body></ac:link>
 Use footnotes link <sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>
 Use <a href="foo">Link [Text]</a>
 <div class="footnotes" role="doc-endnotes">
diff --git a/pkg/mark/testdata/links.html b/pkg/mark/testdata/links.html
index 9a35df0..6160135 100644
--- a/pkg/mark/testdata/links.html
+++ b/pkg/mark/testdata/links.html
@@ -3,9 +3,12 @@ Use <ac:rich-text-body>aaa</ac:rich-text-body>
 Use <ac:link><ri:page ri:content-title="Page"/><ac:plain-text-link-body><![CDATA[page link]]></ac:plain-text-link-body></ac:link>
 Use <ac:link><ri:page ri:content-title="AnotherPage"/><ac:plain-text-link-body><![CDATA[AnotherPage]]></ac:plain-text-link-body></ac:link>
 Use <ac:link><ri:page ri:content-title="Another Page"/><ac:plain-text-link-body><![CDATA[Another Page]]></ac:plain-text-link-body></ac:link>
+Use <ac:link><ri:page ri:content-title="test_link"/><ac:plain-text-link-body><![CDATA[Another Page]]></ac:plain-text-link-body></ac:link>
 Use <ac:link><ri:page ri:content-title="Page With Space"/><ac:plain-text-link-body><![CDATA[page link with spaces]]></ac:plain-text-link-body></ac:link>
 <ac:image ac:alt="My Image"><ri:attachment ri:filename="test.png"/></ac:image>
 <ac:image ac:alt="My External Image"><ri:url ri:value="http://confluence.atlassian.com/images/logo/confluence_48_trans.png?key1=value1&amp;key2=value2"/></ac:image>
+<ac:link><ri:page ri:content-title="test_link"/><ac:plain-text-link-body><![CDATA[My test_link]]></ac:plain-text-link-body></ac:link>
+<ac:link><ri:page ri:content-title="test_link_link"/><ac:plain-text-link-body><![CDATA[Another [Link]]]></ac:plain-text-link-body></ac:link>
 Use footnotes link <sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>
 Use <a href="foo">Link [Text]</a>
 <div class="footnotes" role="doc-endnotes">
diff --git a/pkg/mark/testdata/links.md b/pkg/mark/testdata/links.md
index c6bbe7d..073db52 100644
--- a/pkg/mark/testdata/links.md
+++ b/pkg/mark/testdata/links.md
@@ -8,12 +8,18 @@ Use [AnotherPage](ac:)
 
 Use [Another Page](ac:)
 
+Use [Another Page](ac:test_link)
+
 Use [page link with spaces](<ac:Page With Space>)
 
 ![My Image](test.png)
 
 ![My External Image](http://confluence.atlassian.com/images/logo/confluence_48_trans.png?key1=value1&key2=value2)
 
+[My test_link](ac:test_link)
+
+[Another [Link]](ac:test_link_link)
+
 Use footnotes link [^1]  
 [^1]: a footnote link