Disabled formatting in URLs
This commit is contained in:
parent
891e3938fa
commit
9e6767fb12
@ -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
|
||||||
|
53
Render.go
53
Render.go
@ -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,30 +281,46 @@ 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
|
||||||
|
|
||||||
case ')':
|
for {
|
||||||
parentheses--
|
pos := strings.IndexAny(markdown[searchStart:], "()")
|
||||||
|
|
||||||
if parentheses == 0 && linkTextStart >= 0 && linkTextEnd >= 0 && urlStart >= 0 {
|
if pos == -1 {
|
||||||
linkText := markdown[linkTextStart+1 : linkTextEnd]
|
goto begin
|
||||||
linkURL := markdown[urlStart+1 : i]
|
}
|
||||||
|
|
||||||
r.WriteString("<a href=\"")
|
switch markdown[searchStart+pos] {
|
||||||
r.WriteString(sanitizeURL(linkURL))
|
case '(':
|
||||||
r.WriteString("\">")
|
level++
|
||||||
r.WriteString(html.EscapeString(linkText))
|
case ')':
|
||||||
r.WriteString("</a>")
|
level--
|
||||||
|
|
||||||
linkTextStart = -1
|
if level == 0 {
|
||||||
linkTextEnd = -1
|
urlEnd := searchStart + pos
|
||||||
urlStart = -1
|
searchStart = urlEnd + 1
|
||||||
|
|
||||||
tokenStart = i + 1
|
linkText := markdown[linkTextStart+1 : linkTextEnd]
|
||||||
|
linkURL := markdown[i+1 : urlEnd]
|
||||||
|
|
||||||
|
r.WriteString("<a href=\"")
|
||||||
|
r.WriteString(sanitizeURL(linkURL))
|
||||||
|
r.WriteString("\">")
|
||||||
|
r.WriteString(html.EscapeString(linkText))
|
||||||
|
r.WriteString("</a>")
|
||||||
|
|
||||||
|
linkTextStart = -1
|
||||||
|
linkTextEnd = -1
|
||||||
|
tokenStart = urlEnd + 1
|
||||||
|
goto begin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
searchStart += pos + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
case '`':
|
case '`':
|
||||||
|
@ -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>")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user