Fix headers with inline formatting breaking onto new line

When streaming markdown headers containing inline tags (backticks, bold,
italic), the closing delimiter triggered early emission via
emit_formatted_inline(). Since format_header() appends a newline, any
text after the closing tag ended up on a separate line.

Added an in_header guard to handle_delimiter() so headers wait for the
actual newline to emit as a complete line. Added 4 char-by-char streaming
tests covering the bug pattern.
This commit is contained in:
Dhanji R. Prasanna
2026-02-17 12:42:17 +11:00
parent ca1cf5998a
commit e30ddb8cbc
2 changed files with 123 additions and 1 deletions

View File

@@ -385,7 +385,11 @@ impl StreamingMarkdownFormatter {
let could_be_hr = self.current_line.chars().all(|c| c == '*' || c == '-' || c == '_')
&& self.current_line.len() >= 2; // At least ** or -- or __
if self.delimiter_stack.is_empty() && !in_potential_link && !could_be_hr {
// Don't emit yet if we're inside a header - headers must be emitted
// as a complete line at newline, otherwise trailing text after the
// closing delimiter ends up on a new line (format_header adds \n)
let in_header = self.current_line.starts_with('#');
if self.delimiter_stack.is_empty() && !in_potential_link && !could_be_hr && !in_header {
self.emit_formatted_inline();
}
} else {