From f363ea1ae8ea2626e58c116fce03e59653a3c762 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 23 Jan 2023 20:39:10 +0100 Subject: [PATCH] Filter out temp files or with no extensions from colocated assets --- CHANGELOG.md | 3 ++- components/content/src/utils.rs | 16 +++++++++------- components/utils/src/fs.rs | 28 ++++++++++++++++++++++++++++ src/cmd/serve.rs | 32 +++----------------------------- 4 files changed, 42 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a848ca42c..624c328cc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ for breaking changes with libsass: look for "beginning in Dart Sass" This will error if 2 values are set - Code blocks content are no longer included in the search index - 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 diff --git a/components/content/src/utils.rs b/components/content/src/utils.rs index 176b221016..58fd3ec847 100644 --- a/components/content/src/utils.rs +++ b/components/content/src/utils.rs @@ -4,6 +4,7 @@ use libs::unicode_segmentation::UnicodeSegmentation; use libs::walkdir::WalkDir; use config::Config; +use utils::fs::is_temp_file; use utils::table_of_contents::Heading; 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) { let entry_path = entry.path(); - if entry_path.is_file() { + + if entry_path.is_file() && !is_temp_file(entry_path) { match entry_path.extension() { Some(e) => match e.to_str() { Some("md") => continue, @@ -82,10 +84,10 @@ mod tests { File::create(path.join("subdir").join("example.js")).unwrap(); let assets = find_related_assets(path, &Config::default(), true); - assert_eq!(assets.len(), 5); - assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 5); + assert_eq!(assets.len(), 4); + 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))) } } @@ -103,10 +105,10 @@ 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(), false); - assert_eq!(assets.len(), 4); - assert_eq!(assets.iter().filter(|p| p.extension().unwrap_or_default() != "md").count(), 4); + assert_eq!(assets.len(), 3); + 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))) } } diff --git a/components/utils/src/fs.rs b/components/utils/src/fs.rs index 9b927d42d1..1e719f0c7b 100644 --- a/components/utils/src/fs.rs +++ b/components/utils/src/fs.rs @@ -174,6 +174,34 @@ where 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)] mod tests { use std::fs::{metadata, read_to_string, File}; diff --git a/src/cmd/serve.rs b/src/cmd/serve.rs index 791ad63b6f..9d0128cb2e 100644 --- a/src/cmd/serve.rs +++ b/src/cmd/serve.rs @@ -38,16 +38,16 @@ use time::{OffsetDateTime, UtcOffset}; use libs::percent_encoding; use libs::serde_json; +use libs::globset::GlobSet; +use libs::relative_path::{RelativePath, RelativePathBuf}; use notify::{watcher, RecursiveMode, Watcher}; use ws::{Message, Sender, WebSocket}; use errors::{anyhow, Context, Result}; -use libs::globset::GlobSet; -use libs::relative_path::{RelativePath, RelativePathBuf}; use pathdiff::diff_paths; use site::sass::compile_sass; use site::{Site, SITE_CONTENT}; -use utils::fs::copy_file; +use utils::fs::{copy_file, is_temp_file}; use crate::messages; use std::ffi::OsStr; @@ -652,32 +652,6 @@ fn is_ignored_file(ignored_content_globset: &Option, 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 /// to be reloaded