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: LaTeX support in file preview + editor, extracted shared module

nash_su275d64fa4481
diff --git a/src/components/chat/chat-message.tsx b/src/components/chat/chat-message.tsx
index 01604058..0719806d 100644
--- a/src/components/chat/chat-message.tsx
+++ b/src/components/chat/chat-message.tsx
@@ -11,58 +11,7 @@ import { lastQueryPages } from "@/components/chat/chat-panel"
import type { DisplayMessage } from "@/stores/chat-store"
import type { FileNode } from "@/types/wiki"
-// Comprehensive LaTeX command → Unicode mapping
-const LATEX_TO_UNICODE: Record<string, string> = {
- // Arrows
- rightarrow: "→", Rightarrow: "⇒", longrightarrow: "⟶",
- leftarrow: "←", Leftarrow: "⇐", longleftarrow: "⟵",
- leftrightarrow: "↔", Leftrightarrow: "⇔",
- uparrow: "↑", Uparrow: "⇑", downarrow: "↓", Downarrow: "⇓",
- mapsto: "↦", hookrightarrow: "↪", hookleftarrow: "↩",
- nearrow: "↗", nwarrow: "↖", searrow: "↘", swarrow: "↙",
- // Math operators
- times: "×", div: "÷", pm: "±", mp: "∓", cdot: "·", ast: "∗",
- star: "⋆", circ: "∘", bullet: "•", oplus: "⊕", otimes: "⊗",
- // Relations
- leq: "≤", le: "≤", geq: "≥", ge: "≥", neq: "≠", ne: "≠",
- approx: "≈", sim: "∼", simeq: "≃", equiv: "≡", cong: "≅",
- propto: "∝", ll: "≪", gg: "≫", prec: "≺", succ: "≻",
- subset: "⊂", supset: "⊃", subseteq: "⊆", supseteq: "⊇",
- in: "∈", notin: "∉", ni: "∋", cap: "∩", cup: "∪",
- vee: "∨", wedge: "∧", neg: "¬", lnot: "¬",
- // Greek lowercase
- alpha: "α", beta: "β", gamma: "γ", delta: "δ", epsilon: "ε",
- varepsilon: "ε", zeta: "ζ", eta: "η", theta: "θ", vartheta: "ϑ",
- iota: "ι", kappa: "κ", lambda: "λ", mu: "μ", nu: "ν",
- xi: "ξ", pi: "π", varpi: "ϖ", rho: "ρ", varrho: "ϱ",
- sigma: "σ", varsigma: "ς", tau: "τ", upsilon: "υ", phi: "φ",
- varphi: "ϕ", chi: "χ", psi: "ψ", omega: "ω",
- // Greek uppercase
- Gamma: "Γ", Delta: "Δ", Theta: "Θ", Lambda: "Λ", Xi: "Ξ",
- Pi: "Π", Sigma: "Σ", Upsilon: "Υ", Phi: "Φ", Psi: "Ψ", Omega: "Ω",
- // Misc symbols
- infty: "∞", partial: "∂", nabla: "∇", forall: "∀", exists: "∃",
- emptyset: "∅", varnothing: "∅", wp: "℘", Re: "ℜ", Im: "ℑ",
- aleph: "ℵ", hbar: "ℏ", ell: "ℓ", sharp: "♯", flat: "♭",
- natural: "♮", clubsuit: "♣", diamondsuit: "♢", heartsuit: "♡", spadesuit: "♠",
- // Big operators
- sum: "∑", prod: "∏", coprod: "∐", int: "∫", oint: "∮",
- bigcup: "⋃", bigcap: "⋂", bigoplus: "⨁", bigotimes: "⨂",
- // Dots
- ldots: "…", cdots: "⋯", vdots: "⋮", ddots: "⋱",
- // Formatting
- quad: " ", qquad: " ", text: "", mathrm: "", mathbf: "", mathit: "",
- textbf: "", textit: "", textrm: "",
- // Delimiters
- langle: "⟨", rangle: "⟩", lfloor: "⌊", rfloor: "⌋",
- lceil: "⌈", rceil: "⌉", lvert: "|", rvert: "|",
- // Accents (just show the base)
- hat: "^", tilde: "~", bar: "‾", vec: "→", dot: "·", ddot: "¨",
- // Common functions (keep as text)
- log: "log", ln: "ln", exp: "exp", sin: "sin", cos: "cos", tan: "tan",
- min: "min", max: "max", sup: "sup", inf: "inf", lim: "lim",
- sqrt: "√", frac: "/",
-}
+import { convertLatexToUnicode } from "@/lib/latex-to-unicode"
// Module-level cache of source file names
let cachedSourceFiles: string[] = []
@@ -628,17 +577,7 @@ function processContent(text: string): string {
let result = text
// Clean up LaTeX notation → readable Unicode
- // First handle $\command$ patterns, then strip remaining $...$
- result = result.replace(/\$\\([a-zA-Z]+)\$/g, (_match, cmd: string) => {
- return LATEX_TO_UNICODE[cmd] ?? `\\${cmd}`
- })
- // Handle $$...$$ display math (keep inner content, strip delimiters)
- result = result.replace(/\$\$([^$]+)\$\$/g, "\n$1\n")
- // Handle remaining $...$ inline math with potential LaTeX inside
- result = result.replace(/\$([^$]+)\$/g, (_match, inner: string) => {
- // Replace LaTeX commands inside the math expression
- return inner.replace(/\\([a-zA-Z]+)/g, (_m, cmd: string) => LATEX_TO_UNICODE[cmd] ?? `\\${cmd}`)
- })
+ result = convertLatexToUnicode(result)
// Fix malformed wikilinks like [[name] (missing closing bracket)
result = result.replace(/\[\[([^\]]+)\](?!\])/g, "[[$1]]")
diff --git a/src/components/editor/file-preview.tsx b/src/components/editor/file-preview.tsx
index 10493d52..1e84b6d6 100644
--- a/src/components/editor/file-preview.tsx
+++ b/src/components/editor/file-preview.tsx
@@ -1,4 +1,5 @@
import { convertFileSrc } from "@tauri-apps/api/core"
+import { convertLatexToUnicode } from "@/lib/latex-to-unicode"
import {
FileText,
Image as ImageIcon,
@@ -111,7 +112,7 @@ function TextPreview({ filePath, content, label }: { filePath: string; content:
<span>{filePath}</span>
<span className="rounded bg-muted px-1.5 py-0.5 text-[10px] uppercase">{label}</span>
</div>
- <pre className="whitespace-pre-wrap font-mono text-sm">{content}</pre>
+ <pre className="whitespace-pre-wrap font-mono text-sm">{convertLatexToUnicode(content)}</pre>
</div>
)
}
diff --git a/src/components/editor/wiki-editor.tsx b/src/components/editor/wiki-editor.tsx
index 860586bb..871e16f8 100644
--- a/src/components/editor/wiki-editor.tsx
+++ b/src/components/editor/wiki-editor.tsx
@@ -1,3 +1,4 @@
+import { useMemo } from "react"
import { Editor, rootCtx, defaultValueCtx } from "@milkdown/kit/core"
import { commonmark } from "@milkdown/kit/preset/commonmark"
import { gfm } from "@milkdown/kit/preset/gfm"
@@ -6,6 +7,7 @@ import { listener, listenerCtx } from "@milkdown/kit/plugin/listener"
import { nord } from "@milkdown/theme-nord"
import { Milkdown, MilkdownProvider, useEditor } from "@milkdown/react"
import "@milkdown/theme-nord/style.css"
+import { convertLatexToUnicode } from "@/lib/latex-to-unicode"
interface WikiEditorInnerProps {
content: string
@@ -40,10 +42,12 @@ interface WikiEditorProps {
}
export function WikiEditor({ content, onSave }: WikiEditorProps) {
+ const processedContent = useMemo(() => convertLatexToUnicode(content), [content])
+
return (
<MilkdownProvider>
<div className="prose prose-invert min-w-0 max-w-none overflow-hidden p-6">
- <WikiEditorInner content={content} onSave={onSave} />
+ <WikiEditorInner content={processedContent} onSave={onSave} />
</div>
</MilkdownProvider>
)
diff --git a/src/lib/latex-to-unicode.ts b/src/lib/latex-to-unicode.ts
new file mode 100644
index 00000000..2c92f7af
--- /dev/null
+++ b/src/lib/latex-to-unicode.ts
@@ -0,0 +1,75 @@
+// Comprehensive LaTeX command → Unicode mapping
+const LATEX_TO_UNICODE: Record<string, string> = {
+ // Arrows
+ rightarrow: "→", Rightarrow: "⇒", longrightarrow: "⟶",
+ leftarrow: "←", Leftarrow: "⇐", longleftarrow: "⟵",
+ leftrightarrow: "↔", Leftrightarrow: "⇔",
+ uparrow: "↑", Uparrow: "⇑", downarrow: "↓", Downarrow: "⇓",
+ mapsto: "↦", hookrightarrow: "↪", hookleftarrow: "↩",
+ nearrow: "↗", nwarrow: "↖", searrow: "↘", swarrow: "↙",
+ // Math operators
+ times: "×", div: "÷", pm: "±", mp: "∓", cdot: "·", ast: "∗",
+ star: "⋆", circ: "∘", bullet: "•", oplus: "⊕", otimes: "⊗",
+ // Relations
+ leq: "≤", le: "≤", geq: "≥", ge: "≥", neq: "≠", ne: "≠",
+ approx: "≈", sim: "∼", simeq: "≃", equiv: "≡", cong: "≅",
+ propto: "∝", ll: "≪", gg: "≫", prec: "≺", succ: "≻",
+ subset: "⊂", supset: "⊃", subseteq: "⊆", supseteq: "⊇",
+ in: "∈", notin: "∉", ni: "∋", cap: "∩", cup: "∪",
+ vee: "∨", wedge: "∧", neg: "¬", lnot: "¬",
+ // Greek lowercase
+ alpha: "α", beta: "β", gamma: "γ", delta: "δ", epsilon: "ε",
+ varepsilon: "ε", zeta: "ζ", eta: "η", theta: "θ", vartheta: "ϑ",
+ iota: "ι", kappa: "κ", lambda: "λ", mu: "μ", nu: "ν",
+ xi: "ξ", pi: "π", varpi: "ϖ", rho: "ρ", varrho: "ϱ",
+ sigma: "σ", varsigma: "ς", tau: "τ", upsilon: "υ", phi: "φ",
+ varphi: "ϕ", chi: "χ", psi: "ψ", omega: "ω",
+ // Greek uppercase
+ Gamma: "Γ", Delta: "Δ", Theta: "Θ", Lambda: "Λ", Xi: "Ξ",
+ Pi: "Π", Sigma: "Σ", Upsilon: "Υ", Phi: "Φ", Psi: "Ψ", Omega: "Ω",
+ // Misc symbols
+ infty: "∞", partial: "∂", nabla: "∇", forall: "∀", exists: "∃",
+ emptyset: "∅", varnothing: "∅", wp: "℘", Re: "ℜ", Im: "ℑ",
+ aleph: "ℵ", hbar: "ℏ", ell: "ℓ", sharp: "♯", flat: "♭",
+ natural: "♮", clubsuit: "♣", diamondsuit: "♢", heartsuit: "♡", spadesuit: "♠",
+ // Big operators
+ sum: "∑", prod: "∏", coprod: "∐", int: "∫", oint: "∮",
+ bigcup: "⋃", bigcap: "⋂", bigoplus: "⨁", bigotimes: "⨂",
+ // Dots
+ ldots: "…", cdots: "⋯", vdots: "⋮", ddots: "⋱",
+ // Formatting
+ quad: " ", qquad: " ", text: "", mathrm: "", mathbf: "", mathit: "",
+ textbf: "", textit: "", textrm: "",
+ // Delimiters
+ langle: "⟨", rangle: "⟩", lfloor: "⌊", rfloor: "⌋",
+ lceil: "⌈", rceil: "⌉", lvert: "|", rvert: "|",
+ // Accents
+ hat: "^", tilde: "~", bar: "‾", vec: "→", dot: "·", ddot: "¨",
+ // Common functions
+ log: "log", ln: "ln", exp: "exp", sin: "sin", cos: "cos", tan: "tan",
+ min: "min", max: "max", sup: "sup", inf: "inf", lim: "lim",
+ sqrt: "√", frac: "/",
+}
+
+/**
+ * Convert LaTeX notation in text to Unicode characters.
+ * Handles $\command$, $expr with \commands$, and $$...$$ blocks.
+ */
+export function convertLatexToUnicode(text: string): string {
+ let result = text
+
+ // Handle $\command$ patterns
+ result = result.replace(/\$\\([a-zA-Z]+)\$/g, (_match, cmd: string) => {
+ return LATEX_TO_UNICODE[cmd] ?? `\\${cmd}`
+ })
+
+ // Handle $$...$$ display math
+ result = result.replace(/\$\$([^$]+)\$\$/g, "\n$1\n")
+
+ // Handle remaining $...$ inline math with LaTeX commands inside
+ result = result.replace(/\$([^$]+)\$/g, (_match, inner: string) => {
+ return inner.replace(/\\([a-zA-Z]+)/g, (_m, cmd: string) => LATEX_TO_UNICODE[cmd] ?? `\\${cmd}`)
+ })
+
+ return result
+}