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: robust document text extraction for all common formats

nash_su26ddd1e5aec0
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock
index 70d5a7f0..f3f2c981 100644
--- a/src-tauri/Cargo.lock
+++ b/src-tauri/Cargo.lock
@@ -67,6 +67,15 @@ version = "1.0.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
+[[package]]
+name = "arbitrary"
+version = "1.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1"
+dependencies = [
+ "derive_arbitrary",
+]
+
[[package]]
name = "async-broadcast"
version = "0.7.2"
@@ -369,6 +378,25 @@ dependencies = [
"serde",
]
+[[package]]
+name = "bzip2"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49ecfb22d906f800d4fe833b6282cf4dc1c298f5057ca0b5445e5c209735ca47"
+dependencies = [
+ "bzip2-sys",
+]
+
+[[package]]
+name = "bzip2-sys"
+version = "0.1.13+1.0.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "225bff33b2141874fe80d71e07d6eec4f85c5c216453dd96388240f96e1acc14"
+dependencies = [
+ "cc",
+ "pkg-config",
+]
+
[[package]]
name = "cairo-rs"
version = "0.18.5"
@@ -452,6 +480,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7a4d3ec6524d28a329fc53654bbadc9bdd7b0431f5d65f1a56ffb28a1ee5283"
dependencies = [
"find-msvc-tools",
+ "jobserver",
+ "libc",
"shlex",
]
@@ -537,6 +567,12 @@ dependencies = [
"crossbeam-utils",
]
+[[package]]
+name = "constant_time_eq"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7c74b8349d32d297c9134b8c88677813a227df8f779daa29bfc29c183fe3dca6"
+
[[package]]
name = "convert_case"
version = "0.4.0"
@@ -602,6 +638,21 @@ dependencies = [
"libc",
]
+[[package]]
+name = "crc"
+version = "3.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5eb8a2a1cd12ab0d987a5d5e825195d372001a4094a0376319d5a0ad71c1ba0d"
+dependencies = [
+ "crc-catalog",
+]
+
+[[package]]
+name = "crc-catalog"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19d374276b40fb8bbdee95aef7c7fa6b5316ec764510eb64b8dd0e2ed0d7e7f5"
+
[[package]]
name = "crc32fast"
version = "1.5.0"
@@ -720,6 +771,12 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "deflate64"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac6b926516df9c60bfa16e107b21086399f8285a44ca9711344b9e553c5146e2"
+
[[package]]
name = "deranged"
version = "0.5.8"
@@ -730,6 +787,17 @@ dependencies = [
"serde_core",
]
+[[package]]
+name = "derive_arbitrary"
+version = "1.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e567bd82dcff979e4b03460c307b3cdc9e96fde3d73bed1496d2bc75d9dd62a"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
[[package]]
name = "derive_more"
version = "0.99.20"
@@ -772,6 +840,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
+ "subtle",
]
[[package]]
@@ -1350,9 +1419,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd"
dependencies = [
"cfg-if",
+ "js-sys",
"libc",
"r-efi 5.3.0",
"wasip2",
+ "wasm-bindgen",
]
[[package]]
@@ -1561,6 +1632,15 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
+[[package]]
+name = "hmac"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
+dependencies = [
+ "digest",
+]
+
[[package]]
name = "html5ever"
version = "0.29.1"
@@ -1970,6 +2050,16 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "jobserver"
+version = "0.1.34"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9afb3de4395d6b3e67a780b6de64b51c978ecf11cb9a462c66be7d4ca9039d33"
+dependencies = [
+ "getrandom 0.3.4",
+ "libc",
+]
+
[[package]]
name = "js-sys"
version = "0.3.94"
@@ -2107,6 +2197,7 @@ dependencies = [
"tauri-plugin-dialog",
"tauri-plugin-opener",
"tauri-plugin-store",
+ "zip",
]
[[package]]
@@ -2152,6 +2243,27 @@ dependencies = [
"weezl",
]
+[[package]]
+name = "lzma-rs"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "297e814c836ae64db86b36cf2a557ba54368d03f6afcd7d947c266692f71115e"
+dependencies = [
+ "byteorder",
+ "crc",
+]
+
+[[package]]
+name = "lzma-sys"
+version = "0.1.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5fda04ab3764e6cde78b9974eec4f779acaba7c4e84b36eca3cf77c581b85d27"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+]
+
[[package]]
name = "mac"
version = "0.1.1"
@@ -2590,6 +2702,16 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3"
+[[package]]
+name = "pbkdf2"
+version = "0.12.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2"
+dependencies = [
+ "digest",
+ "hmac",
+]
+
[[package]]
name = "pdf-extract"
version = "0.10.0"
@@ -3576,6 +3698,17 @@ dependencies = [
"stable_deref_trait",
]
+[[package]]
+name = "sha1"
+version = "0.10.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
+dependencies = [
+ "cfg-if",
+ "cpufeatures",
+ "digest",
+]
+
[[package]]
name = "sha2"
version = "0.10.9"
@@ -3763,6 +3896,12 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
+[[package]]
+name = "subtle"
+version = "2.6.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
+
[[package]]
name = "swift-rs"
version = "1.0.7"
@@ -5653,6 +5792,15 @@ dependencies = [
"pkg-config",
]
+[[package]]
+name = "xz2"
+version = "0.1.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "388c44dc09d76f1536602ead6d325eb532f5c122f17782bd57fb47baeeb767e2"
+dependencies = [
+ "lzma-sys",
+]
+
[[package]]
name = "yoke"
version = "0.8.2"
@@ -5778,6 +5926,26 @@ dependencies = [
"synstructure",
]
+[[package]]
+name = "zeroize"
+version = "1.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0"
+dependencies = [
+ "zeroize_derive",
+]
+
+[[package]]
+name = "zeroize_derive"
+version = "1.4.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85a5b4158499876c763cb03bc4e49185d3cccbabb15b33c627f7884f43db852e"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
[[package]]
name = "zerotrie"
version = "0.2.4"
@@ -5811,12 +5979,82 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "zip"
+version = "2.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fabe6324e908f85a1c52063ce7aa26b68dcb7eb6dbc83a2d148403c9bc3eba50"
+dependencies = [
+ "aes",
+ "arbitrary",
+ "bzip2",
+ "constant_time_eq",
+ "crc32fast",
+ "crossbeam-utils",
+ "deflate64",
+ "displaydoc",
+ "flate2",
+ "getrandom 0.3.4",
+ "hmac",
+ "indexmap 2.13.1",
+ "lzma-rs",
+ "memchr",
+ "pbkdf2",
+ "sha1",
+ "thiserror 2.0.18",
+ "time",
+ "xz2",
+ "zeroize",
+ "zopfli",
+ "zstd",
+]
+
[[package]]
name = "zmij"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
+[[package]]
+name = "zopfli"
+version = "0.8.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f05cd8797d63865425ff89b5c4a48804f35ba0ce8d125800027ad6017d2b5249"
+dependencies = [
+ "bumpalo",
+ "crc32fast",
+ "log",
+ "simd-adler32",
+]
+
+[[package]]
+name = "zstd"
+version = "0.13.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e91ee311a569c327171651566e07972200e76fcfe2242a4fa446149a3881c08a"
+dependencies = [
+ "zstd-safe",
+]
+
+[[package]]
+name = "zstd-safe"
+version = "7.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f49c4d5f0abb602a93fb8736af2a4f4dd9512e36f7f570d66e65ff867ed3b9d"
+dependencies = [
+ "zstd-sys",
+]
+
+[[package]]
+name = "zstd-sys"
+version = "2.0.16+zstd.1.5.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "91e19ebc2adc8f83e43039e79776e3fda8ca919132d68a1fed6a5faca2683748"
+dependencies = [
+ "cc",
+ "pkg-config",
+]
+
[[package]]
name = "zvariant"
version = "5.10.0"
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index 2be6cb23..c91712aa 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -27,6 +27,7 @@ chrono = { version = "0.4", features = ["clock"] }
tauri-plugin-dialog = "2.7.0"
pdf-extract = "0.10.0"
tauri-plugin-store = "2.4.2"
+zip = "2"
[profile.release]
codegen-units = 1
diff --git a/src-tauri/src/commands/fs.rs b/src-tauri/src/commands/fs.rs
index c461169d..32271599 100644
--- a/src-tauri/src/commands/fs.rs
+++ b/src-tauri/src/commands/fs.rs
@@ -1,8 +1,20 @@
use std::fs;
+use std::io::Read as IoRead;
use std::path::Path;
use crate::types::wiki::FileNode;
+/// Known binary formats that need special extraction
+const OFFICE_EXTS: &[&str] = &["docx", "pptx", "xlsx", "odt", "ods", "odp"];
+const IMAGE_EXTS: &[&str] = &[
+ "png", "jpg", "jpeg", "gif", "webp", "bmp", "ico", "tiff", "tif", "avif", "heic", "heif", "svg",
+];
+const MEDIA_EXTS: &[&str] = &[
+ "mp4", "webm", "mov", "avi", "mkv", "flv", "wmv", "m4v",
+ "mp3", "wav", "ogg", "flac", "aac", "m4a", "wma",
+];
+const LEGACY_DOC_EXTS: &[&str] = &["doc", "xls", "ppt", "pages", "numbers", "key", "epub"];
+
#[tauri::command]
pub fn read_file(path: String) -> Result<String, String> {
let p = Path::new(&path);
@@ -12,21 +24,41 @@ pub fn read_file(path: String) -> Result<String, String> {
.unwrap_or("")
.to_lowercase();
+ // Check cache first for any extractable format
+ if let Some(cached) = read_cache(p) {
+ return Ok(cached);
+ }
+
match ext.as_str() {
- "pdf" => {
- // Check for pre-processed cache first
- if let Some(cached) = read_cache(p) {
- return Ok(cached);
+ "pdf" => extract_pdf_text(&path),
+ e if OFFICE_EXTS.contains(&e) => extract_office_text(&path, e),
+ e if IMAGE_EXTS.contains(&e) => {
+ let size = fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
+ Ok(format!("[Image: {} ({:.1} KB)]", p.file_name().unwrap_or_default().to_string_lossy(), size as f64 / 1024.0))
+ }
+ e if MEDIA_EXTS.contains(&e) => {
+ let size = fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
+ Ok(format!("[Media: {} ({:.1} MB)]", p.file_name().unwrap_or_default().to_string_lossy(), size as f64 / 1048576.0))
+ }
+ e if LEGACY_DOC_EXTS.contains(&e) => {
+ Ok(format!("[Document: {} — text extraction not supported for .{} format]",
+ p.file_name().unwrap_or_default().to_string_lossy(), e))
+ }
+ _ => {
+ // Try reading as text; if it fails (binary), return a friendly message
+ match fs::read_to_string(&path) {
+ Ok(content) => Ok(content),
+ Err(_) => {
+ let size = fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
+ Ok(format!("[Binary file: {} ({:.1} KB)]",
+ p.file_name().unwrap_or_default().to_string_lossy(), size as f64 / 1024.0))
+ }
}
- extract_pdf_text(&path)
}
- _ => fs::read_to_string(&path)
- .map_err(|e| format!("Failed to read file '{}': {}", path, e)),
}
}
/// Pre-process a file and cache the extracted text.
-/// Called at import time so previews are instant later.
#[tauri::command]
pub fn preprocess_file(path: String) -> Result<String, String> {
let p = Path::new(&path);
@@ -38,17 +70,11 @@ pub fn preprocess_file(path: String) -> Result<String, String> {
let text = match ext.as_str() {
"pdf" => extract_pdf_text(&path)?,
+ e if OFFICE_EXTS.contains(&e) => extract_office_text(&path, e)?,
_ => return Ok("no preprocessing needed".to_string()),
};
- // Write cache
- let cache_path = cache_path_for(p);
- if let Some(parent) = cache_path.parent() {
- fs::create_dir_all(parent).ok();
- }
- fs::write(&cache_path, &text)
- .map_err(|e| format!("Failed to write cache: {}", e))?;
-
+ write_cache(p, &text)?;
Ok(text)
}
@@ -64,7 +90,6 @@ fn cache_path_for(original: &Path) -> std::path::PathBuf {
fn read_cache(original: &Path) -> Option<String> {
let cache_path = cache_path_for(original);
- // Only use cache if it's newer than the original
let original_modified = fs::metadata(original).ok()?.modified().ok()?;
let cache_modified = fs::metadata(&cache_path).ok()?.modified().ok()?;
if cache_modified >= original_modified {
@@ -74,6 +99,15 @@ fn read_cache(original: &Path) -> Option<String> {
}
}
+fn write_cache(original: &Path, text: &str) -> Result<(), String> {
+ let cache_path = cache_path_for(original);
+ if let Some(parent) = cache_path.parent() {
+ fs::create_dir_all(parent).ok();
+ }
+ fs::write(&cache_path, text)
+ .map_err(|e| format!("Failed to write cache: {}", e))
+}
+
fn extract_pdf_text(path: &str) -> Result<String, String> {
let bytes =
fs::read(path).map_err(|e| format!("Failed to read PDF '{}': {}", path, e))?;
@@ -81,6 +115,99 @@ fn extract_pdf_text(path: &str) -> Result<String, String> {
.map_err(|e| format!("Failed to extract text from PDF '{}': {}", path, e))
}
+/// Extract text from Office Open XML formats (docx, pptx, xlsx) and OpenDocument formats.
+/// These are ZIP archives containing XML files with the actual content.
+fn extract_office_text(path: &str, ext: &str) -> Result<String, String> {
+ let file = fs::File::open(path)
+ .map_err(|e| format!("Failed to open '{}': {}", path, e))?;
+ let mut archive = zip::ZipArchive::new(file)
+ .map_err(|e| format!("Failed to read ZIP archive '{}': {}", path, e))?;
+
+ let xml_paths: Vec<&str> = match ext {
+ "docx" => vec!["word/document.xml"],
+ "pptx" => {
+ // PPTX has slide1.xml, slide2.xml, etc.
+ let names: Vec<String> = (0..archive.len())
+ .filter_map(|i| archive.by_index(i).ok().map(|f| f.name().to_string()))
+ .filter(|n| n.starts_with("ppt/slides/slide") && n.ends_with(".xml"))
+ .collect();
+ return extract_xml_text_from_paths(&mut archive, &names);
+ }
+ "xlsx" => vec!["xl/sharedStrings.xml"],
+ "odt" | "ods" | "odp" => vec!["content.xml"],
+ _ => vec![],
+ };
+
+ extract_xml_text_from_paths(&mut archive, &xml_paths.iter().map(|s| s.to_string()).collect::<Vec<_>>())
+}
+
+fn extract_xml_text_from_paths(
+ archive: &mut zip::ZipArchive<fs::File>,
+ paths: &[String],
+) -> Result<String, String> {
+ let mut all_text = Vec::new();
+
+ for xml_path in paths {
+ let mut file = match archive.by_name(xml_path) {
+ Ok(f) => f,
+ Err(_) => continue,
+ };
+
+ let mut xml_content = String::new();
+ file.read_to_string(&mut xml_content)
+ .map_err(|e| format!("Failed to read XML '{}': {}", xml_path, e))?;
+
+ // Strip XML tags, keep text content
+ let text = strip_xml_tags(&xml_content);
+ let cleaned = text
+ .lines()
+ .map(|l| l.trim())
+ .filter(|l| !l.is_empty())
+ .collect::<Vec<_>>()
+ .join("\n");
+
+ if !cleaned.is_empty() {
+ all_text.push(cleaned);
+ }
+ }
+
+ if all_text.is_empty() {
+ let fname = paths.first().map(|s| s.as_str()).unwrap_or("unknown");
+ Ok(format!("[Could not extract text from this file (tried {})]", fname))
+ } else {
+ Ok(all_text.join("\n\n"))
+ }
+}
+
+/// Simple XML tag stripper — removes all tags and decodes basic entities.
+fn strip_xml_tags(xml: &str) -> String {
+ let mut result = String::with_capacity(xml.len());
+ let mut in_tag = false;
+
+ for ch in xml.chars() {
+ match ch {
+ '<' => in_tag = true,
+ '>' => {
+ in_tag = false;
+ // Add space after closing tags to separate words
+ result.push(' ');
+ }
+ _ if !in_tag => result.push(ch),
+ _ => {}
+ }
+ }
+
+ // Decode common XML entities
+ result
+ .replace("&amp;", "&")
+ .replace("&lt;", "<")
+ .replace("&gt;", ">")
+ .replace("&quot;", "\"")
+ .replace("&apos;", "'")
+ .replace("&#10;", "\n")
+ .replace("&#13;", "")
+}
+
#[tauri::command]
pub fn write_file(path: String, contents: String) -> Result<(), String> {
let p = Path::new(&path);