Filter out temp files or with no extensions from colocated assets

This commit is contained in:
Vincent Prouillet 2023-01-23 20:39:10 +01:00 committed by Vincent Prouillet
parent ee4cbb649a
commit f363ea1ae8
4 changed files with 42 additions and 37 deletions

View File

@ -11,6 +11,7 @@ This will error if 2 values are set
- Code blocks content are no longer included in the search index - Code blocks content are no longer included in the search index
- Remove built-ins shortcodes - Remove built-ins shortcodes
- Having a file called `index.md` in a folder with a `_index.md` is now an error - Having a file called `index.md` in a folder with a `_index.md` is now an error
- Ignore temp files from vim/emacs/macos/etc as well as files without extensions when getting colocated assets
### Other ### Other

View File

@ -4,6 +4,7 @@ use libs::unicode_segmentation::UnicodeSegmentation;
use libs::walkdir::WalkDir; use libs::walkdir::WalkDir;
use config::Config; use config::Config;
use utils::fs::is_temp_file;
use utils::table_of_contents::Heading; use utils::table_of_contents::Heading;
pub fn has_anchor(headings: &[Heading], anchor: &str) -> bool { pub fn has_anchor(headings: &[Heading], anchor: &str) -> bool {
@ -33,7 +34,8 @@ pub fn find_related_assets(path: &Path, config: &Config, recursive: bool) -> Vec
} }
for entry in builder.into_iter().filter_map(std::result::Result::ok) { for entry in builder.into_iter().filter_map(std::result::Result::ok) {
let entry_path = entry.path(); let entry_path = entry.path();
if entry_path.is_file() {
if entry_path.is_file() && !is_temp_file(entry_path) {
match entry_path.extension() { match entry_path.extension() {
Some(e) => match e.to_str() { Some(e) => match e.to_str() {
Some("md") => continue, Some("md") => continue,
@ -82,10 +84,10 @@ mod tests {
File::create(path.join("subdir").join("example.js")).unwrap(); File::create(path.join("subdir").join("example.js")).unwrap();
let assets = find_related_assets(path, &Config::default(), true); let assets = find_related_assets(path, &Config::default(), true);
assert_eq!(assets.len(), 5); assert_eq!(assets.len(), 4);
assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 5); assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 4);
for asset in ["example.js", "graph.jpg", "fail.png", "subdir/example.js", "extensionless"] { for asset in ["example.js", "graph.jpg", "fail.png", "subdir/example.js"] {
assert!(assets.iter().any(|p| p.strip_prefix(path).unwrap() == Path::new(asset))) assert!(assets.iter().any(|p| p.strip_prefix(path).unwrap() == Path::new(asset)))
} }
} }
@ -103,10 +105,10 @@ mod tests {
File::create(path.join("subdir").join("index.md")).unwrap(); File::create(path.join("subdir").join("index.md")).unwrap();
File::create(path.join("subdir").join("example.js")).unwrap(); File::create(path.join("subdir").join("example.js")).unwrap();
let assets = find_related_assets(path, &Config::default(), false); let assets = find_related_assets(path, &Config::default(), false);
assert_eq!(assets.len(), 4); assert_eq!(assets.len(), 3);
assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 4); assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 3);
for asset in ["example.js", "graph.jpg", "fail.png", "extensionless"] { for asset in ["example.js", "graph.jpg", "fail.png"] {
assert!(assets.iter().any(|p| p.strip_prefix(path).unwrap() == Path::new(asset))) assert!(assets.iter().any(|p| p.strip_prefix(path).unwrap() == Path::new(asset)))
} }
} }

View File

@ -174,6 +174,34 @@ where
path.as_ref().file_name().and_then(|s| s.to_str()).map(|s| s.starts_with('.')).unwrap_or(false) path.as_ref().file_name().and_then(|s| s.to_str()).map(|s| s.starts_with('.')).unwrap_or(false)
} }
/// Returns whether the path we received corresponds to a temp file created
/// by an editor or the OS
pub fn is_temp_file(path: &Path) -> bool {
let ext = path.extension();
match ext {
Some(ex) => match ex.to_str().unwrap() {
"swp" | "swx" | "tmp" | ".DS_STORE" | ".DS_Store" => true,
// jetbrains IDE
x if x.ends_with("jb_old___") => true,
x if x.ends_with("jb_tmp___") => true,
x if x.ends_with("jb_bak___") => true,
// vim & jetbrains
x if x.ends_with('~') => true,
_ => {
if let Some(filename) = path.file_stem() {
// emacs
let name = filename.to_str().unwrap();
name.starts_with('#') || name.starts_with(".#")
} else {
false
}
}
},
None => true,
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::fs::{metadata, read_to_string, File}; use std::fs::{metadata, read_to_string, File};

View File

@ -38,16 +38,16 @@ use time::{OffsetDateTime, UtcOffset};
use libs::percent_encoding; use libs::percent_encoding;
use libs::serde_json; use libs::serde_json;
use libs::globset::GlobSet;
use libs::relative_path::{RelativePath, RelativePathBuf};
use notify::{watcher, RecursiveMode, Watcher}; use notify::{watcher, RecursiveMode, Watcher};
use ws::{Message, Sender, WebSocket}; use ws::{Message, Sender, WebSocket};
use errors::{anyhow, Context, Result}; use errors::{anyhow, Context, Result};
use libs::globset::GlobSet;
use libs::relative_path::{RelativePath, RelativePathBuf};
use pathdiff::diff_paths; use pathdiff::diff_paths;
use site::sass::compile_sass; use site::sass::compile_sass;
use site::{Site, SITE_CONTENT}; use site::{Site, SITE_CONTENT};
use utils::fs::copy_file; use utils::fs::{copy_file, is_temp_file};
use crate::messages; use crate::messages;
use std::ffi::OsStr; use std::ffi::OsStr;
@ -652,32 +652,6 @@ fn is_ignored_file(ignored_content_globset: &Option<GlobSet>, path: &Path) -> bo
} }
} }
/// Returns whether the path we received corresponds to a temp file created
/// by an editor or the OS
fn is_temp_file(path: &Path) -> bool {
let ext = path.extension();
match ext {
Some(ex) => match ex.to_str().unwrap() {
"swp" | "swx" | "tmp" | ".DS_STORE" => true,
// jetbrains IDE
x if x.ends_with("jb_old___") => true,
x if x.ends_with("jb_tmp___") => true,
x if x.ends_with("jb_bak___") => true,
// vim & jetbrains
x if x.ends_with('~') => true,
_ => {
if let Some(filename) = path.file_stem() {
// emacs
let name = filename.to_str().unwrap();
name.starts_with('#') || name.starts_with(".#")
} else {
false
}
}
},
None => true,
}
}
/// Detect what changed from the given path so we have an idea what needs /// Detect what changed from the given path so we have an idea what needs
/// to be reloaded /// to be reloaded