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: strip orphan closing ``` from legacy yaml-wrapped frontmatter

nash_su1ba916685f67
diff --git a/src/lib/frontmatter.test.ts b/src/lib/frontmatter.test.ts
index 811612c4..ecf33629 100644
--- a/src/lib/frontmatter.test.ts
+++ b/src/lib/frontmatter.test.ts
@@ -116,16 +116,46 @@ describe("parseFrontmatter", () => {
expect(r.frontmatter?.sources).toEqual(["research--2026-04-07.md"])
})
- it("locates a frontmatter block prefixed by a stray code fence line (LLM corruption)", () => {
+ it("locates a frontmatter block wrapped in ```yaml … ``` and strips the closing fence", () => {
const content =
- "```yaml\n---\ntype: entity\ntitle: \"Accumulibacter\"\n---\n\n# Accumulibacter\n\nbody\n```"
+ "```yaml\n---\ntype: entity\ntitle: \"Accumulibacter\"\n---\n```\n\n# Accumulibacter\n\nbody"
const r = parseFrontmatter(content)
expect(r.frontmatter?.type).toBe("entity")
expect(r.frontmatter?.title).toBe("Accumulibacter")
- // Body retains the trailing closing fence — that's a renderer
- // problem (markdown shows it as an unbalanced ```) not the
- // parser's job. We only commit to extracting frontmatter.
- expect(r.body.startsWith("# Accumulibacter")).toBe(true)
+ // Both the opening AND closing code-fence lines are gone so
+ // the body the renderer sees is balanced markdown. Without
+ // stripping, ReactMarkdown reads the orphan ``` as a
+ // never-closing code block and renders every heading /
+ // list / table below as raw source.
+ expect(r.body).not.toContain("```")
+ expect(r.body.trimStart().startsWith("# Accumulibacter")).toBe(true)
+ })
+
+ it("strips the closing fence even when the closing ``` is on the line directly after ---", () => {
+ // Real-user file pattern (硝化菌.md): the ``` closing fence
+ // lands immediately after the closing ---, with no blank
+ // line between them.
+ const content = [
+ "```yaml",
+ "---",
+ "type: entity",
+ 'title: "硝化菌"',
+ "tags: [微生物]",
+ "---",
+ "```",
+ "",
+ "# 硝化菌",
+ "",
+ "## 基本信息",
+ "",
+ "- **类型**:自养型好氧细菌",
+ ].join("\n")
+ const r = parseFrontmatter(content)
+ expect(r.frontmatter?.type).toBe("entity")
+ expect(r.frontmatter?.title).toBe("硝化菌")
+ expect(r.body).not.toContain("```")
+ expect(r.body).toContain("# 硝化菌")
+ expect(r.body).toContain("## 基本信息")
})
it("locates a frontmatter block prefixed by a `frontmatter:` key (LLM corruption)", () => {
diff --git a/src/lib/frontmatter.ts b/src/lib/frontmatter.ts
index 5934a72b..95c9b997 100644
--- a/src/lib/frontmatter.ts
+++ b/src/lib/frontmatter.ts
@@ -101,10 +101,31 @@ function locateFrontmatterBlock(
}
const rawBlock = content.slice(openIdx, openIdx + fallback[0].length - 1)
+ const bodyAfterFm = content.slice(openIdx + rawBlock.length)
+
+ // If the prefix that pushed us into the fallback is a ```yaml /
+ // ```yml (or bare ```) code fence opener, strip the matching
+ // CLOSING fence at the head of the body too. Without this, a
+ // legacy LLM-corrupted page that wrapped its frontmatter in a
+ // code fence renders correctly up top (the parser still
+ // recovered the YAML) but the body opens with an orphan ```
+ // that ReactMarkdown treats as a never-closed code block —
+ // every heading / list / table below appears as raw source.
+ const prefix = content.slice(0, openIdx)
+ const prefixIsYamlFence = /^\s*```(?:yaml|yml)?\s*\r?\n$/i.test(prefix)
+ if (prefixIsYamlFence) {
+ const stripped = bodyAfterFm.replace(/^\s*```\s*(?:\r?\n|$)/, "")
+ return {
+ yamlPayload: fallback[1],
+ rawBlock,
+ body: stripped,
+ }
+ }
+
return {
yamlPayload: fallback[1],
rawBlock,
- body: content.slice(openIdx + rawBlock.length),
+ body: bodyAfterFm,
}
}