mirror of
https://github.com/kovetskiy/mark.git
synced 2025-04-24 05:42:40 +08:00
Fix Regex for links that contain square brackets in the text
This commit is contained in:
parent
f54cceac83
commit
29c4e3020e
@ -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))
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
1
pkg/mark/testdata/links-droph1.html
vendored
1
pkg/mark/testdata/links-droph1.html
vendored
@ -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&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&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>
|
||||||
|
1
pkg/mark/testdata/links-stripnewlines.html
vendored
1
pkg/mark/testdata/links-stripnewlines.html
vendored
@ -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&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&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>
|
||||||
|
1
pkg/mark/testdata/links.html
vendored
1
pkg/mark/testdata/links.html
vendored
@ -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&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&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>
|
||||||
|
1
pkg/mark/testdata/links.md
vendored
1
pkg/mark/testdata/links.md
vendored
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user