From 9bd10072ad8944f0d4675bfb2c4a9858f9b39471 Mon Sep 17 00:00:00 2001 From: Konstantin Kardamanov Date: Fri, 17 Apr 2026 14:23:14 +0200 Subject: [PATCH] md: emit newline before closing fence in codeBlock The codeBlock renderer wrote the closing backtick fence directly after the node literal. When the literal did not end with a newline (as with a manually constructed ast.CodeBlock), the result was malformed markdown like 'val x : Int = 42```' on a single line. Emit a newline before the closing fence when the literal does not already end with one. Also correct the expected string in TestRenderCodeBlock to end with \n\n (matching doubleSpace semantics used by every other test in this file). --- md/md_renderer.go | 3 +++ md/md_renderer_test.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/md/md_renderer.go b/md/md_renderer.go index d5c8455..ebc06d5 100644 --- a/md/md_renderer.go +++ b/md/md_renderer.go @@ -264,6 +264,9 @@ func (r *Renderer) codeBlock(w io.Writer, node *ast.CodeBlock) { } r.outs(w, "\n") r.out(w, text) + if len(text) == 0 || text[len(text)-1] != '\n' { + r.outs(w, "\n") + } r.outs(w, "```\n\n") } diff --git a/md/md_renderer_test.go b/md/md_renderer_test.go index eef736e..3535184 100644 --- a/md/md_renderer_test.go +++ b/md/md_renderer_test.go @@ -73,7 +73,7 @@ func TestRenderCode(t *testing.T) { func TestRenderCodeBlock(t *testing.T) { input := &ast.CodeBlock{Info: []byte(string("scala"))} input.Literal = []byte(string("val x : Int = 42")) - expected := "\n```scala\nval x : Int = 42\n```\n" + expected := "\n```scala\nval x : Int = 42\n```\n\n" testRendering(t, input, expected) }