Fix Regex for links that contain square brackets in the text

This commit is contained in:
Manuel Rüger 2024-07-25 23:03:21 +02:00
parent f54cceac83
commit 29c4e3020e
6 changed files with 8 additions and 2 deletions

View File

@ -169,7 +169,7 @@ func SubstituteLinks(markdown []byte, links []LinkSubstitution) []byte {
func parseLinks(markdown string) []markdownLink { func parseLinks(markdown string) []markdownLink {
// Matches links but not inline images // Matches links but not inline images
re := regexp.MustCompile(`[^\!]\[[^\]]+\]\((([^\)#]+)?#?([^\)]+)?)\)`) re := regexp.MustCompile(`[^\!]\[.+\]\((([^\)#]+)?#?([^\)]+)?)\)`)
matches := re.FindAllStringSubmatch(markdown, -1) matches := re.FindAllStringSubmatch(markdown, -1)
links := make([]markdownLink, len(matches)) links := make([]markdownLink, len(matches))

View File

@ -15,6 +15,7 @@ func TestParseLinks(t *testing.T) {
[Image link that should be put as attachment](../path/to/example.png) [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](relative-link-without-dots.md)
[relative link without dots but with hash](relative-link-without-dots-but-with-hash.md#hash) [relative link without dots but with hash](relative-link-without-dots-but-with-hash.md#hash)
[example [example]](example.md)
` `
links := parseLinks(markdown) 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, "relative-link-without-dots-but-with-hash.md", links[6].filename)
assert.Equal(t, "hash", links[6].hash) 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)
} }

View File

@ -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 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: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 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"> <div class="footnotes" role="doc-endnotes">
<hr /> <hr />
<ol> <ol>

View File

@ -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 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: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 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"> <div class="footnotes" role="doc-endnotes">
<hr /> <hr />
<ol> <ol>

View File

@ -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 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: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 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"> <div class="footnotes" role="doc-endnotes">
<hr /> <hr />
<ol> <ol>

View File

@ -17,3 +17,4 @@ Use [page link with spaces](<ac:Page With Space>)
Use footnotes link [^1] Use footnotes link [^1]
[^1]: a footnote link [^1]: a footnote link
Use [Link [Text]](foo)