Skip to content

Commit a1880e1

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

File tree

4 files changed

+723
-324
lines changed

4 files changed

+723
-324
lines changed

src/preprocess/links.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,23 @@ 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 lines = before_link.split('\n').into_iter().collect::<Vec<&str>>();
94+
let offset = lines.last().unwrap_or(&"");
95+
replaced.push_str(before_link);
9396

9497
match link.render_with_path(&path, chapter_title) {
9598
Ok(new_content) => {
9699
if depth < MAX_LINK_NESTED_DEPTH {
97100
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-
));
101+
let v =
102+
replace_all(&new_content, rel_path, source, depth + 1, chapter_title);
103+
let lines = v.split('\n').into_iter().collect::<Vec<&str>>();
104+
// no need to add offset for the first line
105+
replaced.push_str(lines[0]);
106+
for line in lines.iter().skip(1) {
107+
replaced.push_str(&format!("\n{}{}", &offset, &line));
108+
}
105109
} else {
106110
replaced.push_str(&new_content);
107111
}
Lines changed: 2 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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)