Skip to content

Commit e22886a

Browse files
committed
Keep indent when using include in code block
1 parent 68a5c09 commit e22886a

File tree

4 files changed

+726
-324
lines changed

4 files changed

+726
-324
lines changed

src/preprocess/links.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,26 @@ where
8989
let mut replaced = String::new();
9090

9191
for link in find_links(s) {
92-
replaced.push_str(&s[previous_end_index..link.start_index]);
92+
let before_link = &s[previous_end_index..link.start_index];
93+
let offset = if let Some(i) = before_link.rfind('\n') {
94+
&before_link[i + 1..]
95+
} else {
96+
""
97+
};
98+
replaced.push_str(before_link);
9399

94100
match link.render_with_path(&path, chapter_title) {
95101
Ok(new_content) => {
96102
if depth < MAX_LINK_NESTED_DEPTH {
97103
if let Some(rel_path) = link.link_type.relative_path(path) {
98-
replaced.push_str(&replace_all(
99-
&new_content,
100-
rel_path,
101-
source,
102-
depth + 1,
103-
chapter_title,
104-
));
104+
let v =
105+
replace_all(&new_content, rel_path, source, depth + 1, chapter_title);
106+
let lines = v.split('\n').into_iter().collect::<Vec<&str>>();
107+
// no need to add offset for the first line
108+
replaced.push_str(lines[0]);
109+
for line in lines.iter().skip(1) {
110+
replaced.push_str(&format!("\n{}{}", &offset, &line));
111+
}
105112
} else {
106113
replaced.push_str(&new_content);
107114
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
instance Eq (NonEmpty a) where
2+
eq (NonEmpty x xs) (NonEmpty y ys) = x == y && xs == ys

tests/dummy_book/src/first/nested.md

+14
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,17 @@ assert!($TEST_STATUS);
2929
```rust
3030
{{#rustdoc_include partially-included-test-with-anchors.rs:rustdoc-include-anchor}}
3131
```
32+
33+
## Keep Indent
34+
35+
1. The following code remains indent:
36+
```haskell
37+
{{#include ./eqNonEmpty.hs}}
38+
```
39+
we ...
40+
41+
1. The following code also remains indent:
42+
```haskell
43+
{{#include ./eqNonEmpty.hs}}
44+
```
45+
we ...

0 commit comments

Comments
 (0)