Fix code block whitespace handling

This commit is contained in:
Pavan
2026-04-09 18:44:06 +05:30
parent 1948ee19e9
commit 1e593417b3
2 changed files with 14 additions and 1 deletions
+1 -1
View File
@@ -220,7 +220,7 @@ fn parse_codeblock(buf: &[u8]) -> Parsed<'_> {
let mut found = None;
for idx in (0..working.len()).filter(|idx| working[*idx..].starts_with(&end_pat)) {
let (eol_txt, rest) = parse_to_newline(&working[(idx + end_pat.len())..]);
if !eol_txt.iter().any(u8::is_ascii_whitespace) {
if eol_txt.iter().all(u8::is_ascii_whitespace) {
found = Some((&working[..idx], rest));
break;
}
@@ -364,3 +364,16 @@ fn test_snake_case() {
let res = entrypoint(SNAKE_CASE);
assert_eq!(res, expected);
}
#[test]
fn test_codeblock_trailing_whitespace() {
let buf = "```rust\ncode\n``` \nrest";
let (t, r) = parse_codeblock(buf.as_bytes());
assert_eq!(t, MdTree::CodeBlock { txt: "code", lang: Some("rust") });
assert_eq!(r, b"\nrest");
let buf = "```rust\ncode\n```abc\nrest";
let (t, r) = parse_codeblock(buf.as_bytes());
assert_eq!(t, MdTree::CodeBlock { txt: "code\n```abc\nrest", lang: Some("rust") });
assert_eq!(r, b"");
}