From cceb40d0c021981e2aef0f11ab001c955d0dccc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Sun, 5 Oct 2025 15:59:18 +0200 Subject: fix(markdown): missing space after linebreak and link --- markdown/ast_external.go | 20 ++++++++++++++------ markdown/ast_paragraph.go | 4 +--- markdown/ast_test.go | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) (limited to 'markdown') diff --git a/markdown/ast_external.go b/markdown/ast_external.go index e648ad5..ee00eab 100644 --- a/markdown/ast_external.go +++ b/markdown/ast_external.go @@ -9,8 +9,9 @@ import ( var externalLink = regexp.MustCompile(`https?://`) type astLink struct { - content block - href block + content block + href block + addSpace bool } func (a *astLink) Eval(opt *Option) (template.HTML, *ParseError) { @@ -22,7 +23,11 @@ func (a *astLink) Eval(opt *Option) (template.HTML, *ParseError) { if err != nil { return "", err } - return RenderLink(string(content), string(href)), nil + rr := RenderLink(string(content), string(href)) + if a.addSpace { + return " " + rr, nil + } + return rr, nil } func RenderLink(content, href string) template.HTML { @@ -68,22 +73,25 @@ func external(lxs *lexers) (block, *ParseError) { if !lxs.Next() { return astLiteral(tp), nil } - lxs.Before() // because we call Next + lxs.Before() // because we called Next + addSpace := lxs.Before() && lxs.Current().Type == lexerBreak + lxs.Next() // because we called Before var b block var err *ParseError switch tp { case "![": b, err = image(lxs) case "[": - b, err = link(lxs) + b, err = link(lxs, addSpace) default: b = astLiteral(tp) } return b, err } -func link(lxs *lexers) (block, *ParseError) { +func link(lxs *lexers, addSpace bool) (block, *ParseError) { lk := new(astLink) + lk.addSpace = addSpace start := lxs.current content, href, _, ok := parseExternal(lxs, false) if !ok { diff --git a/markdown/ast_paragraph.go b/markdown/ast_paragraph.go index ae1f657..ca5541f 100644 --- a/markdown/ast_paragraph.go +++ b/markdown/ast_paragraph.go @@ -87,9 +87,7 @@ func paragraph(lxs *lexers, oneLine bool) (*astParagraph, *ParseError) { tree.content = append(tree.content, b) } } - if !lxs.Finished() { - lxs.Before() // because we never handle the last item - } + lxs.Before() // because we never handle the last item return tree, nil } diff --git a/markdown/ast_test.go b/markdown/ast_test.go index ff11b1e..45eb49d 100644 --- a/markdown/ast_test.go +++ b/markdown/ast_test.go @@ -41,7 +41,7 @@ var parsed = `
Ceci est ma pfp :3 -
Ma pfp hehe :D Elle est magnifique, n'est-ce pas ?
+
Ma pfp hehe :D Elle est magnifique, n'est-ce pas ?
` -- cgit v1.2.3