Skip to content

Commit eec05ee

Browse files
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](rustfoundation/interop-initiative#53)
2 parents 3b8c7ec + ef3a89c commit eec05ee

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

compiler/rustc_errors/src/markdown/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fn parse_codeblock(buf: &[u8]) -> Parsed<'_> {
220220
let mut found = None;
221221
for idx in (0..working.len()).filter(|idx| working[*idx..].starts_with(&end_pat)) {
222222
let (eol_txt, rest) = parse_to_newline(&working[(idx + end_pat.len())..]);
223-
if !eol_txt.iter().any(u8::is_ascii_whitespace) {
223+
if eol_txt.iter().all(u8::is_ascii_whitespace) {
224224
found = Some((&working[..idx], rest));
225225
break;
226226
}

compiler/rustc_errors/src/markdown/tests/parse.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,16 @@ fn test_snake_case() {
364364
let res = entrypoint(SNAKE_CASE);
365365
assert_eq!(res, expected);
366366
}
367+
368+
#[test]
369+
fn test_codeblock_trailing_whitespace() {
370+
let buf = "```rust\ncode\n``` \nrest";
371+
let (t, r) = parse_codeblock(buf.as_bytes());
372+
assert_eq!(t, MdTree::CodeBlock { txt: "code", lang: Some("rust") });
373+
assert_eq!(r, b"\nrest");
374+
375+
let buf = "```rust\ncode\n```abc\nrest";
376+
let (t, r) = parse_codeblock(buf.as_bytes());
377+
assert_eq!(t, MdTree::CodeBlock { txt: "code\n```abc\nrest", lang: Some("rust") });
378+
assert_eq!(r, b"");
379+
}

0 commit comments

Comments
 (0)