From bd5183c44ff70e8c20d3b0178d05535b68bed406 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Tue, 22 Oct 2019 00:16:28 -0400 Subject: [PATCH] support inline imgs in markdown --- markitzero.go | 15 ++++++++++++++- markitzero_test.go | 6 ++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/markitzero.go b/markitzero.go index b918cdb..d83efbd 100644 --- a/markitzero.go +++ b/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(`]*)>`) var lighter = synlight.New(synlight.Options{Format: synlight.HTML}) @@ -40,7 +41,7 @@ func markitzero(s string) string { 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 "``````" @@ -49,6 +50,10 @@ func markitzero(s string) string { lilcodes = append(lilcodes, code) return "`x`" }) + s = re_imgfix.ReplaceAllStringFunc(s, func(img string) string { + images = append(images, img) + return "" + }) // fewer side effects than html.EscapeString buf := make([]byte, 0, len(s)) @@ -73,6 +78,14 @@ func markitzero(s string) string { s = re_link.ReplaceAllStringFunc(s, linkreplacer) s = re_zerolink.ReplaceAllString(s, `$1`) + // restore images + s = strings.Replace(s, "<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 { code := lilcodes[0] diff --git a/markitzero_test.go b/markitzero_test.go index 5cdd3a6..db0e912 100644 --- a/markitzero_test.go +++ b/markitzero_test.go @@ -71,3 +71,9 @@ func TestHonklink(t *testing.T) { doonezerotest(t, input, output) } +func TestImagelink(t *testing.T) { + input := `an image caption and linked [](example.com)` + output := `an image caption and linked ` + doonezerotest(t, input, output) +} +