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: edit wiki frontmatter as raw markdown

nash_su02518633f7e5
diff --git a/src/components/editor/wiki-editor.tsx b/src/components/editor/wiki-editor.tsx
index d05c40b5..eff7a294 100644
--- a/src/components/editor/wiki-editor.tsx
+++ b/src/components/editor/wiki-editor.tsx
@@ -1,61 +1,10 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
-import { Editor, rootCtx, defaultValueCtx } from "@milkdown/kit/core"
-import { commonmark } from "@milkdown/kit/preset/commonmark"
-import { gfm } from "@milkdown/kit/preset/gfm"
-import { history } from "@milkdown/kit/plugin/history"
-import { listener, listenerCtx } from "@milkdown/kit/plugin/listener"
-import { math } from "@milkdown/plugin-math"
-import { nord } from "@milkdown/theme-nord"
-import { Milkdown, MilkdownProvider, useEditor } from "@milkdown/react"
-import "@milkdown/theme-nord/style.css"
import "katex/dist/katex.min.css"
import { Pencil, Eye } from "lucide-react"
import { parseFrontmatter } from "@/lib/frontmatter"
import { FrontmatterPanel } from "@/components/editor/frontmatter-panel"
import { WikiReader } from "@/components/editor/wiki-reader"
-interface WikiEditorInnerProps {
- content: string
- onSave: (markdown: string, options?: { immediate?: boolean }) => void
- onMarkdownChange: (markdown: string) => void
-}
-
-function WikiEditorInner({ content, onSave, onMarkdownChange }: WikiEditorInnerProps) {
- // Milkdown fires `markdownUpdated` once on initial parse before any
- // user interaction. That one emit must not be forwarded as a save,
- // otherwise just opening a file can overwrite its content with
- // Milkdown's normalized-but-equivalent re-emit (or, worse, with a
- // placeholder string that came back from a failed read).
- const initialEmitConsumedRef = useRef(false)
-
- useEditor(
- (root) =>
- Editor.make()
- .config(nord)
- .config((ctx) => {
- ctx.set(rootCtx, root)
- ctx.set(defaultValueCtx, content)
- initialEmitConsumedRef.current = false
- ctx.get(listenerCtx).markdownUpdated((_ctx, markdown) => {
- if (!initialEmitConsumedRef.current) {
- initialEmitConsumedRef.current = true
- return
- }
- onMarkdownChange(markdown)
- onSave(markdown)
- })
- })
- .use(commonmark)
- .use(gfm)
- .use(math)
- .use(history)
- .use(listener),
- [content],
- )
-
- return <Milkdown />
-}
-
interface WikiEditorProps {
content: string
onSave: (markdown: string, options?: { immediate?: boolean }) => void
@@ -64,53 +13,34 @@ interface WikiEditorProps {
filePath?: string
}
-function wrapBareMathBlocks(text: string): string {
- return text.replace(
- /(?<!\$\$\s*)(\\begin\{[^}]+\}[\s\S]*?\\end\{[^}]+\})(?!\s*\$\$)/g,
- (_match, block: string) => `$$\n${block}\n$$`,
- )
-}
-
export function WikiEditor({ content, onSave, filePath }: WikiEditorProps) {
- // Default to read mode (ReactMarkdown render). Edit mode swaps
- // in Milkdown WYSIWYG. We default to read because:
- // 1. Milkdown's commonmark/gfm preset has no wikilink schema,
- // so `[[…]]` shows up as raw text — exactly what users
- // called out as "looking like raw code".
- // 2. We can pre-process wikilinks for the read view safely
- // (the rendered output is throwaway). Doing the same in
- // Milkdown would be a save-corruption hazard because
- // Milkdown serializes its current state on save — the
- // transformed `[label](#slug)` would overwrite the
- // original `[[…]]` source.
- // 3. Users read wiki pages far more often than they edit
- // them; the toggle makes editing a deliberate action
- // rather than the default state.
+ // Default to read mode (ReactMarkdown render). Edit mode is a raw Markdown
+ // textarea so metadata/frontmatter can be edited without a WYSIWYG serializer
+ // rewriting YAML, wikilinks, or other wiki-specific source syntax.
const [mode, setMode] = useState<"read" | "edit">("read")
- // Split frontmatter from body. Both modes consume `body`;
- // Milkdown additionally rebuilds the full file via `rawBlock`
- // on save so user-managed YAML survives untouched.
- const { frontmatter, body, rawBlock } = useMemo(
+ // Read mode renders frontmatter as UI plus the Markdown body. Edit mode uses
+ // a plain-text Markdown editor for the full file so frontmatter can be edited
+ // without passing YAML through Milkdown's CommonMark serializer.
+ const { frontmatter, body } = useMemo(
() => parseFrontmatter(content),
[content],
)
- const processedBody = useMemo(() => wrapBareMathBlocks(body), [body])
- const latestBodyRef = useRef(processedBody)
+ const editableMarkdown = content
+ const [draftMarkdown, setDraftMarkdown] = useState(editableMarkdown)
+ const latestMarkdownRef = useRef(editableMarkdown)
useEffect(() => {
- latestBodyRef.current = processedBody
- }, [processedBody])
-
- const handleSave = useMemo(
- () => (markdown: string, options?: { immediate?: boolean }) => onSave(rawBlock + markdown, options),
- [onSave, rawBlock],
- )
+ if (mode !== "edit") {
+ setDraftMarkdown(editableMarkdown)
+ latestMarkdownRef.current = editableMarkdown
+ }
+ }, [editableMarkdown, mode])
const saveLatestNow = useCallback(() => {
- onSave(rawBlock + latestBodyRef.current, { immediate: true })
- }, [onSave, rawBlock])
+ onSave(latestMarkdownRef.current, { immediate: true })
+ }, [onSave])
return (
<div
@@ -128,6 +58,12 @@ export function WikiEditor({ content, onSave, filePath }: WikiEditorProps) {
type="button"
onClick={() => {
if (mode === "edit") saveLatestNow()
+ if (mode === "read") {
+ // Seed edit mode from the latest raw file content before switching;
+ // the sync effect intentionally does not reset drafts while editing.
+ setDraftMarkdown(editableMarkdown)
+ latestMarkdownRef.current = editableMarkdown
+ }
setMode((m) => (m === "read" ? "edit" : "read"))
}}
title={mode === "read" ? "Edit (raw markdown)" : "Done editing"}
@@ -143,18 +79,20 @@ export function WikiEditor({ content, onSave, filePath }: WikiEditorProps) {
<WikiReader body={body} filePath={filePath} />
</div>
) : (
- <MilkdownProvider>
- <div className="prose prose-invert min-w-0 max-w-none overflow-hidden p-6">
- {frontmatter && <FrontmatterPanel data={frontmatter} />}
- <WikiEditorInner
- content={processedBody}
- onSave={handleSave}
- onMarkdownChange={(markdown) => {
- latestBodyRef.current = markdown
- }}
- />
- </div>
- </MilkdownProvider>
+ <div className="h-full p-6">
+ <textarea
+ aria-label="Raw Markdown editor"
+ value={draftMarkdown}
+ onChange={(event) => {
+ const next = event.currentTarget.value
+ setDraftMarkdown(next)
+ latestMarkdownRef.current = next
+ onSave(next)
+ }}
+ spellCheck={false}
+ className="h-full min-h-[60vh] w-full resize-none rounded-md border border-border/60 bg-background/70 p-4 font-mono text-sm leading-6 text-foreground outline-none focus:border-primary/60 focus:ring-2 focus:ring-primary/20"
+ />
+ </div>
)}
</div>
)
diff --git a/src/lib/frontmatter.test.ts b/src/lib/frontmatter.test.ts
index ecf33629..f3537100 100644
--- a/src/lib/frontmatter.test.ts
+++ b/src/lib/frontmatter.test.ts
@@ -29,6 +29,16 @@ describe("parseFrontmatter", () => {
}
})
+ it("documents that recovered fallback frontmatter may be lossy", () => {
+ const content = "```yaml\n---\ntitle: Fixed\n---\n```\n\n# Body"
+ const r = parseFrontmatter(content)
+
+ expect(r.frontmatter).toEqual({ title: "Fixed" })
+ expect(r.rawBlock).toBe("---\ntitle: Fixed\n---\n")
+ expect(r.rawBlock + r.body).not.toBe(content)
+ expect(r.body).toBe("# Body")
+ })
+
it("parses inline arrays", () => {
const r = parseFrontmatter(`---\ntags: [foo, bar, baz]\n---\nx`)
expect(r.frontmatter).toEqual({ tags: ["foo", "bar", "baz"] })
diff --git a/src/lib/frontmatter.ts b/src/lib/frontmatter.ts
index 95c9b997..204d181f 100644
--- a/src/lib/frontmatter.ts
+++ b/src/lib/frontmatter.ts
@@ -10,8 +10,8 @@ export interface FrontmatterParseResult {
* closing `---`, plus the newlines that separate it from the
* body) as it appears in the input. Empty string when there is
* no frontmatter. Callers that edit only the body — e.g. the
- * WikiEditor — write back `rawBlock + body` so user-managed YAML
- * survives untouched.
+ * read-mode renderers or body-only transforms — write back
+ * `rawBlock + body` so user-managed YAML survives untouched.
*/
rawBlock: string
}