use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use anyhow::{Context, Result};
use notify_debouncer_full::notify::{RecommendedWatcher, RecursiveMode};
use notify_debouncer_full::{DebounceEventResult, Debouncer, RecommendedCache, new_debouncer};
use tokio::sync::{mpsc, watch};
use crate::render::Renderer;
const DEBOUNCE: Duration = Duration::from_millis(100);
const MAX_FILES: usize = 2000;
const MAX_DEPTH: usize = 6;
const SKIP_DIRS: &[&str] = &["node_modules", "target", "vendor", "__pycache__"];
const MAX_HITS: usize = 100;
const MAX_HITS_PER_FILE: usize = 3;
const MAX_SEARCH_FILE_BYTES: u64 = 2 * 1024 * 1024;
#[derive(Debug, Clone, serde::Serialize, PartialEq)]
pub struct Hit {
pub file: String,
pub line: usize,
pub text: String,
}
pub struct Docs {
root: PathBuf,
renderer: Arc<Renderer>,
channels: Mutex<HashMap<PathBuf, watch::Sender<String>>>,
watched: Mutex<HashSet<PathBuf>>,
debouncer: Mutex<Debouncer<RecommendedWatcher, RecommendedCache>>,
}
impl Docs {
pub fn new(root: PathBuf, renderer: Arc<Renderer>) -> Result<Arc<Self>> {
let (tx, mut rx) = mpsc::unbounded_channel::<Vec<PathBuf>>();
let debouncer = new_debouncer(DEBOUNCE, None, move |res: DebounceEventResult| {
if let Ok(events) = res {
let paths = events.into_iter().flat_map(|e| e.event.paths).collect();
let _ = tx.send(paths);
}
})
.context("failed to start file watcher")?;
let vault = renderer.options().obsidian;
let docs = Arc::new(Self {
root,
renderer,
channels: Mutex::default(),
watched: Mutex::default(),
debouncer: Mutex::new(debouncer),
});
if vault {
docs.debouncer
.lock()
.unwrap()
.watch(&docs.root, RecursiveMode::Recursive)
.with_context(|| format!("failed to watch {}", docs.root.display()))?;
}
let weak = Arc::downgrade(&docs);
tokio::spawn(async move {
while let Some(paths) = rx.recv().await {
let Some(docs) = weak.upgrade() else { break };
let _ = tokio::task::spawn_blocking(move || docs.changed(&paths)).await;
}
});
Ok(docs)
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn subscribe(&self, doc: &Path) -> Result<watch::Receiver<String>> {
let mut channels = self.channels.lock().unwrap();
if let Some(tx) = channels.get(doc) {
return Ok(tx.subscribe());
}
let html = self.render(doc)?;
let (tx, rx) = watch::channel(html);
channels.insert(doc.to_path_buf(), tx);
drop(channels);
let dir = if doc.is_dir() {
doc
} else {
doc.parent().unwrap_or(doc)
};
self.watch_dir(dir)?;
Ok(rx)
}
fn watch_dir(&self, dir: &Path) -> Result<()> {
let mut watched = self.watched.lock().unwrap();
if watched.insert(dir.to_path_buf()) {
self.debouncer
.lock()
.unwrap()
.watch(dir, RecursiveMode::NonRecursive)
.with_context(|| format!("failed to watch {}", dir.display()))?;
}
Ok(())
}
fn changed(&self, paths: &[PathBuf]) {
if self.renderer.options().obsidian && paths.iter().any(|p| is_markdown(p)) {
let all: Vec<PathBuf> = self.channels.lock().unwrap().keys().cloned().collect();
return self.refresh(all);
}
let mut affected = HashSet::new();
for path in paths {
let Some(parent) = path.parent().and_then(|p| p.canonicalize().ok()) else {
continue;
};
if let Some(name) = path.file_name() {
affected.insert(parent.join(name));
}
affected.insert(parent);
}
self.refresh(affected);
}
fn refresh(&self, docs: impl IntoIterator<Item = PathBuf>) {
let channels = self.channels.lock().unwrap();
for doc in docs {
let Some(tx) = channels.get(&doc) else {
continue;
};
if let Ok(html) = self.render(&doc) {
tx.send_if_modified(|current| {
if *current == html {
false
} else {
*current = html;
true
}
});
}
}
}
fn render(&self, doc: &Path) -> Result<String> {
if doc.is_dir() {
return Ok(self
.renderer
.render_body(&listing_markdown(&self.root, doc)));
}
let text = std::fs::read_to_string(doc)
.with_context(|| format!("cannot read {}", doc.display()))?;
Ok(self.render_text(&text, doc.parent().unwrap_or(&self.root)))
}
pub fn render_text(&self, text: &str, dir: &Path) -> String {
if !self.renderer.options().obsidian {
return self.renderer.render_body(text);
}
let (files, _) = self.list_files();
let ctx = crate::obsidian::LinkContext {
root: &self.root,
dir,
files: &files,
served: true,
depth: 0,
};
self.renderer.render_doc(text, Some(&ctx))
}
pub fn add_virtual(&self, key: &Path, html: String) {
let (tx, _) = watch::channel(html);
self.channels.lock().unwrap().insert(key.to_path_buf(), tx);
}
pub fn set_virtual(&self, key: &Path, html: String) {
if let Some(tx) = self.channels.lock().unwrap().get(key) {
tx.send_if_modified(|current| {
let changed = *current != html;
if changed {
*current = html;
}
changed
});
}
}
pub fn backlinks(&self, doc: &Path) -> Vec<Hit> {
let Ok(rel) = doc.strip_prefix(&self.root) else {
return Vec::new();
};
let rel = rel.to_string_lossy().replace('\\', "/");
let (files, _) = self.list_files();
crate::obsidian::backlinks(&self.root, &files, &rel)
.into_iter()
.map(|(file, line, text)| Hit { file, line, text })
.collect()
}
pub fn search(&self, query: &str) -> Vec<Hit> {
let needle = query.trim().to_lowercase();
let mut hits = Vec::new();
if needle.is_empty() {
return hits;
}
for file in self.list_files().0 {
let path = self.root.join(&file);
if std::fs::metadata(&path).map_or(true, |m| m.len() > MAX_SEARCH_FILE_BYTES) {
continue;
}
let Ok(text) = std::fs::read_to_string(&path) else {
continue;
};
let mut in_file = 0;
for (i, line) in text.lines().enumerate() {
let lower = line.to_lowercase();
let Some(at) = lower.find(&needle) else {
continue;
};
hits.push(Hit {
file: file.clone(),
line: i + 1,
text: snippet(line, at, needle.len()),
});
in_file += 1;
if hits.len() >= MAX_HITS {
return hits;
}
if in_file >= MAX_HITS_PER_FILE {
break;
}
}
}
hits
}
pub fn list_files(&self) -> (Vec<String>, bool) {
markdown_files(&self.root)
}
}
pub fn markdown_files(root: &Path) -> (Vec<String>, bool) {
let mut files = Vec::new();
let truncated = collect(root, root, 0, &mut files);
files.sort_by(|a, b| {
let depth = |s: &str| s.matches('/').count().min(1);
(depth(a), a.to_lowercase()).cmp(&(depth(b), b.to_lowercase()))
});
(files, truncated)
}
fn snippet(line: &str, at: usize, len: usize) -> String {
const CONTEXT: usize = 50;
let start = floor_char(line, at.saturating_sub(CONTEXT).min(line.len()));
let end = ceil_char(line, (at + len + CONTEXT).min(line.len()));
let mut out = String::new();
if start > 0 {
out.push('…');
}
out.push_str(line[start..end].trim());
if end < line.len() {
out.push('…');
}
out
}
fn floor_char(s: &str, mut i: usize) -> usize {
while i > 0 && !s.is_char_boundary(i) {
i -= 1;
}
i
}
fn ceil_char(s: &str, mut i: usize) -> usize {
while i < s.len() && !s.is_char_boundary(i) {
i += 1;
}
i
}
fn collect(root: &Path, dir: &Path, depth: usize, out: &mut Vec<String>) -> bool {
let Ok(entries) = std::fs::read_dir(dir) else {
return false;
};
let mut entries: Vec<_> = entries.flatten().collect();
entries.sort_by_key(|e| e.file_name());
for entry in entries {
if out.len() >= MAX_FILES {
return true;
}
let name = entry.file_name();
let name = name.to_string_lossy();
if name.starts_with('.') {
continue;
}
let Ok(kind) = entry.file_type() else {
continue;
};
let path = entry.path();
if kind.is_dir() {
if depth + 1 >= MAX_DEPTH || SKIP_DIRS.contains(&name.as_ref()) {
continue;
}
if collect(root, &path, depth + 1, out) {
return true;
}
} else if is_markdown(&path)
&& let Ok(rel) = path.strip_prefix(root)
{
out.push(rel.to_string_lossy().replace('\\', "/"));
}
}
false
}
pub fn is_markdown(path: &Path) -> bool {
path.extension().and_then(|e| e.to_str()).is_some_and(|e| {
matches!(
e.to_ascii_lowercase().as_str(),
"md" | "markdown" | "mdown" | "mkd" | "mkdn"
)
})
}
pub fn readme(dir: &Path) -> Option<PathBuf> {
let entries = std::fs::read_dir(dir).ok()?;
let mut names: Vec<_> = entries
.flatten()
.map(|e| e.path())
.filter(|p| p.is_file() && is_markdown(p))
.collect();
names.sort();
for wanted in ["readme", "index"] {
if let Some(p) = names.iter().find(|p| {
p.file_stem()
.is_some_and(|s| s.to_string_lossy().eq_ignore_ascii_case(wanted))
}) {
return Some(p.clone());
}
}
None
}
fn listing_markdown(root: &Path, dir: &Path) -> String {
let title = if dir == root {
dir.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| "/".into())
} else {
dir.strip_prefix(root)
.map(|p| p.to_string_lossy().into_owned())
.unwrap_or_default()
};
let mut dirs = Vec::new();
let mut files = Vec::new();
if let Ok(entries) = std::fs::read_dir(dir) {
for entry in entries.flatten() {
let name = entry.file_name().to_string_lossy().into_owned();
if name.starts_with('.') {
continue;
}
match entry.file_type() {
Ok(t) if t.is_dir() && !SKIP_DIRS.contains(&name.as_str()) => dirs.push(name),
Ok(t) if t.is_file() && is_markdown(&entry.path()) => files.push(name),
_ => {}
}
}
}
dirs.sort_by_key(|n| n.to_lowercase());
files.sort_by_key(|n| n.to_lowercase());
let mut md = format!("# {}\n\n", escape_md(&title));
if dir != root {
md.push_str("[↑ Parent folder](../)\n\n");
}
if dirs.is_empty() && files.is_empty() {
md.push_str("No Markdown files here.\n");
}
for d in &dirs {
md.push_str(&format!("- 📁 [{}/](<{}/>)\n", escape_md(d), d));
}
for f in &files {
md.push_str(&format!("- [{}](<{}>)\n", escape_md(f), f));
}
md
}
fn escape_md(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for c in s.chars() {
if "\\`*_{}[]()<>#+-.!|~$".contains(c) {
out.push('\\');
}
out.push(c);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
fn tree() -> tempfile::TempDir {
let dir = tempfile::tempdir().unwrap();
let r = dir.path();
std::fs::write(r.join("README.md"), "# Root").unwrap();
std::fs::write(r.join("notes.markdown"), "x").unwrap();
std::fs::write(r.join("image.png"), "x").unwrap();
std::fs::create_dir_all(r.join("docs/deep")).unwrap();
std::fs::write(r.join("docs/guide.md"), "x").unwrap();
std::fs::write(r.join("docs/deep/more.md"), "x").unwrap();
std::fs::create_dir_all(r.join("node_modules/pkg")).unwrap();
std::fs::write(r.join("node_modules/pkg/README.md"), "x").unwrap();
std::fs::create_dir_all(r.join(".git")).unwrap();
std::fs::write(r.join(".git/x.md"), "x").unwrap();
dir
}
#[test]
fn lists_markdown_files_root_first_skipping_noise() {
let dir = tree();
let mut files = Vec::new();
let root = dir.path().canonicalize().unwrap();
assert!(!collect(&root, &root, 0, &mut files));
files.sort_by(|a, b| {
let depth = |s: &str| s.matches('/').count().min(1);
(depth(a), a.to_lowercase()).cmp(&(depth(b), b.to_lowercase()))
});
assert_eq!(
files,
[
"notes.markdown",
"README.md",
"docs/deep/more.md",
"docs/guide.md"
]
);
}
#[test]
fn snippets_are_trimmed_on_char_boundaries() {
let line = format!("{}needle{}", "é".repeat(80), "ü".repeat(80));
let at = line.find("needle").unwrap();
let s = snippet(&line, at, 6);
assert!(
s.starts_with('…') && s.ends_with('…') && s.contains("needle"),
"{s}"
);
assert_eq!(snippet("short needle", 6, 6), "short needle");
}
#[test]
fn finds_readme_case_insensitively() {
let dir = tree();
assert_eq!(
readme(dir.path()).unwrap().file_name().unwrap(),
"README.md"
);
assert_eq!(readme(&dir.path().join("docs")), None);
}
#[test]
fn listing_links_folders_and_files() {
let dir = tree();
let root = dir.path();
let md = listing_markdown(root, &root.join("docs"));
assert!(md.starts_with("# docs\n"), "{md}");
assert!(md.contains("[↑ Parent folder](../)"));
assert!(md.contains("- 📁 [deep/](<deep/>)"));
assert!(md.contains("- [guide\\.md](<guide.md>)"));
}
}