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.
This commit is contained in:
Eric Huss
2025-10-27 18:38:27 -07:00
parent 3992bc18f5
commit ddf02e0c0c
2 changed files with 11 additions and 4 deletions

View File

@@ -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")
);
}

View File

@@ -1,6 +1,6 @@
<h1 id="code-blocks-fenced-with-indent"><a class="header" href="#code-blocks-fenced-with-indent">Code blocks fenced with indent</a></h1>
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring"> fn main() {
</span>// This has a first line that is indented.
</span><span class="boring">fn main() {
</span> // This has a first line that is indented.
println!("hello");
<span class="boring">}</span></code></pre>