Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 18 additions & 10 deletions crates/mdbook-driver/src/builtin_preprocessors/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
.join(&format!("\n{prefix}"));
replaced.push_str(&prefixed_new_content);
} else {
error!(
"Stack depth exceeded in {}. Check for cyclic includes",
Expand Down
22 changes: 22 additions & 0 deletions tests/testsuite/includes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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##"
<h1 id="multilines-include"><a class="header" href="#multilines-include">Multilines include</a></h1>
<p>Simple inclusion</p>
<pre><pre class="playground"><code class="language-rust">pub fn main() {
println!("Hello, World")
}</code></pre></pre>
<blockquote>
<p>Quoted inclusion</p>
<pre><pre class="playground"><code class="language-rust">pub fn main() {
println!("Hello, World")
}</code></pre></pre>
</blockquote>
<p>end</p>
"##]],
);
}

// Checks for anchored includes.
#[test]
fn anchored_include() {
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/includes/all_includes/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
- [Include Anchors](./anchors.md)
- [Rustdoc Includes](./rustdoc.md)
- [Playground Includes](./playground.md)
- [Multilines Includes](./multilines.md)
3 changes: 3 additions & 0 deletions tests/testsuite/includes/all_includes/src/hello_world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn main() {
println!("Hello, World")
}
14 changes: 14 additions & 0 deletions tests/testsuite/includes/all_includes/src/multilines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Multilines include

Simple inclusion

```rust
{{#include hello_world.rs}}
```

> Quoted inclusion
> ```rust
> {{#include hello_world.rs}}
> ```

end
Loading