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: use proper parsing libraries for DOCX and XLSX

nash_sua968fd5ede03
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock
index f3f2c981..b8cd2eac 100644
--- a/src-tauri/Cargo.lock
+++ b/src-tauri/Cargo.lock
@@ -230,6 +230,15 @@ dependencies = [
"system-deps",
]
+[[package]]
+name = "atoi_simd"
+version = "0.17.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ad17c7c205c2c28b527b9845eeb91cf1b4d008b438f98ce0e628227a822758e"
+dependencies = [
+ "debug_unsafe",
+]
+
[[package]]
name = "atomic-waker"
version = "1.1.2"
@@ -369,6 +378,12 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
+[[package]]
+name = "byteorder-lite"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
+
[[package]]
name = "bytes"
version = "1.11.1"
@@ -422,6 +437,23 @@ dependencies = [
"system-deps",
]
+[[package]]
+name = "calamine"
+version = "0.34.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "20ae05a4e39297eecf9a994210d27501318c37a9318201f8e11050add82bb6f0"
+dependencies = [
+ "atoi_simd",
+ "byteorder",
+ "codepage",
+ "encoding_rs",
+ "fast-float2",
+ "log",
+ "quick-xml 0.39.2",
+ "serde",
+ "zip 7.2.0",
+]
+
[[package]]
name = "camino"
version = "1.2.2"
@@ -548,6 +580,21 @@ dependencies = [
"inout",
]
+[[package]]
+name = "codepage"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "48f68d061bc2828ae826206326e61251aca94c1e4a5305cf52d9138639c918b4"
+dependencies = [
+ "encoding_rs",
+]
+
+[[package]]
+name = "color_quant"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
+
[[package]]
name = "combine"
version = "4.6.7"
@@ -677,6 +724,12 @@ version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
+[[package]]
+name = "crunchy"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5"
+
[[package]]
name = "crypto-common"
version = "0.1.7"
@@ -771,6 +824,12 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "debug_unsafe"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7eed2c4702fa172d1ce21078faa7c5203e69f5394d48cc436d25928394a867a2"
+
[[package]]
name = "deflate64"
version = "0.1.12"
@@ -910,6 +969,21 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "docx-rs"
+version = "0.4.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed73cbf5e1c37baa23f4132569ac1187829f03922c206bd68fe109e3001a343d"
+dependencies = [
+ "base64 0.22.1",
+ "image",
+ "quick-xml 0.36.2",
+ "serde",
+ "serde_json",
+ "thiserror 2.0.18",
+ "zip 0.6.6",
+]
+
[[package]]
name = "dom_query"
version = "0.27.0"
@@ -1083,12 +1157,38 @@ dependencies = [
"pin-project-lite",
]
+[[package]]
+name = "fast-float2"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8eb564c5c7423d25c886fb561d1e4ee69f72354d16918afa32c08811f6b6a55"
+
[[package]]
name = "fastrand"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a043dc74da1e37d6afe657061213aa6f425f855399a11d3463c6ecccc4dfda1f"
+[[package]]
+name = "fax"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f05de7d48f37cd6730705cbca900770cab77a89f413d23e100ad7fad7795a0ab"
+dependencies = [
+ "fax_derive",
+]
+
+[[package]]
+name = "fax_derive"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a0aca10fb742cb43f9e7bb8467c91aa9bcb8e3ffbc6a6f7389bb93ffc920577d"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.117",
+]
+
[[package]]
name = "fdeflate"
version = "0.3.7"
@@ -1122,6 +1222,7 @@ checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"
dependencies = [
"crc32fast",
"miniz_oxide",
+ "zlib-rs",
]
[[package]]
@@ -1439,6 +1540,16 @@ dependencies = [
"wasip3",
]
+[[package]]
+name = "gif"
+version = "0.14.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f5df2ba84018d80c213569363bdcd0c64e6933c67fe4c1d60ecf822971a3c35e"
+dependencies = [
+ "color_quant",
+ "weezl",
+]
+
[[package]]
name = "gio"
version = "0.18.4"
@@ -1587,6 +1698,17 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "half"
+version = "2.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b"
+dependencies = [
+ "cfg-if",
+ "crunchy",
+ "zerocopy",
+]
+
[[package]]
name = "hashbrown"
version = "0.12.3"
@@ -1782,7 +1904,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e795dff5605e0f04bff85ca41b51a96b83e80b281e96231bcaaf1ac35103371"
dependencies = [
"byteorder",
- "png",
+ "png 0.17.16",
]
[[package]]
@@ -1900,6 +2022,24 @@ dependencies = [
"icu_properties",
]
+[[package]]
+name = "image"
+version = "0.25.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85ab80394333c02fe689eaf900ab500fbd0c2213da414687ebf995a65d5a6104"
+dependencies = [
+ "bytemuck",
+ "byteorder-lite",
+ "color_quant",
+ "gif",
+ "moxcms",
+ "num-traits",
+ "png 0.18.1",
+ "tiff",
+ "zune-core",
+ "zune-jpeg",
+]
+
[[package]]
name = "indexmap"
version = "1.9.3"
@@ -2188,7 +2328,9 @@ checksum = "92daf443525c4cce67b150400bc2316076100ce0b3686209eb8cf3c31612e6f0"
name = "llm-wiki"
version = "0.1.0"
dependencies = [
+ "calamine",
"chrono",
+ "docx-rs",
"pdf-extract",
"serde",
"serde_json",
@@ -2197,7 +2339,7 @@ dependencies = [
"tauri-plugin-dialog",
"tauri-plugin-opener",
"tauri-plugin-store",
- "zip",
+ "zip 2.4.2",
]
[[package]]
@@ -2364,6 +2506,16 @@ dependencies = [
"windows-sys 0.61.2",
]
+[[package]]
+name = "moxcms"
+version = "0.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb85c154ba489f01b25c0d36ae69a87e4a1c73a72631fc6c0eb6dde34a73e44b"
+dependencies = [
+ "num-traits",
+ "pxfm",
+]
+
[[package]]
name = "muda"
version = "0.17.2"
@@ -2379,7 +2531,7 @@ dependencies = [
"objc2-core-foundation",
"objc2-foundation",
"once_cell",
- "png",
+ "png 0.17.16",
"serde",
"thiserror 2.0.18",
"windows-sys 0.60.2",
@@ -2953,7 +3105,7 @@ checksum = "740ebea15c5d1428f910cd1a5f52cebf8d25006245ed8ade92702f4943d91e07"
dependencies = [
"base64 0.22.1",
"indexmap 2.13.1",
- "quick-xml",
+ "quick-xml 0.38.4",
"serde",
"time",
]
@@ -2971,6 +3123,19 @@ dependencies = [
"miniz_oxide",
]
+[[package]]
+name = "png"
+version = "0.18.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60769b8b31b2a9f263dae2776c37b1b28ae246943cf719eb6946a1db05128a61"
+dependencies = [
+ "bitflags 2.11.0",
+ "crc32fast",
+ "fdeflate",
+ "flate2",
+ "miniz_oxide",
+]
+
[[package]]
name = "polling"
version = "3.11.0"
@@ -3105,6 +3270,28 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "pxfm"
+version = "0.1.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5a041e753da8b807c9255f28de81879c78c876392ff2469cde94799b2896b9d"
+
+[[package]]
+name = "quick-error"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3"
+
+[[package]]
+name = "quick-xml"
+version = "0.36.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f7649a7b4df05aed9ea7ec6f628c67c9953a43869b8bc50929569b2999d443fe"
+dependencies = [
+ "encoding_rs",
+ "memchr",
+]
+
[[package]]
name = "quick-xml"
version = "0.38.4"
@@ -3114,6 +3301,16 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "quick-xml"
+version = "0.39.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "958f21e8e7ceb5a1aa7fa87fab28e7c75976e0bfe7e23ff069e0a260f894067d"
+dependencies = [
+ "encoding_rs",
+ "memchr",
+]
+
[[package]]
name = "quote"
version = "1.0.45"
@@ -4108,7 +4305,7 @@ dependencies = [
"ico",
"json-patch",
"plist",
- "png",
+ "png 0.17.16",
"proc-macro2",
"quote",
"semver",
@@ -4409,6 +4606,20 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "tiff"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b63feaf3343d35b6ca4d50483f94843803b0f51634937cc2ec519fc32232bc52"
+dependencies = [
+ "fax",
+ "flate2",
+ "half",
+ "quick-error",
+ "weezl",
+ "zune-jpeg",
+]
+
[[package]]
name = "time"
version = "0.3.47"
@@ -4701,7 +4912,7 @@ dependencies = [
"objc2-core-graphics",
"objc2-foundation",
"once_cell",
- "png",
+ "png 0.17.16",
"serde",
"thiserror 2.0.18",
"windows-sys 0.60.2",
@@ -4728,6 +4939,12 @@ dependencies = [
"pom",
]
+[[package]]
+name = "typed-path"
+version = "0.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e28f89b80c87b8fb0cf04ab448d5dd0dd0ade2f8891bae878de66a75a28600e"
+
[[package]]
name = "typeid"
version = "1.0.3"
@@ -5979,6 +6196,18 @@ dependencies = [
"syn 2.0.117",
]
+[[package]]
+name = "zip"
+version = "0.6.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "760394e246e4c28189f19d488c058bf16f564016aefac5d32bb1f3b51d5e9261"
+dependencies = [
+ "byteorder",
+ "crc32fast",
+ "crossbeam-utils",
+ "flate2",
+]
+
[[package]]
name = "zip"
version = "2.4.2"
@@ -6009,6 +6238,26 @@ dependencies = [
"zstd",
]
+[[package]]
+name = "zip"
+version = "7.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c42e33efc22a0650c311c2ef19115ce232583abbe80850bc8b66509ebef02de0"
+dependencies = [
+ "crc32fast",
+ "flate2",
+ "indexmap 2.13.1",
+ "memchr",
+ "typed-path",
+ "zopfli",
+]
+
+[[package]]
+name = "zlib-rs"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3be3d40e40a133f9c916ee3f9f4fa2d9d63435b5fbe1bfc6d9dae0aa0ada1513"
+
[[package]]
name = "zmij"
version = "1.0.21"
@@ -6055,6 +6304,21 @@ dependencies = [
"pkg-config",
]
+[[package]]
+name = "zune-core"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cb8a0807f7c01457d0379ba880ba6322660448ddebc890ce29bb64da71fb40f9"
+
+[[package]]
+name = "zune-jpeg"
+version = "0.5.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "27bc9d5b815bc103f142aa054f561d9187d191692ec7c2d1e2b4737f8dbd7296"
+dependencies = [
+ "zune-core",
+]
+
[[package]]
name = "zvariant"
version = "5.10.0"
diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml
index c91712aa..4ac78f8a 100644
--- a/src-tauri/Cargo.toml
+++ b/src-tauri/Cargo.toml
@@ -28,6 +28,8 @@ tauri-plugin-dialog = "2.7.0"
pdf-extract = "0.10.0"
tauri-plugin-store = "2.4.2"
zip = "2"
+calamine = "0.34.0"
+docx-rs = "0.4.20"
[profile.release]
codegen-units = 1
diff --git a/src-tauri/src/commands/fs.rs b/src-tauri/src/commands/fs.rs
index 2b1d4a92..5b658cd1 100644
--- a/src-tauri/src/commands/fs.rs
+++ b/src-tauri/src/commands/fs.rs
@@ -2,6 +2,8 @@ use std::fs;
use std::io::Read as IoRead;
use std::path::Path;
+use calamine::{Reader, open_workbook_auto, Data};
+
use crate::types::wiki::FileNode;
/// Known binary formats that need special extraction
@@ -117,20 +119,156 @@ fn extract_pdf_text(path: &str) -> Result<String, String> {
/// Extract text from Office Open XML formats, converting to Markdown.
fn extract_office_text(path: &str, ext: &str) -> Result<String, String> {
+ // Spreadsheets: use calamine (supports xlsx, xls, ods)
+ if matches!(ext, "xlsx" | "xls" | "ods") {
+ return extract_spreadsheet(path);
+ }
+
+ // DOCX: use docx-rs library for proper parsing
+ if ext == "docx" {
+ return extract_docx_with_library(path);
+ }
+
+ // PPTX and ODF: use ZIP-based parsing
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))?;
match ext {
- "docx" => extract_docx_markdown(&mut archive),
"pptx" => extract_pptx_markdown(&mut archive),
- "xlsx" => extract_xlsx_markdown(&mut archive),
- "odt" | "ods" | "odp" => extract_odf_text(&mut archive),
+ "odt" | "odp" => extract_odf_text(&mut archive),
_ => Ok("[Unsupported format]".to_string()),
}
}
+/// Extract DOCX using docx-rs library for proper structural parsing.
+fn extract_docx_with_library(path: &str) -> Result<String, String> {
+ let bytes = fs::read(path).map_err(|e| format!("Failed to read DOCX '{}': {}", path, e))?;
+ let docx = docx_rs::read_docx(&bytes)
+ .map_err(|e| format!("Failed to parse DOCX '{}': {:?}", path, e))?;
+
+ let mut result = String::new();
+
+ for child in docx.document.children {
+ match child {
+ docx_rs::DocumentChild::Paragraph(para) => {
+ let mut para_text = String::new();
+ let mut is_heading = false;
+ let mut heading_level: u8 = 1;
+
+ // Check paragraph style for headings
+ if let Some(style) = &para.property.style {
+ let style_val = &style.val;
+ if style_val.contains("Heading") || style_val.contains("heading") {
+ is_heading = true;
+ // Extract level number
+ for ch in style_val.chars() {
+ if ch.is_ascii_digit() {
+ heading_level = ch.to_digit(10).unwrap_or(1) as u8;
+ break;
+ }
+ }
+ }
+ }
+
+ // Check for list (numbering)
+ let is_list = para.property.numbering_property.is_some();
+
+ // Extract text from runs
+ for child in &para.children {
+ if let docx_rs::ParagraphChild::Run(run) = child {
+ let is_bold = run.run_property.bold.is_some();
+ let is_italic = run.run_property.italic.is_some();
+
+ for run_child in &run.children {
+ if let docx_rs::RunChild::Text(text) = run_child {
+ let t = &text.text;
+ if is_bold && is_italic {
+ para_text.push_str(&format!("***{}***", t));
+ } else if is_bold {
+ para_text.push_str(&format!("**{}**", t));
+ } else if is_italic {
+ para_text.push_str(&format!("*{}*", t));
+ } else {
+ para_text.push_str(t);
+ }
+ }
+ }
+ }
+ }
+
+ let text = para_text.trim().to_string();
+ if text.is_empty() { continue; }
+
+ if is_heading {
+ let prefix = "#".repeat(heading_level as usize);
+ result.push_str(&format!("{} {}\n\n", prefix, text));
+ } else if is_list {
+ result.push_str(&format!("- {}\n", text));
+ } else {
+ result.push_str(&text);
+ result.push_str("\n\n");
+ }
+ }
+ docx_rs::DocumentChild::Table(table) => {
+ let mut rows: Vec<Vec<String>> = Vec::new();
+ for row in &table.rows {
+ if let docx_rs::TableChild::TableRow(tr) = row {
+ let mut cells: Vec<String> = Vec::new();
+ for cell in &tr.cells {
+ if let docx_rs::TableRowChild::TableCell(tc) = cell {
+ let mut cell_text = String::new();
+ for child in &tc.children {
+ if let docx_rs::TableCellContent::Paragraph(para) = child {
+ for pchild in &para.children {
+ if let docx_rs::ParagraphChild::Run(run) = pchild {
+ for rc in &run.children {
+ if let docx_rs::RunChild::Text(t) = rc {
+ cell_text.push_str(&t.text);
+ }
+ }
+ }
+ }
+ }
+ }
+ cells.push(cell_text.trim().replace('|', "\\|"));
+ }
+ }
+ rows.push(cells);
+ }
+ }
+ if !rows.is_empty() {
+ let max_cols = rows.iter().map(|r| r.len()).max().unwrap_or(0);
+ for (i, row) in rows.iter().enumerate() {
+ let mut padded = row.clone();
+ padded.resize(max_cols, String::new());
+ result.push_str("| ");
+ result.push_str(&padded.join(" | "));
+ result.push_str(" |\n");
+ if i == 0 {
+ result.push('|');
+ for _ in 0..max_cols { result.push_str(" --- |"); }
+ result.push('\n');
+ }
+ }
+ result.push('\n');
+ }
+ }
+ _ => {}
+ }
+ }
+
+ if result.trim().is_empty() {
+ // Fallback to ZIP-based extraction
+ let file = fs::File::open(path).map_err(|e| e.to_string())?;
+ let mut archive = zip::ZipArchive::new(file).map_err(|e| e.to_string())?;
+ extract_docx_markdown(&mut archive)
+ } else {
+ Ok(result)
+ }
+}
+
fn read_zip_file(archive: &mut zip::ZipArchive<fs::File>, name: &str) -> Option<String> {
let mut file = archive.by_name(name).ok()?;
let mut content = String::new();
@@ -403,131 +541,82 @@ fn extract_pptx_markdown(archive: &mut zip::ZipArchive<fs::File>) -> Result<Stri
}
}
-/// Extract XLSX to Markdown tables.
-fn extract_xlsx_markdown(archive: &mut zip::ZipArchive<fs::File>) -> Result<String, String> {
- // Read shared strings first
- let shared_strings: Vec<String> = match read_zip_file(archive, "xl/sharedStrings.xml") {
- Some(xml) => {
- let mut strings = Vec::new();
- let mut in_t = false;
- let mut current = String::new();
- for ch in xml.chars() {
- if in_t {
- if ch == '<' {
- strings.push(decode_xml_entities(&current));
- current.clear();
- in_t = false;
- } else {
- current.push(ch);
- }
- }
- // Detect <t> or <t ...>
- if !in_t && xml[strings.len()..].contains("<t") {
- // simplified: just look for <t> tags
- }
- }
- // Better approach: regex-like extraction
- let mut result_strings = Vec::new();
- let parts: Vec<&str> = xml.split("<t").collect();
- for part in parts.iter().skip(1) {
- if let Some(end) = part.find("</t>") {
- let text_part = if let Some(start) = part.find('>') {
- &part[start + 1..end]
- } else {
- &part[..end]
- };
- result_strings.push(decode_xml_entities(text_part));
- }
- }
- result_strings
- }
- None => Vec::new(),
- };
+/// Extract XLSX/XLS/ODS to Markdown tables using calamine.
+fn extract_xlsx_markdown(_archive: &mut zip::ZipArchive<fs::File>) -> Result<String, String> {
+ // calamine needs the file path, not the archive
+ Err("Use extract_spreadsheet instead".to_string())
+}
- // Read sheet1
- let sheet_xml = read_zip_file(archive, "xl/worksheets/sheet1.xml")
- .ok_or_else(|| "No sheet1.xml found".to_string())?;
+/// Extract spreadsheet to Markdown using calamine (supports xlsx, xls, ods).
+fn extract_spreadsheet(path: &str) -> Result<String, String> {
+ let mut workbook = open_workbook_auto(path)
+ .map_err(|e| format!("Failed to open spreadsheet '{}': {}", path, e))?;
- let mut rows: Vec<Vec<String>> = Vec::new();
- let mut current_row: Vec<String> = Vec::new();
- let mut cell_type = String::new();
- let mut cell_value = String::new();
- let mut in_row = false;
- let mut in_cell = false;
- let mut in_value = false;
-
- let parts: Vec<&str> = sheet_xml.split('<').collect();
- for part in parts {
- if part.starts_with("row ") || part.starts_with("row>") {
- in_row = true;
- current_row.clear();
- } else if part.starts_with("/row>") {
- if !current_row.is_empty() {
- rows.push(current_row.clone());
- }
- in_row = false;
- } else if in_row && (part.starts_with("c ") || part.starts_with("c>")) {
- in_cell = true;
- cell_type.clear();
- cell_value.clear();
- if part.contains("t=\"s\"") {
- cell_type = "s".to_string();
- }
- } else if part.starts_with("/c>") {
- if cell_type == "s" {
- if let Ok(idx) = cell_value.trim().parse::<usize>() {
- current_row.push(shared_strings.get(idx).cloned().unwrap_or_default());
- } else {
- current_row.push(cell_value.trim().to_string());
- }
- } else {
- current_row.push(cell_value.trim().to_string());
- }
- in_cell = false;
- } else if in_cell && part.starts_with("v>") {
- in_value = true;
- cell_value.clear();
- } else if in_cell && part.starts_with("v ") {
- // <v> with attributes
- if let Some(pos) = part.find('>') {
- cell_value = part[pos + 1..].trim_end_matches('/').to_string();
- }
- } else if part.starts_with("/v>") {
- in_value = false;
- } else if in_value {
- // text content between tags
- if let Some(pos) = part.find('>') {
- cell_value.push_str(&part[pos + 1..]);
+ let mut result = String::new();
+ let sheet_names = workbook.sheet_names().to_vec();
+
+ for sheet_name in &sheet_names {
+ if let Ok(range) = workbook.worksheet_range(sheet_name) {
+ if range.is_empty() { continue; }
+
+ if sheet_names.len() > 1 {
+ result.push_str(&format!("## {}\n\n", sheet_name));
}
- }
- }
- if rows.is_empty() {
- return Ok("[Could not extract data from XLSX]".to_string());
- }
+ let mut rows: Vec<Vec<String>> = Vec::new();
+ let mut max_cols = 0;
+
+ for row in range.rows() {
+ let cells: Vec<String> = row.iter().map(|cell| {
+ match cell {
+ Data::Empty => String::new(),
+ Data::String(s) => s.clone(),
+ Data::Float(f) => {
+ if *f == (*f as i64) as f64 {
+ format!("{}", *f as i64)
+ } else {
+ format!("{:.2}", f)
+ }
+ }
+ Data::Int(i) => i.to_string(),
+ Data::Bool(b) => b.to_string(),
+ Data::DateTime(dt) => format!("{}", dt),
+ Data::DateTimeIso(s) => s.clone(),
+ Data::DurationIso(s) => s.clone(),
+ Data::Error(e) => format!("ERR:{:?}", e),
+ }
+ }).collect();
+ if cells.len() > max_cols { max_cols = cells.len(); }
+ rows.push(cells);
+ }
- // Convert to Markdown table
- let mut result = String::new();
- let max_cols = rows.iter().map(|r| r.len()).max().unwrap_or(0);
-
- for (i, row) in rows.iter().enumerate() {
- let mut padded = row.clone();
- padded.resize(max_cols, String::new());
- result.push_str("| ");
- result.push_str(&padded.join(" | "));
- result.push_str(" |\n");
-
- // Header separator after first row
- if i == 0 {
- result.push('|');
- for _ in 0..max_cols {
- result.push_str(" --- |");
+ // Skip empty sheets
+ if rows.is_empty() || max_cols == 0 { continue; }
+
+ for (i, row) in rows.iter().enumerate() {
+ let mut padded = row.clone();
+ padded.resize(max_cols, String::new());
+ // Escape pipe characters in cell values
+ let escaped: Vec<String> = padded.iter().map(|c| c.replace('|', "\\|")).collect();
+ result.push_str("| ");
+ result.push_str(&escaped.join(" | "));
+ result.push_str(" |\n");
+
+ if i == 0 {
+ result.push('|');
+ for _ in 0..max_cols { result.push_str(" --- |"); }
+ result.push('\n');
+ }
}
result.push('\n');
}
}
- Ok(result)
+ if result.trim().is_empty() {
+ Ok("[Could not extract data from spreadsheet]".to_string())
+ } else {
+ Ok(result)
+ }
}
/// Extract OpenDocument format text (basic).