Repository: kovetskiy/mark
Pull request #472 (2024-07-25 21:16:06)

Fix Regex for links that contain square brackets in the text
Should fix https://github.com/kovetskiy/mark/issues/461
@provok


Patch:
diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml
index 7773ec2..89add9a 100644
--- a/.github/workflows/goreleaser.yml
+++ b/.github/workflows/goreleaser.yml
@@ -18,9 +18,9 @@ jobs:
       with:
         go-version: "1.21"
     - name: Run GoReleaser
-      uses: goreleaser/goreleaser-action@v5
+      uses: goreleaser/goreleaser-action@v6
       with:
-        version: latest
+        version: "~> 2"
         args: release --clean
       env:
         GOPATH: /home/runner/work/
diff --git a/.goreleaser.yml b/.goreleaser.yml
index 970469b..0157630 100644
--- a/.goreleaser.yml
+++ b/.goreleaser.yml
@@ -1,5 +1,6 @@
 # This is an example goreleaser.yaml file with some sane defaults.
 # Make sure to check the documentation at http://goreleaser.com
+version: 2
 before:
   hooks:
     # You may remove this if you don't use go modules.
diff --git a/pkg/mark/link.go b/pkg/mark/link.go
index c107c64..7ba9502 100644
--- a/pkg/mark/link.go
+++ b/pkg/mark/link.go
@@ -169,7 +169,7 @@ func SubstituteLinks(markdown []byte, links []LinkSubstitution) []byte {
 
 func parseLinks(markdown string) []markdownLink {
 	// Matches links but not inline images
-	re := regexp.MustCompile(`[^\!]\[[^\]]+\]\((([^\)#]+)?#?([^\)]+)?)\)`)
+	re := regexp.MustCompile(`[^\!]\[.+\]\((([^\)#]+)?#?([^\)]+)?)\)`)
 	matches := re.FindAllStringSubmatch(markdown, -1)
 
 	links := make([]markdownLink, len(matches))


Test patch:
diff --git a/pkg/mark/link_test.go b/pkg/mark/link_test.go
index ace2601..7b79078 100644
--- a/pkg/mark/link_test.go
+++ b/pkg/mark/link_test.go
@@ -15,6 +15,7 @@ func TestParseLinks(t *testing.T) {
 	[Image link that should be put as attachment](../path/to/example.png)
 	[relative link without dots](relative-link-without-dots.md)
 	[relative link without dots but with hash](relative-link-without-dots-but-with-hash.md#hash)
+	[example [example]](example.md)
 	`
 
 	links := parseLinks(markdown)
@@ -47,5 +48,6 @@ func TestParseLinks(t *testing.T) {
 	assert.Equal(t, "relative-link-without-dots-but-with-hash.md", links[6].filename)
 	assert.Equal(t, "hash", links[6].hash)
 
-	assert.Equal(t, len(links), 7)
+	assert.Equal(t, "example.md", links[7].full)
+	assert.Equal(t, len(links), 8)
 }
diff --git a/pkg/mark/testdata/links-droph1.html b/pkg/mark/testdata/links-droph1.html
index b002880..9a35df0 100644
--- a/pkg/mark/testdata/links-droph1.html
+++ b/pkg/mark/testdata/links-droph1.html
@@ -7,6 +7,7 @@ Use <ac:link><ri:page ri:content-title="Page With Space"/><ac:plain-text-link-bo
 <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>
 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">
 <hr />
 <ol>
diff --git a/pkg/mark/testdata/links-stripnewlines.html b/pkg/mark/testdata/links-stripnewlines.html
index b002880..9a35df0 100644
--- a/pkg/mark/testdata/links-stripnewlines.html
+++ b/pkg/mark/testdata/links-stripnewlines.html
@@ -7,6 +7,7 @@ Use <ac:link><ri:page ri:content-title="Page With Space"/><ac:plain-text-link-bo
 <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>
 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">
 <hr />
 <ol>
diff --git a/pkg/mark/testdata/links.html b/pkg/mark/testdata/links.html
index b002880..9a35df0 100644
--- a/pkg/mark/testdata/links.html
+++ b/pkg/mark/testdata/links.html
@@ -7,6 +7,7 @@ Use <ac:link><ri:page ri:content-title="Page With Space"/><ac:plain-text-link-bo
 <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>
 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">
 <hr />
 <ol>
diff --git a/pkg/mark/testdata/links.md b/pkg/mark/testdata/links.md
index 855b0af..c6bbe7d 100644
--- a/pkg/mark/testdata/links.md
+++ b/pkg/mark/testdata/links.md
@@ -17,3 +17,4 @@ Use [page link with spaces](<ac:Page With Space>)
 Use footnotes link [^1]  
 [^1]: a footnote link
 
+Use [Link [Text]](foo)