From 3992bc18f512f30c37576b010fb1b33455486a3c Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 27 Oct 2025 18:17:22 -0700 Subject: [PATCH 1/2] Add a test for a fenced code block with an indent --- crates/mdbook-html/src/html/hide_lines.rs | 4 ++++ tests/testsuite/rendering.rs | 6 ++++++ .../rendering/code_blocks_fenced_with_indent/book.toml | 2 ++ .../expected/code-blocks-fenced-with-indent.html | 6 ++++++ .../rendering/code_blocks_fenced_with_indent/src/SUMMARY.md | 3 +++ .../src/code-blocks-fenced-with-indent.md | 6 ++++++ 6 files changed, 27 insertions(+) create mode 100644 tests/testsuite/rendering/code_blocks_fenced_with_indent/book.toml create mode 100644 tests/testsuite/rendering/code_blocks_fenced_with_indent/expected/code-blocks-fenced-with-indent.html create mode 100644 tests/testsuite/rendering/code_blocks_fenced_with_indent/src/SUMMARY.md create mode 100644 tests/testsuite/rendering/code_blocks_fenced_with_indent/src/code-blocks-fenced-with-indent.md diff --git a/crates/mdbook-html/src/html/hide_lines.rs b/crates/mdbook-html/src/html/hide_lines.rs index 6852b6c4c5..c81b44d886 100644 --- a/crates/mdbook-html/src/html/hide_lines.rs +++ b/crates/mdbook-html/src/html/hide_lines.rs @@ -179,4 +179,8 @@ fn it_partitions_rust_source() { ), ("\n#![allow(foo)]\n\n#![allow(bar)]\n\n", "let x = 1;") ); + assert_eq!( + partition_rust_source(" // Example"), + (" ", "// Example") + ); } diff --git a/tests/testsuite/rendering.rs b/tests/testsuite/rendering.rs index d9ceb854f4..1f8590ad33 100644 --- a/tests/testsuite/rendering.rs +++ b/tests/testsuite/rendering.rs @@ -223,3 +223,9 @@ Html text was: fn html_blocks() { BookTest::from_dir("rendering/html_blocks").check_all_main_files(); } + +// Test for a fenced code block that is also indented. +#[test] +fn code_block_fenced_with_indent() { + BookTest::from_dir("rendering/code_blocks_fenced_with_indent").check_all_main_files(); +} diff --git a/tests/testsuite/rendering/code_blocks_fenced_with_indent/book.toml b/tests/testsuite/rendering/code_blocks_fenced_with_indent/book.toml new file mode 100644 index 0000000000..7ed7ab96c1 --- /dev/null +++ b/tests/testsuite/rendering/code_blocks_fenced_with_indent/book.toml @@ -0,0 +1,2 @@ +[book] +title = "code_blocks_fenced_with_indent" 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 new file mode 100644 index 0000000000..e2bdfd2b00 --- /dev/null +++ b/tests/testsuite/rendering/code_blocks_fenced_with_indent/expected/code-blocks-fenced-with-indent.html @@ -0,0 +1,6 @@ +

Code blocks fenced with indent

+
#![allow(unused)]
+    fn main() {
+// This has a first line that is indented.
+    println!("hello");
+}
\ No newline at end of file diff --git a/tests/testsuite/rendering/code_blocks_fenced_with_indent/src/SUMMARY.md b/tests/testsuite/rendering/code_blocks_fenced_with_indent/src/SUMMARY.md new file mode 100644 index 0000000000..46b1673b02 --- /dev/null +++ b/tests/testsuite/rendering/code_blocks_fenced_with_indent/src/SUMMARY.md @@ -0,0 +1,3 @@ +# Summary + +- [Code blocks fenced with indent](./code-blocks-fenced-with-indent.md) diff --git a/tests/testsuite/rendering/code_blocks_fenced_with_indent/src/code-blocks-fenced-with-indent.md b/tests/testsuite/rendering/code_blocks_fenced_with_indent/src/code-blocks-fenced-with-indent.md new file mode 100644 index 0000000000..e8722979d0 --- /dev/null +++ b/tests/testsuite/rendering/code_blocks_fenced_with_indent/src/code-blocks-fenced-with-indent.md @@ -0,0 +1,6 @@ +# Code blocks fenced with indent + +```rust + // This has a first line that is indented. + println!("hello"); +``` From ddf02e0c0ce3740d27db3f1d89e8ffe19ee7b7bd Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 27 Oct 2025 18:38:27 -0700 Subject: [PATCH 2/2] 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 c81b44d886..8dbe7b12c5 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 e2bdfd2b00..b4ed1ef491 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