Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Regex for links that contain square brackets in the text #472

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)