Fix code fence closing without trailing newline

When a code block ended without a trailing newline after the closing
\`\`\`, two bugs occurred in flush_incomplete():

1. The closing \`\`\` was included as part of the code block content
   (displayed with syntax highlighting)
2. The same \`\`\` was then emitted again as literal text because
   current_line was not cleared after being pushed to block_buffer

The fix:
- Check if current_line is the closing fence before adding to block_buffer
- Always clear current_line after processing in the CodeBlock case

Added two tests:
- test_code_fence_after_blank_line: code fence with trailing newline
- test_code_fence_no_trailing_newline: code fence without trailing newline
This commit is contained in:
Dhanji R. Prasanna
2026-01-11 19:34:46 +05:30
parent bb25c7881a
commit 9754c4ee66
2 changed files with 56 additions and 1 deletions

View File

@@ -697,7 +697,16 @@ impl StreamingMarkdownFormatter {
// Unclosed code block - emit as-is
if !self.block_buffer.is_empty() || !self.current_line.is_empty() {
if !self.current_line.is_empty() {
self.block_buffer.push(self.current_line.clone());
// Check if current_line is the closing fence (``` without trailing newline)
let trimmed = self.current_line.trim_start();
let leading_spaces = self.current_line.len() - trimmed.len();
if trimmed == "```" && leading_spaces <= 3 {
// This is the closing fence - don't include it in content
// Just clear it and emit the block
} else {
self.block_buffer.push(self.current_line.clone());
}
self.current_line.clear();
}
self.emit_code_block();
}