From 707121c88e2a97d3bfceaba850dcdda79c0f4f37 Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Wed, 10 Dec 2025 12:20:21 +0100 Subject: fix(markdown): bad render of external images in rare cases --- markdown/ast_external_test.go | 3 ++- markdown/ast_paragraph.go | 4 ++++ markdown/lexer.go | 7 +++++-- markdown/lexer_test.go | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) (limited to 'markdown') diff --git a/markdown/ast_external_test.go b/markdown/ast_external_test.go index f3ac976..514cc89 100644 --- a/markdown/ast_external_test.go +++ b/markdown/ast_external_test.go @@ -32,6 +32,7 @@ func TestExternal(t *testing.T) { } lxs = lex(` +Avant la source ![image alt](image src) source 1 source 2 @@ -46,7 +47,7 @@ Hors de la source if err != nil { t.Fatal(err) } - if string(got) != `
image alt
source 1 source 2

Hors de la source

` { + if string(got) != `

Avant la source

image alt
source 1 source 2

Hors de la source

` { t.Errorf("invalid value, got %s", got) } diff --git a/markdown/ast_paragraph.go b/markdown/ast_paragraph.go index 361b2f2..197c7e3 100644 --- a/markdown/ast_paragraph.go +++ b/markdown/ast_paragraph.go @@ -65,6 +65,10 @@ func paragraph(lxs *lexers, oneLine bool) (*astParagraph, *ParseError) { } tree.content = append(tree.content, mod) case lexerExternal: + if n > 0 && lxs.Current().Value == "![" { + lxs.Before() // because we did not use it + return tree, nil + } n = 0 if lxs.Current().Value == "!" { tree.content = append(tree.content, astLiteral(lxs.Current().Value)) diff --git a/markdown/lexer.go b/markdown/lexer.go index d5c0f5f..30587e4 100644 --- a/markdown/lexer.go +++ b/markdown/lexer.go @@ -1,6 +1,9 @@ package markdown -import "fmt" +import ( + "fmt" + "strings" +) type lexerType string @@ -26,7 +29,7 @@ type lexer struct { } func (l lexer) String() string { - return fmt.Sprintf("%s(%s)", l.Type, l.Value) + return fmt.Sprintf("%s(%s)", l.Type, strings.ReplaceAll(l.Value, "\n", `{\n}`)) } type lexers struct { diff --git a/markdown/lexer_test.go b/markdown/lexer_test.go index c670753..e994142 100644 --- a/markdown/lexer_test.go +++ b/markdown/lexer_test.go @@ -12,7 +12,7 @@ func TestLex(t *testing.T) { t.Errorf("invalid lex, got %s", lxs) } lxs = lex("# bonjour les gens\nComment ça va ?") - if lxs.String() != "Lexers[header(#) literal( bonjour les gens) break(\n) literal(Comment ça va ?) ]" { + if lxs.String() != `Lexers[header(#) literal( bonjour les gens) break({\n}) literal(Comment ça va ?) ]` { t.Errorf("invalid lex, got %s", lxs) } lxs = lex("***hey***, what's up?") -- cgit v1.2.3