Only find assets recursively for pages (#1677)
This commit is contained in:
parent
51784aa60f
commit
b3139b7622
@ -30,10 +30,17 @@ pub fn has_anchor(headings: &[Heading], anchor: &str) -> bool {
|
||||
|
||||
/// Looks into the current folder for the path and see if there's anything that is not a .md
|
||||
/// file. Those will be copied next to the rendered .html file
|
||||
pub fn find_related_assets(path: &Path, config: &Config) -> Vec<PathBuf> {
|
||||
/// If `recursive` is set to `true`, it will add all subdirectories assets as well. This should
|
||||
/// only be set when finding page assets currently.
|
||||
/// TODO: remove this flag once sections with assets behave the same as pages with assets
|
||||
pub fn find_related_assets(path: &Path, config: &Config, recursive: bool) -> Vec<PathBuf> {
|
||||
let mut assets = vec![];
|
||||
|
||||
for entry in WalkDir::new(path).into_iter().filter_map(std::result::Result::ok) {
|
||||
let mut builder = WalkDir::new(path);
|
||||
if !recursive {
|
||||
builder = builder.max_depth(1);
|
||||
}
|
||||
for entry in builder.into_iter().filter_map(std::result::Result::ok) {
|
||||
let entry_path = entry.path();
|
||||
if entry_path.is_file() {
|
||||
match entry_path.extension() {
|
||||
@ -68,7 +75,7 @@ mod tests {
|
||||
use tempfile::tempdir;
|
||||
|
||||
#[test]
|
||||
fn can_find_related_assets() {
|
||||
fn can_find_related_assets_recursive() {
|
||||
let tmp_dir = tempdir().expect("create temp dir");
|
||||
let path = tmp_dir.path();
|
||||
File::create(path.join("index.md")).unwrap();
|
||||
@ -79,39 +86,41 @@ mod tests {
|
||||
File::create(path.join("subdir").join("index.md")).unwrap();
|
||||
File::create(path.join("subdir").join("example.js")).unwrap();
|
||||
|
||||
let assets = find_related_assets(path, &Config::default());
|
||||
let assets = find_related_assets(path, &Config::default(), true);
|
||||
assert_eq!(assets.len(), 4);
|
||||
assert_eq!(assets.iter().filter(|p| p.extension().unwrap() != "md").count(), 4);
|
||||
assert_eq!(
|
||||
assets
|
||||
|
||||
for asset in vec!["example.js", "graph.jpg", "fail.png", "subdir/example.js"] {
|
||||
assert!(assets
|
||||
.iter()
|
||||
.filter(|p| p.strip_prefix(path).unwrap() == Path::new("example.js"))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
assets
|
||||
.iter()
|
||||
.filter(|p| p.strip_prefix(path).unwrap() == Path::new("graph.jpg"))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
assets
|
||||
.iter()
|
||||
.filter(|p| p.strip_prefix(path).unwrap() == Path::new("fail.png"))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
assets
|
||||
.iter()
|
||||
.filter(|p| p.strip_prefix(path).unwrap() == Path::new("subdir/example.js"))
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
.find(|p| p.strip_prefix(path).unwrap() == Path::new(asset))
|
||||
.is_some())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_find_related_assets_non_recursive() {
|
||||
let tmp_dir = tempdir().expect("create temp dir");
|
||||
let path = tmp_dir.path();
|
||||
File::create(path.join("index.md")).unwrap();
|
||||
File::create(path.join("example.js")).unwrap();
|
||||
File::create(path.join("graph.jpg")).unwrap();
|
||||
File::create(path.join("fail.png")).unwrap();
|
||||
create_dir(path.join("subdir")).expect("create subdir temp dir");
|
||||
File::create(path.join("subdir").join("index.md")).unwrap();
|
||||
File::create(path.join("subdir").join("example.js")).unwrap();
|
||||
|
||||
let assets = find_related_assets(path, &Config::default(), false);
|
||||
assert_eq!(assets.len(), 3);
|
||||
assert_eq!(assets.iter().filter(|p| p.extension().unwrap() != "md").count(), 3);
|
||||
|
||||
for asset in vec!["example.js", "graph.jpg", "fail.png"] {
|
||||
assert!(assets
|
||||
.iter()
|
||||
.find(|p| p.strip_prefix(path).unwrap() == Path::new(asset))
|
||||
.is_some())
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn can_find_anchor_at_root() {
|
||||
let input = vec![
|
||||
|
@ -208,7 +208,7 @@ impl Page {
|
||||
|
||||
if page.file.name == "index" {
|
||||
let parent_dir = path.parent().unwrap();
|
||||
page.assets = find_related_assets(parent_dir, config);
|
||||
page.assets = find_related_assets(parent_dir, config, true);
|
||||
page.serialized_assets = page.serialize_assets(base_path);
|
||||
} else {
|
||||
page.assets = vec![];
|
||||
|
@ -122,7 +122,7 @@ impl Section {
|
||||
let mut section = Section::parse(path, &content, config, base_path)?;
|
||||
|
||||
let parent_dir = path.parent().unwrap();
|
||||
section.assets = find_related_assets(parent_dir, config);
|
||||
section.assets = find_related_assets(parent_dir, config, false);
|
||||
section.serialized_assets = section.serialize_assets();
|
||||
|
||||
Ok(section)
|
||||
|
Loading…
Reference in New Issue
Block a user