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

Conversation

rchaser53
Copy link

@rchaser53 rchaser53 commented Jan 2, 2022

I tried to fix issue 1626. mdBook ignores the indent in code block created by #include. I think this is the reason why issue 1626 happened. So I implemented to add the offset to the code block created by #include.
fix: #1626

@rchaser53
Copy link
Author

rchaser53 commented Jan 5, 2022

I think this PR also fixes #1564. Could you review this PR? @ehuss
Many users suffer from this bug. The following snapshot is an output using this branch for #1564 input.

Evidence

@onbjerg
Copy link

onbjerg commented Apr 6, 2022

Writing a book currently where I ran into the issue that is resolved in this PR. Are there any blockers for this to get merged?

// 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.

));
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintain leading whitespace when including portions of a file
4 participants