Skip to content

Commit 3fa4921

Browse files
authored
Merge pull request #2905 from ehuss/fix-indented-code-block
Fix rust fenced code blocks with an indent
2 parents f84b1a1 + ddf02e0 commit 3fa4921

File tree

6 files changed

+35
-1
lines changed

6 files changed

+35
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
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
};
@@ -179,4 +186,8 @@ fn it_partitions_rust_source() {
179186
),
180187
("\n#![allow(foo)]\n\n#![allow(bar)]\n\n", "let x = 1;")
181188
);
189+
assert_eq!(
190+
partition_rust_source(" // Example"),
191+
("", " // Example")
192+
);
182193
}

tests/testsuite/rendering.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,9 @@ Html text was:
223223
fn html_blocks() {
224224
BookTest::from_dir("rendering/html_blocks").check_all_main_files();
225225
}
226+
227+
// Test for a fenced code block that is also indented.
228+
#[test]
229+
fn code_block_fenced_with_indent() {
230+
BookTest::from_dir("rendering/code_blocks_fenced_with_indent").check_all_main_files();
231+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[book]
2+
title = "code_blocks_fenced_with_indent"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1 id="code-blocks-fenced-with-indent"><a class="header" href="#code-blocks-fenced-with-indent">Code blocks fenced with indent</a></h1>
2+
<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.
5+
println!("hello");
6+
<span class="boring">}</span></code></pre>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Summary
2+
3+
- [Code blocks fenced with indent](./code-blocks-fenced-with-indent.md)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Code blocks fenced with indent
2+
3+
```rust
4+
// This has a first line that is indented.
5+
println!("hello");
6+
```

0 commit comments

Comments
 (0)