normalize path to relative

This commit is contained in:
leonzchang 2023-11-01 12:33:13 +08:00
parent 3ab19f3295
commit 722c55f85f
No known key found for this signature in database
GPG Key ID: DBC5671A30EB9CA3
3 changed files with 12 additions and 4 deletions

7
Cargo.lock generated
View File

@ -974,6 +974,7 @@ dependencies = [
"notify-debouncer-mini",
"once_cell",
"opener",
"pathdiff",
"predicates",
"pretty_assertions",
"pulldown-cmark",
@ -1151,6 +1152,12 @@ dependencies = [
"windows-targets 0.48.1",
]
[[package]]
name = "pathdiff"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
[[package]]
name = "percent-encoding"
version = "2.3.0"

View File

@ -28,6 +28,7 @@ log = "0.4.17"
memchr = "2.5.0"
opener = "0.6.1"
pulldown-cmark = { version = "0.9.3", default-features = false }
pathdiff = "0.2.1"
regex = "1.8.1"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"

View File

@ -4,6 +4,7 @@ use ignore::gitignore::Gitignore;
use mdbook::errors::Result;
use mdbook::utils;
use mdbook::MDBook;
use pathdiff::diff_paths;
use std::env;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
@ -93,11 +94,10 @@ fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> {
paths
.iter()
.filter(|path| {
let normalized_path = path
.strip_prefix(&current_dir)
.expect("Could not normalize path");
let relative_path =
diff_paths(&current_dir, &path).expect("One of the paths should be an absolute");
!ignore
.matched_path_or_any_parents(normalized_path, normalized_path.is_dir())
.matched_path_or_any_parents(&relative_path, relative_path.is_dir())
.is_ignore()
})
.map(|path| path.to_path_buf())