Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep indent when using include in code block #1718

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/preprocess/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,26 @@ where
let mut replaced = String::new();

for link in find_links(s) {
replaced.push_str(&s[previous_end_index..link.start_index]);
let before_link = &s[previous_end_index..link.start_index];
let offset = if let Some(i) = before_link.rfind('\n') {
&before_link[i + 1..]
} else {
""
};
replaced.push_str(before_link);

match link.render_with_path(&path, chapter_title) {
Ok(new_content) => {
if depth < MAX_LINK_NESTED_DEPTH {
if let Some(rel_path) = link.link_type.relative_path(path) {
replaced.push_str(&replace_all(
&new_content,
rel_path,
source,
depth + 1,
chapter_title,
));
let v =
replace_all(&new_content, rel_path, source, depth + 1, chapter_title);
let lines = v.split('\n').into_iter().collect::<Vec<&str>>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're iterating over the lines one-by-one, you could iterate directly over the result of split. So you would save the collect() call be a tiny bit more efficient.

// no need to add offset for the first line
replaced.push_str(lines[0]);
for line in lines.iter().skip(1) {
replaced.push_str(&format!("\n{}{}", &offset, &line));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could consider changing this to

                            replaced.push('\n');
                            replaced.push_str(&offset);
                            replaced.push_str(&line);

since you're simply concatenating strings.

}
} else {
replaced.push_str(&new_content);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/dummy_book/src/first/eqNonEmpty.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instance Eq (NonEmpty a) where
eq (NonEmpty x xs) (NonEmpty y ys) = x == y && xs == ys
14 changes: 14 additions & 0 deletions tests/dummy_book/src/first/nested.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,17 @@ assert!($TEST_STATUS);
```rust
{{#rustdoc_include partially-included-test-with-anchors.rs:rustdoc-include-anchor}}
```

## Keep Indent

1. The following code remains indent:
```haskell
{{#include ./eqNonEmpty.hs}}
```
we ...

1. The following code also remains indent:
```haskell
{{#include ./eqNonEmpty.hs}}
```
we ...
Loading