Make ignored_content work with nested paths and directories (#1738)

* Make ignored_content work with directories
Just had to remove strip_prefix(path) from file ignore code.
Added tests for subdirectory globbing.

* Add documentation

* add more tests
to confim that simple filename globs still match paths
without strip_prefix
This commit is contained in:
Kartavya Vashishtha 2022-02-07 01:42:27 +05:30 committed by Vincent Prouillet
parent ae3a8b802e
commit a5890a9901
5 changed files with 30 additions and 23 deletions

View File

@ -561,21 +561,28 @@ ignored_content = []
let config_str = r#"
title = "My site"
base_url = "example.com"
ignored_content = ["*.{graphml,iso}", "*.py?"]
ignored_content = ["*.{graphml,iso}", "*.py?", "**/{target,temp_folder}"]
"#;
let config = Config::parse(config_str).unwrap();
let v = config.ignored_content;
assert_eq!(v, vec!["*.{graphml,iso}", "*.py?"]);
assert_eq!(v, vec!["*.{graphml,iso}", "*.py?", "**/{target,temp_folder}"]);
let g = config.ignored_content_globset.unwrap();
assert_eq!(g.len(), 2);
assert_eq!(g.len(), 3);
assert!(g.is_match("foo.graphml"));
assert!(g.is_match("foo/bar/foo.graphml"));
assert!(g.is_match("foo.iso"));
assert!(!g.is_match("foo.png"));
assert!(g.is_match("foo.py2"));
assert!(g.is_match("foo.py3"));
assert!(!g.is_match("foo.py"));
assert!(g.is_match("foo/bar/target"));
assert!(g.is_match("foo/bar/baz/temp_folder"));
assert!(g.is_match("foo/bar/baz/temp_folder/target"));
assert!(g.is_match("temp_folder"));
assert!(g.is_match("my/isos/foo.iso"));
assert!(g.is_match("content/poetry/zen.py2"));
}
#[test]

View File

@ -56,10 +56,7 @@ pub fn find_related_assets(path: &Path, config: &Config, recursive: bool) -> Vec
if let Some(ref globset) = config.ignored_content_globset {
assets = assets
.into_iter()
.filter(|p| match p.strip_prefix(path) {
Err(_) => false,
Ok(file) => !globset.is_match(file),
})
.filter(|p| !globset.is_match(p))
.collect();
}

View File

@ -228,7 +228,7 @@ impl Section {
#[cfg(test)]
mod tests {
use std::fs::{create_dir, File};
use std::fs::{create_dir, File, create_dir_all};
use std::io::Write;
use std::path::{Path, PathBuf};
@ -268,23 +268,26 @@ mod tests {
fn section_with_ignored_assets_filters_out_correct_files() {
let tmp_dir = tempdir().expect("create temp dir");
let path = tmp_dir.path();
create_dir(&path.join("content")).expect("create content temp dir");
create_dir(&path.join("content").join("posts")).expect("create posts temp dir");
let nested_path = path.join("content").join("posts").join("with-assets");
create_dir(&nested_path).expect("create nested temp dir");
let mut f = File::create(nested_path.join("_index.md")).unwrap();
let article_path = path.join("content/posts/with-assets");
create_dir_all(path.join(&article_path).join("foo/bar/baz/quux")).expect("create nested temp dir");
create_dir_all(path.join(&article_path).join("foo/baz/quux")).expect("create nested temp dir");
let mut f = File::create(article_path.join("_index.md")).unwrap();
f.write_all(b"+++\nslug=\"hey\"\n+++\n").unwrap();
File::create(nested_path.join("example.js")).unwrap();
File::create(nested_path.join("graph.jpg")).unwrap();
File::create(nested_path.join("fail.png")).unwrap();
File::create(article_path.join("example.js")).unwrap();
File::create(article_path.join("graph.jpg")).unwrap();
File::create(article_path.join("fail.png")).unwrap();
File::create(article_path.join("foo/bar/baz/quux/quo.xlsx")).unwrap();
File::create(article_path.join("foo/bar/baz/quux/quo.docx")).unwrap();
let mut gsb = GlobSetBuilder::new();
gsb.add(Glob::new("*.{js,png}").unwrap());
gsb.add(Glob::new("foo/**/baz").unwrap());
let mut config = Config::default();
config.ignored_content_globset = Some(gsb.build().unwrap());
let res =
Section::from_file(nested_path.join("_index.md").as_path(), &config, &PathBuf::new());
Section::from_file(article_path.join("_index.md").as_path(), &config, &PathBuf::new());
assert!(res.is_ok());
let page = res.unwrap();

View File

@ -70,13 +70,13 @@ By default, this page's slug will be the directory name and thus its permalink w
It is possible to ignore selected asset files using the
[ignored_content](@/documentation/getting-started/configuration.md) setting in the config file.
For example, say that you have an Excel spreadsheet from which you are taking several screenshots and
then linking to these image files on your website. For maintainability, you want to keep
the spreadsheet in the same directory as the Markdown file, but you don't want to copy the spreadsheet to
the public web site. You can achieve this by setting `ignored_content` in the config file:
For example, say that you have several code files which you are linking to on your website.
For maintainability, you want to keep your code in the same directory as the Markdown file,
but you don't want to copy the build folders to the public web site. You can achieve this by setting `ignored_content` in the config file:
(Note of caution: `{Cargo.lock,target}` is _not_ the same as `{Cargo.lock, target}`)
```
ignored_content = ["*.xlsx"]
ignored_content = ["code_articles/**/{Cargo.lock,target}, *.rs"]
```
## Static assets

View File

@ -52,7 +52,7 @@ minify_html = false
# directory is processed. Defaults to none, which means that all asset files are
# copied over to the `public` directory.
# Example:
# ignored_content = ["*.{graphml,xlsx}", "temp.*"]
# ignored_content = ["*.{graphml,xlsx}", "temp.*", "**/build_folder"]
ignored_content = []
# When set to "true", a feed is automatically generated.