From 6d5adbb16a85bcfe0fb3dfbbb7c6921640253c9d Mon Sep 17 00:00:00 2001 From: Viktor Voropaev Date: Wed, 11 Mar 2026 12:50:08 -0700 Subject: [PATCH] Fix output block closing fence when content lacks trailing newline Commands like `printf "hello"` produce output without a trailing newline, causing the closing ``` fence to land on the same line as the output and breaking the resulting markdown. Co-Authored-By: Claude Opus 4.6 --- markdown/writer.go | 6 +++++- markdown/writer_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/markdown/writer.go b/markdown/writer.go index 06d8593..53a7086 100644 --- a/markdown/writer.go +++ b/markdown/writer.go @@ -49,7 +49,11 @@ func writeBlock(w io.Writer, block Block) error { return err case OutputBlock: fence := fenceFor(b.Content) - _, err := fmt.Fprintf(w, "%soutput\n%s%s\n", fence, b.Content, fence) + sep := "" + if b.Content != "" && !strings.HasSuffix(b.Content, "\n") { + sep = "\n" + } + _, err := fmt.Fprintf(w, "%soutput\n%s%s%s\n", fence, b.Content, sep, fence) return err case ImageOutputBlock: _, err := fmt.Fprintf(w, "![%s](%s)\n", b.AltText, b.Filename) diff --git a/markdown/writer_test.go b/markdown/writer_test.go index 391a936..7267f70 100644 --- a/markdown/writer_test.go +++ b/markdown/writer_test.go @@ -128,6 +128,21 @@ func TestWriteOutputNoBackticks(t *testing.T) { } } +func TestWriteOutputNoTrailingNewline(t *testing.T) { + var buf strings.Builder + blocks := []Block{ + OutputBlock{Content: "hello"}, + } + err := Write(&buf, blocks) + if err != nil { + t.Fatal(err) + } + expected := "```output\nhello\n```\n" + if buf.String() != expected { + t.Errorf("expected:\n%q\ngot:\n%q", expected, buf.String()) + } +} + func TestWriteTitleWithDocumentID(t *testing.T) { var buf strings.Builder blocks := []Block{