support inline imgs in markdown
Ted Unangst tedu@tedunangst.com
Tue, 22 Oct 2019 00:16:28 -0400
2 files changed,
20 insertions(+),
1 deletions(-)
M
markitzero.go
→
markitzero.go
@@ -31,6 +31,7 @@ var re_coder = regexp.MustCompile("`([^`]*)`")
var re_quoter = regexp.MustCompile(`(?m:^> (.*)\n?)`) var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)!]`) var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`) +var re_imgfix = regexp.MustCompile(`<img ([^>]*)>`) var lighter = synlight.New(synlight.Options{Format: synlight.HTML})@@ -40,7 +41,7 @@ s = strings.TrimSpace(s)
s = strings.Replace(s, "\r", "", -1) // save away the code blocks so we don't mess them up further - var bigcodes, lilcodes []string + var bigcodes, lilcodes, images []string s = re_bigcoder.ReplaceAllStringFunc(s, func(code string) string { bigcodes = append(bigcodes, code) return "``````"@@ -48,6 +49,10 @@ })
s = re_coder.ReplaceAllStringFunc(s, func(code string) string { lilcodes = append(lilcodes, code) return "`x`" + }) + s = re_imgfix.ReplaceAllStringFunc(s, func(img string) string { + images = append(images, img) + return "<img x>" }) // fewer side effects than html.EscapeString@@ -72,6 +77,14 @@ s = re_italicer.ReplaceAllString(s, "$1<i>$2</i>$3")
s = re_quoter.ReplaceAllString(s, "<blockquote>$1</blockquote><p>") s = re_link.ReplaceAllStringFunc(s, linkreplacer) s = re_zerolink.ReplaceAllString(s, `<a class="mention u-url" href="$2">$1</a>`) + + // restore images + s = strings.Replace(s, "<img x>", "<img x>", -1) + s = re_imgfix.ReplaceAllStringFunc(s, func(string) string { + img := images[0] + images = images[1:] + return img + }) // now restore the code blocks s = re_coder.ReplaceAllStringFunc(s, func(string) string {
M
markitzero_test.go
→
markitzero_test.go
@@ -71,3 +71,9 @@ output := `<a class="mention u-url" href="https://en.wikipedia.org/wiki/Honk!">https://en.wikipedia.org/wiki/Honk!</a>`
doonezerotest(t, input, output) } +func TestImagelink(t *testing.T) { + input := `an image <img alt="caption" src="https://example.com/wherever"> and linked [<img src="there">](example.com)` + output := `an image <img alt="caption" src="https://example.com/wherever"> and linked <a class="mention u-url" href="example.com"><img src="there"></a>` + doonezerotest(t, input, output) +} +