From 587516ff7956d8f588f5bfb6aa54b357af27d0a7 Mon Sep 17 00:00:00 2001 From: Hatano Yuusuke <56590357+kynthus@users.noreply.github.com> Date: Sat, 5 Jun 2021 23:05:53 +0900 Subject: [PATCH] Align when `{{#include}}` is indented --- src/preprocess/links.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/preprocess/links.rs b/src/preprocess/links.rs index edd97ba9..52c8791b 100644 --- a/src/preprocess/links.rs +++ b/src/preprocess/links.rs @@ -93,17 +93,31 @@ where match link.render_with_path(&path, chapter_title) { 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 let Some(rel_path) = link.link_type.relative_path(path) { replaced.push_str(&replace_all( - &new_content, + &indented_new_content, rel_path, source, depth + 1, chapter_title, )); } else { - replaced.push_str(&new_content); + replaced.push_str(&indented_new_content); } } else { error!(