Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

fix: complete deletion flow — precise matching, cache cleanup, wikilink cleanup, log

nash_sudb3789d7be9d
diff --git a/src-tauri/src/commands/fs.rs b/src-tauri/src/commands/fs.rs
index 36d6a582..d536a5f7 100644
--- a/src-tauri/src/commands/fs.rs
+++ b/src-tauri/src/commands/fs.rs
@@ -758,33 +758,62 @@ pub fn find_related_wiki_pages(project_path: String, source_name: String) -> Res
fn collect_related_pages(dir: &Path, source_name: &str, results: &mut Vec<String>) -> Result<(), String> {
let entries = fs::read_dir(dir).map_err(|e| e.to_string())?;
- // derive a slug from source filename for matching (e.g., "my-article.pdf" → "my-article")
- let slug = source_name
+
+ // Get just the filename without path
+ let file_name = source_name
.rsplit('/')
.next()
- .unwrap_or(source_name)
+ .unwrap_or(source_name);
+ let file_name_lower = file_name.to_lowercase();
+
+ // Derive stem (filename without extension) for source summary matching
+ let file_stem = file_name
.rsplit('.')
- .last()
- .unwrap_or(source_name)
- .to_lowercase();
+ .skip(1)
+ .collect::<Vec<_>>()
+ .into_iter()
+ .rev()
+ .collect::<Vec<_>>()
+ .join(".");
+ let file_stem_lower = if file_stem.is_empty() { file_name_lower.clone() } else { file_stem.to_lowercase() };
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
collect_related_pages(&path, source_name, results)?;
} else if path.extension().map(|e| e == "md").unwrap_or(false) {
- // Skip index.md and log.md — they'll be updated separately
let fname = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
+ // Skip index.md, log.md, overview.md — updated separately
if fname == "index.md" || fname == "log.md" || fname == "overview.md" {
continue;
}
if let Ok(content) = fs::read_to_string(&path) {
let content_lower = content.to_lowercase();
- // Match if the page references the source file name or its slug
- if content_lower.contains(&source_name.to_lowercase())
- || content_lower.contains(&slug)
- {
+
+ // Match 1: frontmatter sources field contains the exact filename
+ // e.g., sources: ["2603.25723v1.pdf"]
+ let sources_match = content_lower.contains(&format!("\"{}\"", file_name_lower))
+ || content_lower.contains(&format!("'{}'", file_name_lower));
+
+ // Match 2: source summary page (wiki/sources/{stem}.md)
+ let is_source_summary = path.to_string_lossy().contains("/sources/")
+ && fname.to_lowercase().starts_with(&file_stem_lower);
+
+ // Match 3: page was generated from this source (check frontmatter sources field)
+ let frontmatter_match = if let Some(fm_start) = content.find("---\n") {
+ if let Some(fm_end) = content[fm_start + 4..].find("\n---") {
+ let frontmatter = &content[fm_start..fm_start + 4 + fm_end].to_lowercase();
+ frontmatter.contains("sources:")
+ && frontmatter.contains(&file_name_lower)
+ } else {
+ false
+ }
+ } else {
+ false
+ };
+
+ if sources_match || is_source_summary || frontmatter_match {
results.push(path.to_string_lossy().to_string());
}
}
diff --git a/src/components/sources/sources-view.tsx b/src/components/sources/sources-view.tsx
index b7fc7e79..f5a72fd8 100644
--- a/src/components/sources/sources-view.tsx
+++ b/src/components/sources/sources-view.tsx
@@ -130,13 +130,24 @@ export function SourcesView() {
if (!confirmed) return
try {
- // Find related wiki pages before deleting
+ // Step 1: Find related wiki pages before deleting
const relatedPages = await findRelatedWikiPages(project.path, fileName)
+ const deletedSlugs = relatedPages.map((p) => {
+ const name = p.split("/").pop()?.replace(".md", "") ?? ""
+ return name
+ }).filter(Boolean)
- // Delete the source file
+ // Step 2: Delete the source file
await deleteFile(node.path)
- // Delete related wiki pages
+ // Step 3: Delete preprocessed cache
+ try {
+ await deleteFile(`${project.path}/raw/sources/.cache/${fileName}.txt`)
+ } catch {
+ // cache file may not exist
+ }
+
+ // Step 4: Delete related wiki pages
for (const pagePath of relatedPages) {
try {
await deleteFile(pagePath)
@@ -145,26 +156,55 @@ export function SourcesView() {
}
}
- // Remove entries from index.md that reference deleted pages
- if (relatedPages.length > 0) {
- try {
- const indexPath = `${project.path}/wiki/index.md`
- const indexContent = await readFile(indexPath)
- const deletedSlugs = relatedPages.map((p) => {
- const name = p.split("/").pop()?.replace(".md", "") ?? ""
- return name
- })
- const updatedIndex = indexContent
- .split("\n")
- .filter((line) => !deletedSlugs.some((slug) => slug && line.toLowerCase().includes(slug)))
- .join("\n")
- await writeFile(indexPath, updatedIndex)
- } catch {
- // index update failure is non-critical
+ // Step 5: Clean index.md — remove entries referencing deleted pages
+ try {
+ const indexPath = `${project.path}/wiki/index.md`
+ const indexContent = await readFile(indexPath)
+ const updatedIndex = indexContent
+ .split("\n")
+ .filter((line) => !deletedSlugs.some((slug) => line.toLowerCase().includes(slug.toLowerCase())))
+ .join("\n")
+ await writeFile(indexPath, updatedIndex)
+ } catch {
+ // non-critical
+ }
+
+ // Step 6: Clean [[wikilinks]] to deleted pages from remaining wiki files
+ try {
+ const wikiTree = await listDirectory(`${project.path}/wiki`)
+ const allMdFiles = flattenMdFiles(wikiTree)
+ for (const file of allMdFiles) {
+ try {
+ const content = await readFile(file.path)
+ let updated = content
+ for (const slug of deletedSlugs) {
+ // Replace [[slug]] and [[slug|display text]] with just the display text
+ const linkRegex = new RegExp(`\\[\\[${slug.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?:\\|([^\\]]+))?\\]\\]`, "gi")
+ updated = updated.replace(linkRegex, (_match, displayText) => displayText || slug)
+ }
+ if (updated !== content) {
+ await writeFile(file.path, updated)
+ }
+ } catch {
+ // skip unreadable files
+ }
}
+ } catch {
+ // non-critical
+ }
+
+ // Step 7: Append deletion record to log.md
+ try {
+ const logPath = `${project.path}/wiki/log.md`
+ const logContent = await readFile(logPath).catch(() => "# Wiki Log\n")
+ const date = new Date().toISOString().slice(0, 10)
+ const logEntry = `\n## [${date}] delete | ${fileName}\n\nDeleted source file and ${relatedPages.length} related wiki pages.\n`
+ await writeFile(logPath, logContent.trimEnd() + logEntry)
+ } catch {
+ // non-critical
}
- // Refresh
+ // Step 8: Refresh everything
await loadSources()
const tree = await listDirectory(project.path)
setFileTree(tree)
@@ -319,3 +359,15 @@ function flattenFiles(nodes: FileNode[]): FileNode[] {
}
return files
}
+
+function flattenMdFiles(nodes: FileNode[]): FileNode[] {
+ const files: FileNode[] = []
+ for (const node of nodes) {
+ if (node.is_dir && node.children) {
+ files.push(...flattenMdFiles(node.children))
+ } else if (!node.is_dir && node.name.endsWith(".md")) {
+ files.push(node)
+ }
+ }
+ return files
+}