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: support Org mode source ingestion

nash_su18dddc6e7e31
diff --git a/src-tauri/src/commands/fs.rs b/src-tauri/src/commands/fs.rs
index 421ee4c6..67d920b9 100644
--- a/src-tauri/src/commands/fs.rs
+++ b/src-tauri/src/commands/fs.rs
@@ -82,6 +82,7 @@ pub async fn read_file(path: String, extract_images: Option<bool>) -> Result<Str
match ext.as_str() {
"pdf" => extract_pdf_text(&path, include_images),
+ "org" => extract_org_text(&path),
e if OFFICE_EXTS.contains(&e) => extract_office_text(&path, e),
e if EBOOK_EXTS.contains(&e) => crate::commands::ebook::extract_ebook_text(&path, e),
e if IMAGE_EXTS.contains(&e) => {
@@ -134,6 +135,7 @@ pub async fn preprocess_file(path: String) -> Result<String, String> {
let text = match ext.as_str() {
"pdf" => extract_pdf_text(&path, false)?,
+ "org" => extract_org_text(&path)?,
e if OFFICE_EXTS.contains(&e) => extract_office_text(&path, e)?,
e if EBOOK_EXTS.contains(&e) => {
crate::commands::ebook::extract_ebook_text(&path, e)?
@@ -176,6 +178,135 @@ fn write_cache(original: &Path, text: &str) -> Result<(), String> {
fs::write(&cache_path, text).map_err(|e| format!("Failed to write cache: {}", e))
}
+/// Convert common Org syntax into Markdown-shaped text for ingestion and
+/// preview. The original `.org` file remains untouched in `raw/sources`.
+/// Source blocks are copied verbatim inside fences and are never executed.
+fn extract_org_text(path: &str) -> Result<String, String> {
+ let content = fs::read_to_string(path)
+ .map_err(|e| format!("Failed to read Org file '{}': {}", path, e))?;
+ Ok(org_to_markdown(&content))
+}
+
+fn org_to_markdown(content: &str) -> String {
+ let normalized = content.replace("\r\n", "\n").replace('\r', "\n");
+ let mut output = Vec::new();
+ let mut block_end: Option<&'static str> = None;
+
+ for line in normalized.lines() {
+ let trimmed = line.trim();
+ let upper = trimmed.to_ascii_uppercase();
+ if let Some(end) = block_end {
+ if upper == end {
+ output.push("```".to_string());
+ block_end = None;
+ } else {
+ output.push(line.to_string());
+ }
+ continue;
+ }
+
+ if upper.starts_with("#+BEGIN_SRC") {
+ let language = trimmed[11..].trim().split_whitespace().next().unwrap_or("");
+ output.push(format!("```{language}"));
+ block_end = Some("#+END_SRC");
+ continue;
+ }
+ if upper == "#+BEGIN_EXAMPLE" {
+ output.push("```text".to_string());
+ block_end = Some("#+END_EXAMPLE");
+ continue;
+ }
+ if upper == "#+BEGIN_QUOTE" {
+ output.push("```text".to_string());
+ block_end = Some("#+END_QUOTE");
+ continue;
+ }
+
+ if let Some((key, value)) = parse_org_keyword(trimmed) {
+ if key == "TITLE" {
+ output.push(format!("# {value}"));
+ } else if !matches!(key.as_str(), "OPTIONS" | "PROPERTY" | "SETUPFILE") {
+ output.push(format!("**{}:** {}", title_case_ascii(&key), value));
+ }
+ continue;
+ }
+
+ if let Some((level, heading)) = parse_org_heading(line) {
+ output.push(format!(
+ "{} {}",
+ "#".repeat(level.min(6)),
+ convert_org_links(heading)
+ ));
+ continue;
+ }
+
+ if trimmed.starts_with('|')
+ && trimmed.ends_with('|')
+ && trimmed.contains('+')
+ && trimmed
+ .chars()
+ .all(|c| matches!(c, '|' | '+' | '-' | ':' | ' '))
+ {
+ output.push(trimmed.replace('+', "|"));
+ continue;
+ }
+
+ output.push(convert_org_links(line));
+ }
+ if block_end.is_some() {
+ output.push("```".to_string());
+ }
+ output.join("\n")
+}
+
+fn parse_org_keyword(line: &str) -> Option<(String, &str)> {
+ let rest = line.strip_prefix("#+")?;
+ let (key, value) = rest.split_once(':')?;
+ if key.is_empty() || !key.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
+ return None;
+ }
+ Some((key.to_ascii_uppercase(), value.trim()))
+}
+
+fn parse_org_heading(line: &str) -> Option<(usize, &str)> {
+ let stars = line.chars().take_while(|c| *c == '*').count();
+ if stars == 0 || line.as_bytes().get(stars) != Some(&b' ') {
+ return None;
+ }
+ Some((stars, line[stars + 1..].trim()))
+}
+
+fn title_case_ascii(value: &str) -> String {
+ let lower = value.replace('_', " ").to_ascii_lowercase();
+ let mut chars = lower.chars();
+ chars
+ .next()
+ .map(|c| c.to_ascii_uppercase().to_string() + chars.as_str())
+ .unwrap_or_default()
+}
+
+fn convert_org_links(line: &str) -> String {
+ let mut out = String::with_capacity(line.len());
+ let mut rest = line;
+ while let Some(start) = rest.find("[[") {
+ out.push_str(&rest[..start]);
+ let after = &rest[start + 2..];
+ let Some(end) = after.find("]]") else {
+ out.push_str(&rest[start..]);
+ return out;
+ };
+ let inner = &after[..end];
+ if let Some((target, description)) = inner.split_once("][") {
+ out.push_str(&format!("[{}]({})", description, target));
+ } else {
+ out.push_str(&format!("<{}>", inner));
+ }
+ rest = &after[end + 2..];
+ }
+ out.push_str(rest);
+ out
+}
+
/// Global PDFium instance — the library prefers a single binding shared
/// across threads over repeatedly binding/unbinding.
static PDFIUM: std::sync::OnceLock<Result<pdfium_render::prelude::Pdfium, String>> =
@@ -1815,6 +1946,29 @@ mod tests {
use super::*;
use std::io::Write;
+ #[test]
+ fn org_to_markdown_preserves_common_elements_without_executing_source_blocks() {
+ let input = "#+TITLE: Research Notes\r\n#+AUTHOR: Ada\r\n* TODO Topic :rust:notes:\r\nParagraph with [[https://example.com][reference]] and [[file:local.pdf]].\r\n- [ ] Verify\r\n| Name | Value |\r\n|------+-------|\r\n| A | 1 |\r\n#+BEGIN_SRC sh\r\necho never-run\r\n#+END_SRC\r\n";
+ let output = org_to_markdown(input);
+ assert!(output.contains("# Research Notes"));
+ assert!(output.contains("**Author:** Ada"));
+ assert!(output.contains("# TODO Topic :rust:notes:"));
+ assert!(output.contains("[reference](https://example.com)"));
+ assert!(output.contains("<file:local.pdf>"));
+ assert!(output.contains("|------|-------|"));
+ assert!(output.contains("```sh\necho never-run\n```"));
+ }
+
+ #[test]
+ fn org_to_markdown_closes_unterminated_source_block_and_keeps_unknown_lines() {
+ let output = org_to_markdown(
+ "#+OPTIONS: toc:nil\n#+CUSTOM: kept\n* Heading\n#+BEGIN_SRC python\nprint('text only')",
+ );
+ assert!(!output.contains("OPTIONS"));
+ assert!(output.contains("**Custom:** kept"));
+ assert!(output.ends_with("```"));
+ }
+
/// Write `bytes` to a fresh tmp path with `.pdf` suffix and return
/// the path (the OS tmpdir is NOT cleaned up — acceptable for tests).
fn tmp_pdf_with_bytes(bytes: &[u8]) -> String {
diff --git a/src/components/sources/sources-view.tsx b/src/components/sources/sources-view.tsx
index 3cfdb881..9123eade 100644
--- a/src/components/sources/sources-view.tsx
+++ b/src/components/sources/sources-view.tsx
@@ -110,7 +110,7 @@ export function SourcesView() {
{
name: "Documents",
extensions: [
- "md", "mdx", "txt", "rtf", "pdf",
+ "md", "mdx", "txt", "org", "rtf", "pdf",
"html", "htm", "xml",
"doc", "docx", "xls", "xlsx", "ppt", "pptx",
"odt", "ods", "odp", "epub", "mobi", "pages", "numbers", "key",
diff --git a/src/lib/file-types.test.ts b/src/lib/file-types.test.ts
index c65ff8ce..78085493 100644
--- a/src/lib/file-types.test.ts
+++ b/src/lib/file-types.test.ts
@@ -14,12 +14,14 @@ describe("file types", () => {
expect(isExtractedTextPreviewFile("/project/raw/sources/sheet.xlsx")).toBe(true)
expect(isExtractedTextPreviewFile("/project/raw/sources/book.epub")).toBe(true)
expect(isExtractedTextPreviewFile("C:\\books\\book.MOBI")).toBe(true)
+ expect(isExtractedTextPreviewFile("C:\\notes\\journal.ORG")).toBe(true)
expect(isExtractedTextPreviewFile("/project/raw/sources/archive.zip")).toBe(false)
})
- it("classifies EPUB and MOBI as extracted documents", () => {
+ it("classifies EPUB, MOBI, and Org as extracted documents", () => {
expect(getFileCategory("/project/book.epub")).toBe("document")
expect(getFileCategory("C:\\books\\book.MOBI")).toBe("document")
+ expect(getFileCategory("/project/notes.org")).toBe("document")
})
it("extracts extensions from windows and unix paths", () => {
diff --git a/src/lib/file-types.ts b/src/lib/file-types.ts
index 10aee65a..b082dae3 100644
--- a/src/lib/file-types.ts
+++ b/src/lib/file-types.ts
@@ -117,6 +117,7 @@ const EXT_MAP: Record<string, FileCategory> = {
key: "document",
epub: "document",
mobi: "document",
+ org: "document",
// Data
json: "data",
@@ -149,6 +150,7 @@ export const EXTRACTED_TEXT_PREVIEW_EXTENSIONS = new Set([
"odp",
"epub",
"mobi",
+ "org",
])
export function getFileExtension(filePath: string): string {
diff --git a/src/lib/source-lifecycle.ts b/src/lib/source-lifecycle.ts
index 9ae6d025..946e1b68 100644
--- a/src/lib/source-lifecycle.ts
+++ b/src/lib/source-lifecycle.ts
@@ -63,6 +63,7 @@ export const INGESTABLE_SOURCE_EXTENSIONS = new Set([
"yml",
"epub",
"mobi",
+ "org",
])
function flattenFiles(nodes: FileNode[]): FileNode[] {
diff --git a/src/lib/source-watch-config.test.ts b/src/lib/source-watch-config.test.ts
index 22d12084..8ad2f95c 100644
--- a/src/lib/source-watch-config.test.ts
+++ b/src/lib/source-watch-config.test.ts
@@ -14,6 +14,7 @@ describe("source watch config", () => {
it("allows document types by default and rejects config/media/binaries", () => {
expect(isPathAllowedBySourceWatch("raw/sources/report.pdf", DEFAULT_SOURCE_WATCH_CONFIG)).toBe(true)
expect(isPathAllowedBySourceWatch("raw/sources/notes.md", DEFAULT_SOURCE_WATCH_CONFIG)).toBe(true)
+ expect(isPathAllowedBySourceWatch("raw/sources/notes.org", DEFAULT_SOURCE_WATCH_CONFIG)).toBe(true)
expect(isPathAllowedBySourceWatch("raw/sources/report.doc", DEFAULT_SOURCE_WATCH_CONFIG)).toBe(true)
expect(isPathAllowedBySourceWatch("raw/sources/secrets.json", DEFAULT_SOURCE_WATCH_CONFIG)).toBe(false)
expect(isPathAllowedBySourceWatch("raw/sources/video.mp4", DEFAULT_SOURCE_WATCH_CONFIG)).toBe(false)
diff --git a/src/lib/source-watch-config.ts b/src/lib/source-watch-config.ts
index 31003be3..2ac9abec 100644
--- a/src/lib/source-watch-config.ts
+++ b/src/lib/source-watch-config.ts
@@ -7,7 +7,7 @@ export const DEFAULT_SOURCE_WATCH_CONFIG: SourceWatchConfig = sourceWatchDefault
export const SOURCE_WATCH_FILE_TYPE_GROUPS = [
{
id: "documents",
- extensions: ["md", "mdx", "txt", "pdf", "doc", "docx", "odt", "rtf", "epub", "mobi"],
+ extensions: ["md", "mdx", "txt", "org", "pdf", "doc", "docx", "odt", "rtf", "epub", "mobi"],
},
{
id: "presentations",
diff --git a/src/lib/source-watch-defaults.json b/src/lib/source-watch-defaults.json
index d7c7f2a4..7ed0691c 100644
--- a/src/lib/source-watch-defaults.json
+++ b/src/lib/source-watch-defaults.json
@@ -5,6 +5,7 @@
"md",
"mdx",
"txt",
+ "org",
"pdf",
"doc",
"docx",