a few more autolink edge cases
Ted Unangst tedu@tedunangst.com
Mon, 21 Oct 2019 23:43:15 -0400
2 files changed,
29 insertions(+),
3 deletions(-)
M
markitzero.go
→
markitzero.go
@@ -29,7 +29,7 @@ var re_italicer = regexp.MustCompile(`(^|\W)\*([\w\s,.!?':_-]+)\*($|\W)`)
var re_bigcoder = regexp.MustCompile("```(.*)\n?((?s:.*?))\n?```\n?") var re_coder = regexp.MustCompile("`([^`]*)`") var re_quoter = regexp.MustCompile(`(?m:^> (.*)\n?)`) -var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)]`) +var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)!]`) var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`) var lighter = synlight.New(synlight.Options{Format: synlight.HTML})@@ -50,8 +50,21 @@ lilcodes = append(lilcodes, code)
return "`x`" }) - s = html.EscapeString(s) - s = strings.Replace(s, "'", "'", -1) // dammit go + // fewer side effects than html.EscapeString + buf := make([]byte, 0, len(s)) + for _, c := range []byte(s) { + switch c { + case '&': + buf = append(buf, []byte("&")...) + case '<': + buf = append(buf, []byte("<")...) + case '>': + buf = append(buf, []byte(">")...) + default: + buf = append(buf, c) + } + } + s = string(buf) // mark it zero s = re_bolder.ReplaceAllString(s, "$1<b>$2</b>$3")
M
markitzero_test.go
→
markitzero_test.go
@@ -58,3 +58,16 @@ input := "I watched [Hackers](https://en.wikipedia.org/wiki/Hackers_(film))"
output := `I watched <a class="mention u-url" href="https://en.wikipedia.org/wiki/Hackers_(film)">Hackers</a>` doonezerotest(t, input, output) } + +func TestQuotedlink(t *testing.T) { + input := `quoted "https://example.com/link" here` + output := `quoted "<a class="mention u-url" href="https://example.com/link">https://example.com/link</a>" here` + doonezerotest(t, input, output) +} + +func TestHonklink(t *testing.T) { + input := `https://en.wikipedia.org/wiki/Honk!` + output := `<a class="mention u-url" href="https://en.wikipedia.org/wiki/Honk!">https://en.wikipedia.org/wiki/Honk!</a>` + doonezerotest(t, input, output) +} +