Align when `{{#include}}` is indented

This commit is contained in:
Hatano Yuusuke 2021-06-05 23:05:53 +09:00
parent 6425c29893
commit 587516ff79
1 changed files with 16 additions and 2 deletions

View File

@ -93,17 +93,31 @@ where
match link.render_with_path(&path, chapter_title) { match link.render_with_path(&path, chapter_title) {
Ok(new_content) => { Ok(new_content) => {
// When the beginning of `{{#include ...}}` is indented,
// the same indent string is added to the beginning of the second and subsequent lines.
let indented_new_content = Regex::new(r"(?m)^[[:blank:]]*")
.ok()
.and_then(|re| re.find_iter(&s[..link.start_index]).last())
.and_then(|mt| {
Regex::new(r"(?m)^")
.ok()
.map(|bol| (bol, mt.start(), mt.end()))
})
.map(|(bol, st, ed)| bol.replace_all(new_content.as_str(), &s[st..ed]))
.map(|cow| String::from(cow.trim()))
.unwrap_or_else(|| new_content);
if depth < MAX_LINK_NESTED_DEPTH { if depth < MAX_LINK_NESTED_DEPTH {
if let Some(rel_path) = link.link_type.relative_path(path) { if let Some(rel_path) = link.link_type.relative_path(path) {
replaced.push_str(&replace_all( replaced.push_str(&replace_all(
&new_content, &indented_new_content,
rel_path, rel_path,
source, source,
depth + 1, depth + 1,
chapter_title, chapter_title,
)); ));
} else { } else {
replaced.push_str(&new_content); replaced.push_str(&indented_new_content);
} }
} else { } else {
error!( error!(