Skip to content

Commit

Permalink
Filter out temp files or with no extensions from colocated assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Keats committed Feb 16, 2023
1 parent ee4cbb6 commit f363ea1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 37 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 9 additions & 7 deletions components/content/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)))
}
}
Expand All @@ -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)))
}
}
Expand Down
28 changes: 28 additions & 0 deletions components/utils/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
32 changes: 3 additions & 29 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
/// to be reloaded
Expand Down

0 comments on commit f363ea1

Please sign in to comment.