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
65 changes: 63 additions & 2 deletions crates/mdbook-driver/src/builtin_preprocessors/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,21 @@ impl<'a> Link<'a> {
(_, Some(typ), Some(title)) if typ.as_str() == "title" => {
Some(LinkType::Title(title.as_str()))
}
// Include directives take the full remaining capture as the path so
// file names may contain spaces. Playground still splits on whitespace
// because it accepts trailing properties after the path.
(_, Some(typ), Some(rest)) if typ.as_str() == "include" => {
Some(parse_include_path(rest.as_str().trim()))
}
(_, Some(typ), Some(rest)) if typ.as_str() == "rustdoc_include" => {
Some(parse_rustdoc_include_path(rest.as_str().trim()))
}
(_, Some(typ), Some(rest)) => {
let mut path_props = rest.as_str().split_whitespace();
let file_arg = path_props.next();
let props: Vec<&str> = path_props.collect();

match (typ.as_str(), file_arg) {
("include", Some(pth)) => Some(parse_include_path(pth)),
("playground", Some(pth)) => Some(LinkType::Playground(pth.into(), props)),
("playpen", Some(pth)) => {
warn!(
Expand All @@ -301,7 +309,6 @@ impl<'a> Link<'a> {
);
Some(LinkType::Playground(pth.into(), props))
}
("rustdoc_include", Some(pth)) => Some(parse_rustdoc_include_path(pth)),
_ => None,
}
}
Expand Down Expand Up @@ -644,6 +651,60 @@ mod tests {
);
}

#[test]
fn test_find_links_with_space_in_path() {
let s = "Some random text with {{#include fila a.md}}...";
let res = find_links(s).collect::<Vec<_>>();
assert_eq!(
res,
vec![Link {
start_index: 22,
end_index: 44,
link_type: LinkType::Include(
PathBuf::from("fila a.md"),
RangeOrAnchor::Range(LineRange::from(..)),
),
link_text: "{{#include fila a.md}}",
}]
);
}

#[test]
fn test_find_links_with_space_in_path_and_range() {
let s = "Some random text with {{#include fila a.md:1:2}}...";
let res = find_links(s).collect::<Vec<_>>();
assert_eq!(
res,
vec![Link {
start_index: 22,
end_index: 48,
link_type: LinkType::Include(
PathBuf::from("fila a.md"),
RangeOrAnchor::Range(LineRange::from(0..2)),
),
link_text: "{{#include fila a.md:1:2}}",
}]
);
}

#[test]
fn test_find_links_rustdoc_include_with_space_in_path() {
let s = "Some random text with {{#rustdoc_include fila a.rs}}...";
let res = find_links(s).collect::<Vec<_>>();
assert_eq!(
res,
vec![Link {
start_index: 22,
end_index: 52,
link_type: LinkType::RustdocInclude(
PathBuf::from("fila a.rs"),
RangeOrAnchor::Range(LineRange::from(..)),
),
link_text: "{{#rustdoc_include fila a.rs}}",
}]
);
}

#[test]
fn test_find_links_with_anchor() {
let s = "Some random text with {{#include file.rs:anchor}}...";
Expand Down
3 changes: 3 additions & 0 deletions tests/testsuite/includes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ fn include() {
<h1 id="basic-includes"><a class="header" href="#basic-includes">Basic Includes</a></h1>
<h2 id="sample"><a class="header" href="#sample">Sample</a></h2>
<p>This is a sample include.</p>
<h2 id="file-with-space-in-name"><a class="header" href="#file-with-space-in-name">File with space in name</a></h2>
<h1 id="file-with-space"><a class="header" href="#file-with-space">File With Space</a></h1>
<p>This file name contains a space.</p>
"##]],
)
.check_main_file(
Expand Down
3 changes: 3 additions & 0 deletions tests/testsuite/includes/all_includes/src/fila a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# File With Space

This file name contains a space.
4 changes: 4 additions & 0 deletions tests/testsuite/includes/all_includes/src/includes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@

{{#include sample.md}}

## File with space in name

{{#include fila a.md}}