Fix two markdown formatting bugs

Bug 1: Inline code after list bullets not detected
- After emitting a list bullet, at_line_start was not set to false
- This caused the next backtick to be treated as a potential code fence
- Fixed by setting at_line_start = false after emitting bullet

Bug 2: Code block closing on indented backticks
- Code blocks containing indented ``` (4+ spaces) were closing prematurely
- The .trim() check was too permissive
- Fixed by only allowing closing fence with <= 3 spaces indent (CommonMark spec)

Added tests for both edge cases.
This commit is contained in:
Dhanji R. Prasanna
2026-01-08 20:50:26 +11:00
parent 19a804e0be
commit a72d5a650a
2 changed files with 190 additions and 1 deletions

View File

@@ -179,6 +179,7 @@ impl StreamingMarkdownFormatter {
self.pending_output.push_back(indent);
}
self.pending_output.push_back("".to_string());
self.at_line_start = false;
return;
}
@@ -424,7 +425,12 @@ impl StreamingMarkdownFormatter {
fn process_in_code_block(&mut self, ch: char) {
if ch == '\n' {
// Check if this line closes the code block
if self.current_line.trim() == "```" {
// Only close if the fence is at the start of the line with at most 3 spaces
// of indentation (per CommonMark spec). This prevents content like " ```"
// (4+ spaces, which is code indentation) from closing the block.
let trimmed = self.current_line.trim_start();
let leading_spaces = self.current_line.len() - trimmed.len();
if trimmed == "```" && leading_spaces <= 3 {
// Emit the entire code block
self.emit_code_block();
self.block_state = BlockState::None;