Disabled formatting in URLs

This commit is contained in:
2024-04-02 21:30:50 +02:00
parent 891e3938fa
commit 9e6767fb12
3 changed files with 38 additions and 22 deletions

View File

@ -51,9 +51,9 @@ coverage: 100.0% of statements
## Benchmarks ## Benchmarks
``` ```
BenchmarkSmall-12 6202569 190.9 ns/op 32 B/op 1 allocs/op BenchmarkSmall-12 5933677 190.8 ns/op 32 B/op 1 allocs/op
BenchmarkMedium-12 1000000 1080 ns/op 512 B/op 1 allocs/op BenchmarkMedium-12 971929 1085 ns/op 512 B/op 1 allocs/op
BenchmarkLarge-12 271048 4115 ns/op 2560 B/op 2 allocs/op BenchmarkLarge-12 277028 4075 ns/op 2560 B/op 2 allocs/op
``` ```
## License ## License

View File

@ -253,14 +253,13 @@ func (r *renderer) writeText(markdown string) {
searchStart = 0 searchStart = 0
linkTextStart = -1 linkTextStart = -1
linkTextEnd = -1 linkTextEnd = -1
urlStart = -1
codeStart = -1 codeStart = -1
emStart = -1 emStart = -1
strongStart = -1 strongStart = -1
strikeStart = -1 strikeStart = -1
parentheses = 0
) )
begin:
for { for {
i := strings.IndexAny(markdown[searchStart:], "[]()`*_~") i := strings.IndexAny(markdown[searchStart:], "[]()`*_~")
@ -282,18 +281,31 @@ func (r *renderer) writeText(markdown string) {
linkTextEnd = i linkTextEnd = i
case '(': case '(':
if parentheses == 0 { if linkTextStart == -1 || linkTextEnd == -1 {
urlStart = i continue
} }
parentheses++ level := 1
for {
pos := strings.IndexAny(markdown[searchStart:], "()")
if pos == -1 {
goto begin
}
switch markdown[searchStart+pos] {
case '(':
level++
case ')': case ')':
parentheses-- level--
if level == 0 {
urlEnd := searchStart + pos
searchStart = urlEnd + 1
if parentheses == 0 && linkTextStart >= 0 && linkTextEnd >= 0 && urlStart >= 0 {
linkText := markdown[linkTextStart+1 : linkTextEnd] linkText := markdown[linkTextStart+1 : linkTextEnd]
linkURL := markdown[urlStart+1 : i] linkURL := markdown[i+1 : urlEnd]
r.WriteString("<a href=\"") r.WriteString("<a href=\"")
r.WriteString(sanitizeURL(linkURL)) r.WriteString(sanitizeURL(linkURL))
@ -303,9 +315,12 @@ func (r *renderer) writeText(markdown string) {
linkTextStart = -1 linkTextStart = -1
linkTextEnd = -1 linkTextEnd = -1
urlStart = -1 tokenStart = urlEnd + 1
goto begin
}
}
tokenStart = i + 1 searchStart += pos + 1
} }
case '`': case '`':

View File

@ -50,6 +50,7 @@ func TestLink(t *testing.T) {
assert.Equal(t, markdown.Render("[text]https://example.com/)"), "<p>[text]https://example.com/)</p>") assert.Equal(t, markdown.Render("[text]https://example.com/)"), "<p>[text]https://example.com/)</p>")
assert.Equal(t, markdown.Render("[text(https://example.com/)"), "<p>[text(https://example.com/)</p>") assert.Equal(t, markdown.Render("[text(https://example.com/)"), "<p>[text(https://example.com/)</p>")
assert.Equal(t, markdown.Render("text](https://example.com/)"), "<p>text](https://example.com/)</p>") assert.Equal(t, markdown.Render("text](https://example.com/)"), "<p>text](https://example.com/)</p>")
assert.Equal(t, markdown.Render("[text](https://example.com/_test_)"), "<p><a href=\"https://example.com/_test_\">text</a></p>")
assert.Equal(t, markdown.Render("Prefix [text](https://example.com/) suffix."), "<p>Prefix <a href=\"https://example.com/\">text</a> suffix.</p>") assert.Equal(t, markdown.Render("Prefix [text](https://example.com/) suffix."), "<p>Prefix <a href=\"https://example.com/\">text</a> suffix.</p>")
} }