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

fix: Save to Wiki no longer overwrites same-day saves with CJK titles

nash_su869583acc675
diff --git a/src/components/chat/chat-message.tsx b/src/components/chat/chat-message.tsx
index 5428214b..7ec28856 100644
--- a/src/components/chat/chat-message.tsx
+++ b/src/components/chat/chat-message.tsx
@@ -16,6 +16,7 @@ import type { FileNode } from "@/types/wiki"
import { convertLatexToUnicode } from "@/lib/latex-to-unicode"
import { normalizePath, getFileName } from "@/lib/path-utils"
+import { makeQueryFileName } from "@/lib/wiki-filename"
// Module-level cache of source file names
let cachedSourceFiles: string[] = []
@@ -155,17 +156,13 @@ function SaveToWikiButton({ content, visible }: { content: string; visible: bool
const pp = normalizePath(project.path)
setSaving(true)
try {
- // Generate slug from first line or first 50 chars
+ // Generate a unique filename for this save.
+ // See `src/lib/wiki-filename.ts` — the slug is Unicode-aware
+ // (so CJK titles don't collapse to empty) and the HHMMSS
+ // timestamp suffix guarantees same-day saves stay distinct.
const firstLine = content.split("\n")[0].replace(/^#+\s*/, "").trim()
const title = firstLine.slice(0, 60) || "Saved Query"
- const slug = title
- .toLowerCase()
- .replace(/[^a-z0-9\s-]/g, "")
- .trim()
- .replace(/\s+/g, "-")
- .slice(0, 50)
- const date = new Date().toISOString().slice(0, 10)
- const fileName = `${slug}-${date}.md`
+ const { date, fileName } = makeQueryFileName(title)
const filePath = `${pp}/wiki/queries/${fileName}`
// Strip hidden sources comment and thinking blocks from content
@@ -195,7 +192,11 @@ function SaveToWikiButton({ content, visible }: { content: string; visible: bool
} catch {
indexContent = "# Wiki Index\n\n## Queries\n"
}
- const entry = `- [[queries/${slug}-${date}|${title}]]`
+ // The wikilink target is the filename WITHOUT the `.md`
+ // extension — must match `fileName` exactly (including the
+ // time suffix) or the link lands on a 404.
+ const linkTarget = fileName.replace(/\.md$/, "")
+ const entry = `- [[queries/${linkTarget}|${title}]]`
if (indexContent.includes("## Queries")) {
indexContent = indexContent.replace(
/(## Queries\n)/,
diff --git a/src/lib/wiki-filename.test.ts b/src/lib/wiki-filename.test.ts
new file mode 100644
index 00000000..12db987d
--- /dev/null
+++ b/src/lib/wiki-filename.test.ts
@@ -0,0 +1,113 @@
+/**
+ * Regression suite for Save-to-Wiki filename generation. The bug we're
+ * fixing: the previous ASCII-only slug regex made every CJK-titled
+ * conversation collapse to an empty slug, colliding all same-day saves
+ * into a single `-YYYY-MM-DD.md`. These tests pin the new policy.
+ */
+import { describe, it, expect } from "vitest"
+import { makeQuerySlug, makeQueryFileName } from "./wiki-filename"
+
+describe("makeQuerySlug", () => {
+ it("ASCII title is lowercased and hyphenated", () => {
+ expect(makeQuerySlug("Understanding RoPE in LLMs")).toBe("understanding-rope-in-llms")
+ })
+
+ it("strips ASCII punctuation but keeps letters and digits", () => {
+ expect(makeQuerySlug("What is GPT-4's context window?")).toBe("what-is-gpt-4s-context-window")
+ })
+
+ it("keeps CJK characters intact (the core bug fix)", () => {
+ // Previously these collapsed to "" → filename collisions.
+ expect(makeQuerySlug("旋转位置编码")).toBe("旋转位置编码")
+ expect(makeQuerySlug("日本茶道")).toBe("日本茶道")
+ })
+
+ it("handles mixed CJK + ASCII cleanly", () => {
+ expect(makeQuerySlug("RoPE 旋转位置编码")).toBe("rope-旋转位置编码")
+ })
+
+ it("normalizes full-width digits / latin to half-width (NFKC)", () => {
+ // Full-width "RoPE" and "2025" normalize to their ASCII
+ // equivalents so the file tree looks consistent.
+ expect(makeQuerySlug("RoPE 2025")).toBe("rope-2025")
+ })
+
+ it("keeps Japanese kana and kanji", () => {
+ expect(makeQuerySlug("アテンション機構")).toBe("アテンション機構")
+ })
+
+ it("falls back to 'query' when title yields nothing usable", () => {
+ // Emoji-only, punctuation-only, whitespace-only — all previously
+ // would produce "-YYYY-MM-DD.md". The fallback keeps the
+ // filename readable AND distinguishable (the timestamp suffix
+ // on the full filename carries the uniqueness).
+ expect(makeQuerySlug("🎉🔥💯")).toBe("query")
+ expect(makeQuerySlug("!!! ??? ...")).toBe("query")
+ expect(makeQuerySlug(" ")).toBe("query")
+ expect(makeQuerySlug("")).toBe("query")
+ })
+
+ it("collapses runs of hyphens and trims leading/trailing ones", () => {
+ expect(makeQuerySlug("---foo bar---")).toBe("foo-bar")
+ expect(makeQuerySlug("a - b - c")).toBe("a-b-c")
+ })
+
+ it("truncates long slugs to 50 characters", () => {
+ const long = "a".repeat(200)
+ expect(makeQuerySlug(long)).toHaveLength(50)
+ })
+
+ it("preserves case-insensitive equivalence for wikilink matching", () => {
+ // The codebase treats wikilinks case-insensitively (see lint.ts
+ // line 48 comment). Slugs are always lowercase, so two titles
+ // that only differ by case always produce the SAME slug — which
+ // is desirable: repeat saves of the same topic still collide on
+ // slug but the timestamp in the filename keeps them distinct.
+ expect(makeQuerySlug("RoPE")).toBe(makeQuerySlug("rope"))
+ expect(makeQuerySlug("ATTENTION")).toBe(makeQuerySlug("attention"))
+ })
+})
+
+describe("makeQueryFileName", () => {
+ // Fixed UTC clock for deterministic assertions.
+ const NOW = new Date("2026-04-23T14:30:52.123Z")
+
+ it("produces slug-YYYY-MM-DD-HHMMSS.md shape", () => {
+ const { fileName, slug, date, time } = makeQueryFileName("Attention Is All You Need", NOW)
+ expect(slug).toBe("attention-is-all-you-need")
+ expect(date).toBe("2026-04-23")
+ expect(time).toBe("143052")
+ expect(fileName).toBe("attention-is-all-you-need-2026-04-23-143052.md")
+ })
+
+ it("two saves of the SAME title within the same day produce DIFFERENT filenames (the reported bug)", () => {
+ const a = makeQueryFileName("旋转位置编码", new Date("2026-04-23T10:00:00.000Z"))
+ const b = makeQueryFileName("旋转位置编码", new Date("2026-04-23T14:30:52.123Z"))
+ const c = makeQueryFileName("旋转位置编码", new Date("2026-04-23T23:59:59.999Z"))
+ // All three must be distinct — this is the whole point of the fix.
+ expect(new Set([a.fileName, b.fileName, c.fileName]).size).toBe(3)
+ // And all three must share the same slug prefix (so users can
+ // visually group them by topic in the file tree).
+ expect(a.slug).toBe("旋转位置编码")
+ expect(a.fileName.startsWith("旋转位置编码-2026-04-23-")).toBe(true)
+ expect(b.fileName.startsWith("旋转位置编码-2026-04-23-")).toBe(true)
+ expect(c.fileName.startsWith("旋转位置编码-2026-04-23-")).toBe(true)
+ })
+
+ it("emoji-only title falls back to 'query' but still produces distinct filenames per save", () => {
+ const a = makeQueryFileName("🎉🔥", new Date("2026-04-23T10:00:00Z"))
+ const b = makeQueryFileName("🎉🔥", new Date("2026-04-23T10:00:01Z"))
+ expect(a.fileName).toBe("query-2026-04-23-100000.md")
+ expect(b.fileName).toBe("query-2026-04-23-100001.md")
+ expect(a.fileName).not.toBe(b.fileName)
+ })
+
+ it("uses UTC so same timestamp from different timezones hashes identically", () => {
+ // toISOString always reports UTC, so the filename is stable
+ // across machines with different local clocks. A regression to
+ // toTimeString / getHours would make this test fail on any
+ // machine not in UTC.
+ const { time } = makeQueryFileName("x", new Date("2026-04-23T14:30:52.000Z"))
+ expect(time).toBe("143052")
+ })
+})
diff --git a/src/lib/wiki-filename.ts b/src/lib/wiki-filename.ts
new file mode 100644
index 00000000..075bae1a
--- /dev/null
+++ b/src/lib/wiki-filename.ts
@@ -0,0 +1,66 @@
+/**
+ * Filename generation for user-initiated wiki writes ("Save to Wiki").
+ *
+ * Why this exists as its own module:
+ * The previous inline logic stripped all non-ASCII chars from the
+ * slug — which made every CJK-titled conversation collapse to an
+ * empty slug and collide into `-YYYY-MM-DD.md` for the day, so the
+ * user could only keep ONE save per day. This pure module makes
+ * the filename policy trivially testable.
+ *
+ * Filename shape:
+ * {slug}-{YYYY-MM-DD}-{HHMMSS}.md
+ *
+ * Slug rules:
+ * - Unicode-aware: keeps letters & digits across all scripts
+ * (Latin, CJK, Cyrillic, Arabic …) plus ASCII hyphens.
+ * - NFKC-normalized so full-width characters don't drift from
+ * half-width equivalents.
+ * - Lowercased (no-op for scripts without case).
+ * - Whitespace → hyphen.
+ * - Collapses runs of hyphens, trims leading/trailing hyphens.
+ * - Truncated to 50 characters.
+ * - Falls back to `"query"` when nothing usable remains.
+ *
+ * The trailing timestamp guarantees same-day saves stay distinct
+ * even when the title yields an identical slug (e.g. several Chinese
+ * conversations with the same topic, or repeated "Untitled" saves).
+ */
+
+/** Produce just the slug — exported for tests / callers that want
+ * to reuse it in places like the index.md wikilink target. */
+export function makeQuerySlug(title: string): string {
+ const slug = title
+ .normalize("NFKC")
+ .trim()
+ .replace(/\s+/g, "-")
+ // Keep Unicode letters, Unicode digits, and the ASCII hyphen.
+ // Stripping emoji / punctuation keeps the filename
+ // filesystem-safe across Windows / macOS / Linux.
+ .replace(/[^\p{L}\p{N}-]/gu, "")
+ .replace(/-+/g, "-")
+ .replace(/^-|-$/g, "")
+ .toLowerCase()
+ .slice(0, 50)
+ return slug.length > 0 ? slug : "query"
+}
+
+/** Produce the full wiki filename. Accepts an injected `now` for
+ * deterministic tests — production callers should omit it. */
+export function makeQueryFileName(
+ title: string,
+ now: Date = new Date(),
+): { slug: string; fileName: string; date: string; time: string } {
+ const slug = makeQuerySlug(title)
+ // UTC timestamp — avoids DST / timezone-flipping surprises when
+ // the same save produces different filenames on different machines.
+ const iso = now.toISOString() // e.g. 2026-04-23T14:30:52.123Z
+ const date = iso.slice(0, 10) // 2026-04-23
+ const time = iso.slice(11, 19).replace(/:/g, "") // 143052
+ return {
+ slug,
+ date,
+ time,
+ fileName: `${slug}-${date}-${time}.md`,
+ }
+}