From b3139b7622791da07d861da653d8a1264a0de401 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sat, 4 Dec 2021 13:58:02 +0100 Subject: [PATCH] Only find assets recursively for pages (#1677) --- components/library/src/content/mod.rs | 71 +++++++++++++---------- components/library/src/content/page.rs | 2 +- components/library/src/content/section.rs | 2 +- 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/components/library/src/content/mod.rs b/components/library/src/content/mod.rs index 0f5836e3..8a165974 100644 --- a/components/library/src/content/mod.rs +++ b/components/library/src/content/mod.rs @@ -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 { +/// 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 { 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![ diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index 364aa2e9..64d9a82c 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -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![]; diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index 18bb830f..d2fb636d 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -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)