use std::path::{Path, PathBuf};
pub fn collect_markdown_files(paths: &[PathBuf], recursive: bool) -> Vec<PathBuf> {
let mut files = Vec::new();
for path in paths {
if path.is_file() {
if is_markdown(path) {
files.push(path.clone());
}
} else if path.is_dir() {
collect_dir(path, recursive, &mut files);
} else {
eprintln!("Warning: {} does not exist, skipping", path.display());
}
}
files
}
fn collect_dir(dir: &Path, recursive: bool, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.filter_map(Result::ok) {
let path = entry.path();
if path.is_file() && is_markdown(&path) {
out.push(path);
} else if path.is_dir() && recursive {
collect_dir(&path, recursive, out);
}
}
}
fn is_markdown(path: &Path) -> bool {
matches!(
path.extension().and_then(|e| e.to_str()),
Some("md") | Some("markdown")
)
}