Fix panic on empty blockquotes

While renderer previously assumed there would always be a single
paragraph inside the blockquote, there sometimes can be either more or
none.

See #5.
This commit is contained in:
Timur Demin 2021-08-13 08:52:52 +05:00
parent e5d791165d
commit 845bfa7cb2
No known key found for this signature in database
GPG key ID: 9EDF3F9D9286FA20

View file

@ -86,9 +86,17 @@ func (r Renderer) blockquote(w io.Writer, node *ast.BlockQuote, entering bool) {
// TODO: Renderer.blockquote: needs support for subnode rendering;
// ideally to be merged with paragraph
if entering {
if para, ok := node.Children[0].(*ast.Paragraph); ok {
w.Write(quotePrefix)
r.text(w, para)
if node := node.AsContainer(); node != nil {
for _, child := range node.Children {
w.Write(quotePrefix)
// assume children would be paragraphs
r.text(w, child)
// double linebreak to ensure Gemini clients don't merge
// quotes; gomarkdown assumes separate blockquotes are
// paragraphs of the same blockquote while we don't
w.Write(lineBreak)
w.Write(lineBreak)
}
}
}
}
@ -137,6 +145,7 @@ func (r Renderer) paragraph(w io.Writer, node *ast.Paragraph, entering bool) (no
}
linksOnly := func() bool {
for _, child := range children {
// TODO: simplify
if _, ok := child.(*ast.Link); ok {
continue
}
@ -329,7 +338,6 @@ func (r Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.Walk
switch node := node.(type) {
case *ast.BlockQuote:
r.blockquote(w, node, entering)
noNewLine = false
case *ast.Heading:
r.heading(w, node, entering)
noNewLine = false