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

feat: add source deletion with cascading wiki page cleanup

nash_sud7cf6eed8606
diff --git a/src-tauri/src/commands/fs.rs b/src-tauri/src/commands/fs.rs
index b2301be7..cb34b11a 100644
--- a/src-tauri/src/commands/fs.rs
+++ b/src-tauri/src/commands/fs.rs
@@ -123,6 +123,69 @@ pub fn copy_file(source: String, destination: String) -> Result<(), String> {
Ok(())
}
+#[tauri::command]
+pub fn delete_file(path: String) -> Result<(), String> {
+ let p = Path::new(&path);
+ if p.is_dir() {
+ fs::remove_dir_all(&path)
+ .map_err(|e| format!("Failed to delete directory '{}': {}", path, e))
+ } else {
+ fs::remove_file(&path)
+ .map_err(|e| format!("Failed to delete file '{}': {}", path, e))
+ }
+}
+
+/// Find wiki pages that reference a given source file name.
+/// Scans all .md files under wiki/ for the source filename in frontmatter or content.
+#[tauri::command]
+pub fn find_related_wiki_pages(project_path: String, source_name: String) -> Result<Vec<String>, String> {
+ let wiki_dir = Path::new(&project_path).join("wiki");
+ if !wiki_dir.is_dir() {
+ return Ok(vec![]);
+ }
+
+ let mut related = Vec::new();
+ collect_related_pages(&wiki_dir, &source_name, &mut related)?;
+ Ok(related)
+}
+
+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
+ .rsplit('/')
+ .next()
+ .unwrap_or(source_name)
+ .rsplit('.')
+ .last()
+ .unwrap_or(source_name)
+ .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("");
+ 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)
+ {
+ results.push(path.to_string_lossy().to_string());
+ }
+ }
+ }
+ }
+ Ok(())
+}
+
#[tauri::command]
pub fn create_directory(path: String) -> Result<(), String> {
fs::create_dir_all(&path)
diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs
index f7123c25..9e3d59a3 100644
--- a/src-tauri/src/lib.rs
+++ b/src-tauri/src/lib.rs
@@ -12,6 +12,8 @@ pub fn run() {
commands::fs::write_file,
commands::fs::list_directory,
commands::fs::copy_file,
+ commands::fs::delete_file,
+ commands::fs::find_related_wiki_pages,
commands::fs::create_directory,
commands::project::create_project,
commands::project::open_project,
diff --git a/src/commands/fs.ts b/src/commands/fs.ts
index a5131f1d..79227956 100644
--- a/src/commands/fs.ts
+++ b/src/commands/fs.ts
@@ -20,6 +20,17 @@ export async function copyFile(
return invoke("copy_file", { source, destination })
}
+export async function deleteFile(path: string): Promise<void> {
+ return invoke("delete_file", { path })
+}
+
+export async function findRelatedWikiPages(
+ projectPath: string,
+ sourceName: string
+): Promise<string[]> {
+ return invoke<string[]>("find_related_wiki_pages", { projectPath, sourceName })
+}
+
export async function createDirectory(path: string): Promise<void> {
return invoke<void>("create_directory", { path })
}
diff --git a/src/components/sources/sources-view.tsx b/src/components/sources/sources-view.tsx
index 0c0a023c..688a8ec8 100644
--- a/src/components/sources/sources-view.tsx
+++ b/src/components/sources/sources-view.tsx
@@ -1,18 +1,20 @@
import { useState, useEffect, useCallback } from "react"
import { open } from "@tauri-apps/plugin-dialog"
-import { Plus, FileText, RefreshCw, BookOpen } from "lucide-react"
+import { Plus, FileText, RefreshCw, BookOpen, Trash2 } from "lucide-react"
import { Button } from "@/components/ui/button"
import { ScrollArea } from "@/components/ui/scroll-area"
import { useWikiStore } from "@/stores/wiki-store"
-import { copyFile, listDirectory, readFile } from "@/commands/fs"
+import { copyFile, listDirectory, readFile, writeFile, deleteFile, findRelatedWikiPages } from "@/commands/fs"
import type { FileNode } from "@/types/wiki"
import { startIngest, autoIngest } from "@/lib/ingest"
export function SourcesView() {
const project = useWikiStore((s) => s.project)
+ const selectedFile = useWikiStore((s) => s.selectedFile)
const setSelectedFile = useWikiStore((s) => s.setSelectedFile)
const setActiveView = useWikiStore((s) => s.setActiveView)
const setFileContent = useWikiStore((s) => s.setFileContent)
+ const setFileTree = useWikiStore((s) => s.setFileTree)
const setChatExpanded = useWikiStore((s) => s.setChatExpanded)
const llmConfig = useWikiStore((s) => s.llmConfig)
const [sources, setSources] = useState<FileNode[]>([])
@@ -114,6 +116,64 @@ export function SourcesView() {
}
}
+ async function handleDelete(node: FileNode) {
+ if (!project) return
+ const fileName = node.name
+ const confirmed = window.confirm(
+ `Delete "${fileName}" and its related wiki pages?\n\nThis will:\n- Delete the source file\n- Delete wiki pages generated from this source\n- Update index.md`
+ )
+ if (!confirmed) return
+
+ try {
+ // Find related wiki pages before deleting
+ const relatedPages = await findRelatedWikiPages(project.path, fileName)
+
+ // Delete the source file
+ await deleteFile(node.path)
+
+ // Delete related wiki pages
+ for (const pagePath of relatedPages) {
+ try {
+ await deleteFile(pagePath)
+ } catch (err) {
+ console.error(`Failed to delete wiki page ${pagePath}:`, err)
+ }
+ }
+
+ // 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
+ }
+ }
+
+ // Refresh
+ await loadSources()
+ const tree = await listDirectory(project.path)
+ setFileTree(tree)
+
+ // Clear selected file if it was the deleted one
+ if (selectedFile === node.path || relatedPages.includes(selectedFile ?? "")) {
+ setSelectedFile(null)
+ }
+ } catch (err) {
+ console.error("Failed to delete source:", err)
+ window.alert(`Failed to delete: ${err}`)
+ }
+ }
+
async function handleIngest(node: FileNode) {
if (!project || ingestingPath) return
setIngestingPath(node.path)
@@ -177,6 +237,15 @@ export function SourcesView() {
>
<BookOpen className="h-4 w-4" />
</Button>
+ <Button
+ variant="ghost"
+ size="icon"
+ className="h-7 w-7 shrink-0 text-muted-foreground hover:text-destructive"
+ title="Delete source and related wiki pages"
+ onClick={() => handleDelete(source)}
+ >
+ <Trash2 className="h-3.5 w-3.5" />
+ </Button>
</div>
))}
</div>