Rollup merge of #155040 - yalagadapavankumar:fix-whitespace, r=JohnTitor

Fix code block whitespace handling in Markdown

### Fix Markdown code block closing whitespace handling

Previously, the parser incorrectly accepted closing backticks followed by extra text and rejected lines where only spaces appeared after closing backticks. This did not match expected Markdown behavior.

Now, the parser correctly ends a code block only when line after the code ends with spaces or nothing. Lines where extra text appears after the closing backticks are treated as part of the code block.

Includes tests for both correct and incorrect cases.

### Related issue

This PR addresses the Outreachy issue: [Markdown whitespace bug in Rust Compiler](https://github.com/rustfoundation/interop-initiative/issues/53)
This commit is contained in:
Jonathan Brouwer
2026-04-10 15:33:15 +02:00
committed by GitHub
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"");
}