support inline imgs in markdown

master
Ted Unangst 5 years ago
parent f704326849
commit bd5183c44f

@ -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 @@ 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 "<img x>"
})
// 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, `<a class="mention u-url" href="$2">$1</a>`)
// restore images
s = strings.Replace(s, "&lt;img x&gt;", "<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]

@ -71,3 +71,9 @@ func TestHonklink(t *testing.T) {
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)
}

Loading…
Cancel
Save