Skip to content

Commit ddf02e0

Browse files
committed
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.
1 parent 3992bc1 commit ddf02e0

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

crates/mdbook-html/src/html/hide_lines.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,14 @@ fn partition_rust_source(s: &str) -> (&str, &str) {
141141
let split_idx = match HEADER_RE.captures(s) {
142142
Some(caps) => {
143143
let attributes = &caps[1];
144-
attributes.len()
144+
if attributes.trim().is_empty() {
145+
// Don't include pure whitespace as an attribute. The
146+
// whitespace in the regex is intended to handle multiple
147+
// attributes *separated* by potential whitespace.
148+
0
149+
} else {
150+
attributes.len()
151+
}
145152
}
146153
None => 0,
147154
};
@@ -181,6 +188,6 @@ fn it_partitions_rust_source() {
181188
);
182189
assert_eq!(
183190
partition_rust_source(" // Example"),
184-
(" ", "// Example")
191+
("", " // Example")
185192
);
186193
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 id="code-blocks-fenced-with-indent"><a class="header" href="#code-blocks-fenced-with-indent">Code blocks fenced with indent</a></h1>
22
<pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
3-
</span><span class="boring"> fn main() {
4-
</span>// This has a first line that is indented.
3+
</span><span class="boring">fn main() {
4+
</span> // This has a first line that is indented.
55
println!("hello");
66
<span class="boring">}</span></code></pre>

0 commit comments

Comments
 (0)