From 845bfa7cb263d6bdb8049d088c71017ead4cd63c Mon Sep 17 00:00:00 2001 From: Timur Demin Date: Fri, 13 Aug 2021 08:52:52 +0500 Subject: [PATCH] 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. --- internal/gemini/renderer.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/gemini/renderer.go b/internal/gemini/renderer.go index 6c75610..c022e14 100644 --- a/internal/gemini/renderer.go +++ b/internal/gemini/renderer.go @@ -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