From ddf02e0c0ce3740d27db3f1d89e8ffe19ee7b7bd Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 27 Oct 2025 18:38:27 -0700 Subject: [PATCH] Fix rust fenced code blocks with an indent This fixes a bug in the Rust code block partitioning that was incorrectly removing the whitespace from the beginning of a code block. --- crates/mdbook-html/src/html/hide_lines.rs | 11 +++++++++-- .../expected/code-blocks-fenced-with-indent.html | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/mdbook-html/src/html/hide_lines.rs b/crates/mdbook-html/src/html/hide_lines.rs index c81b44d8..8dbe7b12 100644 --- a/crates/mdbook-html/src/html/hide_lines.rs +++ b/crates/mdbook-html/src/html/hide_lines.rs @@ -141,7 +141,14 @@ fn partition_rust_source(s: &str) -> (&str, &str) { let split_idx = match HEADER_RE.captures(s) { Some(caps) => { let attributes = &caps[1]; - attributes.len() + if attributes.trim().is_empty() { + // Don't include pure whitespace as an attribute. The + // whitespace in the regex is intended to handle multiple + // attributes *separated* by potential whitespace. + 0 + } else { + attributes.len() + } } None => 0, }; @@ -181,6 +188,6 @@ fn it_partitions_rust_source() { ); assert_eq!( partition_rust_source(" // Example"), - (" ", "// Example") + ("", " // Example") ); } diff --git a/tests/testsuite/rendering/code_blocks_fenced_with_indent/expected/code-blocks-fenced-with-indent.html b/tests/testsuite/rendering/code_blocks_fenced_with_indent/expected/code-blocks-fenced-with-indent.html index e2bdfd2b..b4ed1ef4 100644 --- a/tests/testsuite/rendering/code_blocks_fenced_with_indent/expected/code-blocks-fenced-with-indent.html +++ b/tests/testsuite/rendering/code_blocks_fenced_with_indent/expected/code-blocks-fenced-with-indent.html @@ -1,6 +1,6 @@

Code blocks fenced with indent

#![allow(unused)]
-    fn main() {
-// This has a first line that is indented.
+fn main() {
+    // This has a first line that is indented.
     println!("hello");
 }
\ No newline at end of file