diff --git a/crates/mdbook-driver/src/builtin_preprocessors/links.rs b/crates/mdbook-driver/src/builtin_preprocessors/links.rs index e6466ee924..8bb2399ea2 100644 --- a/crates/mdbook-driver/src/builtin_preprocessors/links.rs +++ b/crates/mdbook-driver/src/builtin_preprocessors/links.rs @@ -97,17 +97,25 @@ where 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, - )); + // use split('\n') instead of lines because we DON'T + // want the last \n to be removed + // Otherwise includes starting a new line would be prefixed + // by the preceding line + let prefix = replaced.split('\n').last().unwrap_or(""); + let raw_new_content = if let Some(rel_path) = link.link_type.relative_path(path) + { + replace_all(&new_content, rel_path, source, depth + 1, chapter_title) } else { - replaced.push_str(&new_content); - } + new_content + }; + // use lines instead of split('\n') because we DO + // want the last \n to be removed + // Otherwise inlined includes would fail + let prefixed_new_content = raw_new_content + .lines() + .collect::>() + .join(&format!("\n{prefix}")); + replaced.push_str(&prefixed_new_content); } else { error!( "Stack depth exceeded in {}. Check for cyclic includes", diff --git a/tests/testsuite/includes.rs b/tests/testsuite/includes.rs index 300df28e9f..0eb51ce141 100644 --- a/tests/testsuite/includes.rs +++ b/tests/testsuite/includes.rs @@ -24,6 +24,28 @@ fn include() { ); } +// Basic test for #include. +#[test] +fn include_multilines() { + BookTest::from_dir("includes/all_includes").check_main_file( + "book/multilines.html", + str![[r##" +

Multilines include

+

Simple inclusion

+
pub fn main() {
+    println!("Hello, World")
+}
+
+

Quoted inclusion

+
pub fn main() {
+    println!("Hello, World")
+}
+
+

end

+"##]], + ); +} + // Checks for anchored includes. #[test] fn anchored_include() { diff --git a/tests/testsuite/includes/all_includes/src/SUMMARY.md b/tests/testsuite/includes/all_includes/src/SUMMARY.md index 25a9a15f71..f3dabeca4b 100644 --- a/tests/testsuite/includes/all_includes/src/SUMMARY.md +++ b/tests/testsuite/includes/all_includes/src/SUMMARY.md @@ -6,3 +6,4 @@ - [Include Anchors](./anchors.md) - [Rustdoc Includes](./rustdoc.md) - [Playground Includes](./playground.md) +- [Multilines Includes](./multilines.md) diff --git a/tests/testsuite/includes/all_includes/src/hello_world.rs b/tests/testsuite/includes/all_includes/src/hello_world.rs new file mode 100644 index 0000000000..7e58e7a78a --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/hello_world.rs @@ -0,0 +1,3 @@ +pub fn main() { + println!("Hello, World") +} diff --git a/tests/testsuite/includes/all_includes/src/multilines.md b/tests/testsuite/includes/all_includes/src/multilines.md new file mode 100644 index 0000000000..83bb41d80e --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/multilines.md @@ -0,0 +1,14 @@ +# Multilines include + +Simple inclusion + +```rust +{{#include hello_world.rs}} +``` + +> Quoted inclusion +> ```rust +> {{#include hello_world.rs}} +> ``` + +end \ No newline at end of file