diff --git a/src/agent/index-middleware.ts b/src/agent/index-middleware.ts index 675912d5..cb3b64c8 100644 --- a/src/agent/index-middleware.ts +++ b/src/agent/index-middleware.ts @@ -165,7 +165,7 @@ function renderLinks( return `# ${heading}\n\n${items.join("\n")}`; } -/** Parses the optional title and description from YAML front matter. */ +/** Parses usable optional display metadata from YAML front matter. */ function parseFrontmatter( content: string, filePath: string, @@ -191,21 +191,20 @@ function parseFrontmatter( } const { description, title } = fields as Record; - if ( - description !== undefined && - (typeof description !== "string" || !description.trim()) - ) { - throw new Error(`${filePath} YAML description must be a non-empty string.`); - } - if (title !== undefined && typeof title !== "string") { - throw new Error(`${filePath} YAML title must be a string.`); - } + const usableDescription = usableString(description); + const usableTitle = usableString(title); return { - ...(description ? { description } : {}), - ...(title ? { title } : {}), + ...(usableDescription ? { description: usableDescription } : {}), + ...(usableTitle ? { title: usableTitle } : {}), }; } +/** Returns optional front matter text only when it can be rendered in an index. */ +function usableString(value: unknown): string | undefined { + if (typeof value !== "string" || !value.trim()) return undefined; + return value; +} + /** Converts an unknown thrown value into a readable message. */ function errorMessage(error: unknown): string { return error instanceof Error ? error.message : String(error); diff --git a/test/index-middleware.test.ts b/test/index-middleware.test.ts index 9cd5d56d..03fe0b32 100644 --- a/test/index-middleware.test.ts +++ b/test/index-middleware.test.ts @@ -151,20 +151,27 @@ describe("synchronizeWikiIndexes", () => { } }); - test.each(["123", "[one, two]", "{ text: nested }"])( - "rejects a non-string YAML description: %s", - async (description) => { - const { backend } = await setup(); + test.each([ + ["123", "[one, two]"], + ["[one, two]", "{ text: nested }"], + ["{ text: nested }", ""], + ])( + "falls back when optional title and description are not usable strings: %s / %s", + async (title, description) => { + const { backend, rootDir } = await setup(); await backend.write( "/openwiki/page.md", - `---\ntype: Reference\ndescription: ${description}\n---\n`, + `---\ntype: Reference\ntitle: ${title}\ndescription: ${description}\n---\n`, ); - await expect( - synchronizeWikiIndexes(backend, "repository"), - ).rejects.toThrow( - "/openwiki/page.md YAML description must be a non-empty string.", + await synchronizeWikiIndexes(backend, "repository"); + + const index = await readFile( + path.join(rootDir, "openwiki/index.md"), + "utf8", ); + expect(index).toContain("- [page](page.md)\n"); + expect(index).not.toContain(" - "); }, );