use std::io;
use std::path::Path;
use walkdir::{DirEntry, WalkDir};
use crate::model::RawDoc;
const PRUNED_DIR_NAMES: &[&str] = &["node_modules", "target", "vendor", ".git"];
fn is_kept(entry: &DirEntry) -> bool {
if entry.depth() == 0 {
return true;
}
let name = entry.file_name().to_string_lossy();
if entry.file_type().is_dir() {
if name.starts_with('.') || PRUNED_DIR_NAMES.contains(&name.as_ref()) {
return false;
}
}
true
}
pub fn discover_docs(root: &Path) -> std::io::Result<Vec<RawDoc>> {
let mut docs = Vec::new();
let walker = WalkDir::new(root)
.sort_by_file_name()
.into_iter()
.filter_entry(is_kept);
for entry in walker {
let entry = entry.map_err(io::Error::from)?;
let path = entry.path();
if path.extension().and_then(|e| e.to_str()) != Some("md") {
continue;
}
let rel = path
.strip_prefix(root)
.unwrap_or(path)
.to_string_lossy()
.replace('\\', "/");
let raw = std::fs::read_to_string(path)?;
docs.push(RawDoc { rel_path: rel, raw });
}
Ok(docs)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn skips_non_md_and_returns_slash_separated_rel_paths() {
let dir = std::env::temp_dir().join(format!("docgen_discover_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("guide")).unwrap();
std::fs::write(dir.join("index.md"), "# Home\n").unwrap();
std::fs::write(dir.join("notes.txt"), "ignore me\n").unwrap();
std::fs::write(dir.join("guide/intro.md"), "# Intro\n").unwrap();
let docs = discover_docs(&dir).unwrap();
let rels: Vec<&str> = docs.iter().map(|d| d.rel_path.as_str()).collect();
assert_eq!(rels, vec!["guide/intro.md", "index.md"]);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn prunes_hidden_and_vendor_dirs() {
let dir = std::env::temp_dir().join(format!("docgen_prune_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join(".obsidian/plugins")).unwrap();
std::fs::create_dir_all(dir.join("node_modules/pkg")).unwrap();
std::fs::create_dir_all(dir.join("guide")).unwrap();
std::fs::write(dir.join("index.md"), "# Home\n").unwrap();
std::fs::write(dir.join("guide/intro.md"), "# Intro\n").unwrap();
std::fs::write(dir.join(".obsidian/plugins/conf.md"), "# junk\n").unwrap();
std::fs::write(dir.join("node_modules/pkg/README.md"), "# dep\n").unwrap();
let docs = discover_docs(&dir).unwrap();
let rels: Vec<&str> = docs.iter().map(|d| d.rel_path.as_str()).collect();
assert_eq!(rels, vec!["guide/intro.md", "index.md"]);
let _ = std::fs::remove_dir_all(&dir);
}
}