Skip to content

Commit 4f66821

Browse files
committed
fix(memory-wiki): tolerate YAML parse errors in wiki frontmatter
A single wiki page with malformed YAML frontmatter (e.g. markdown bold syntax containing colons inside list items) caused wiki_apply and wiki_lint to fail entirely via YAMLParseError. parseWikiMarkdown now wraps YAML.parse in try-catch: when parsing fails, it returns an empty frontmatter while preserving the body. This prevents one bad file from crashing the entire vault compilation. Fixes #96125
1 parent 8c09419 commit 4f66821

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

extensions/memory-wiki/src/markdown.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,16 @@ describe("toWikiPageSummary", () => {
421421
},
422422
]);
423423
});
424+
425+
it("tolerates frontmatter with YAML parse errors instead of crashing", () => {
426+
const page = toWikiPageSummary({
427+
absolutePath: "/tmp/wiki/sources/bad-yaml.md",
428+
relativePath: "sources/bad-yaml.md",
429+
raw: '---\npageType: source\nid: source.bad-yaml\ntitle: Bad YAML\nsourceIds:\n - **MEMORY.md 第 235 行**:"加特契纳 1783 年才赐予保罗,1770 年保罗才 16 岁"\n---\n\nBody text.\n',
430+
});
431+
expect(page).not.toBeNull();
432+
expect(page?.hasFrontmatter).toBe(true);
433+
expect(page?.title).toBe("bad-yaml"); // title falls back to filename when frontmatter is empty
434+
expect(page?.sourceIds).toEqual([]);
435+
});
424436
});

extensions/memory-wiki/src/markdown.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,19 @@ export function parseWikiMarkdown(content: string): ParsedWikiMarkdown {
178178
if (!match) {
179179
return { hasFrontmatter: false, frontmatter: {}, body: content };
180180
}
181-
const parsed = YAML.parse(match[1]) as unknown;
182-
return {
183-
hasFrontmatter: true,
184-
frontmatter:
185-
parsed && typeof parsed === "object" && !Array.isArray(parsed)
186-
? (parsed as Record<string, unknown>)
187-
: {},
188-
body: content.slice(match[0].length),
189-
};
181+
try {
182+
const parsed = YAML.parse(match[1]) as unknown;
183+
return {
184+
hasFrontmatter: true,
185+
frontmatter:
186+
parsed && typeof parsed === "object" && !Array.isArray(parsed)
187+
? (parsed as Record<string, unknown>)
188+
: {},
189+
body: content.slice(match[0].length),
190+
};
191+
} catch {
192+
return { hasFrontmatter: true, frontmatter: {}, body: content.slice(match[0].length) };
193+
}
190194
}
191195

192196
export function renderWikiMarkdown(params: {

0 commit comments

Comments
 (0)