Skip to content

Commit

Permalink
Fix Regex for links that contain square brackets in the text
Browse files Browse the repository at this point in the history
  • Loading branch information
mrueg committed Jul 25, 2024
1 parent f54ccea commit 29c4e30
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/mark/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion pkg/mark/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions pkg/mark/testdata/links-droph1.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<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>
Expand Down
1 change: 1 addition & 0 deletions pkg/mark/testdata/links-stripnewlines.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<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>
Expand Down
1 change: 1 addition & 0 deletions pkg/mark/testdata/links.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<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>
Expand Down
1 change: 1 addition & 0 deletions pkg/mark/testdata/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 29c4e30

Please sign in to comment.