diff --git a/packages/ui/utils/parser.test.ts b/packages/ui/utils/parser.test.ts new file mode 100644 index 0000000..2d30a43 --- /dev/null +++ b/packages/ui/utils/parser.test.ts @@ -0,0 +1,103 @@ +import { describe, test, expect } from "bun:test"; +import { parseMarkdownToBlocks } from "./parser"; + +describe("parseMarkdownToBlocks — code fences", () => { + /** + * Baseline: the common triple-backtick fence still works after the nested- + * fence fix. Regression guard so we don't break normal plans. + */ + test("triple-backtick fence produces a single code block", () => { + const md = "```js\nconsole.log('hi');\n```"; + const blocks = parseMarkdownToBlocks(md); + expect(blocks).toHaveLength(1); + expect(blocks[0].type).toBe("code"); + expect(blocks[0].language).toBe("js"); + expect(blocks[0].content).toBe("console.log('hi');"); + }); + + /** + * A 4-backtick fence is used to embed a triple-backtick fence inside a code + * block (e.g. showing a markdown example). Before the fix the inner ``` would + * prematurely close the block, producing broken output. + */ + test("4-backtick fence treats inner triple-backtick as content", () => { + const md = "````md\n```js\ncode\n```\n````"; + const blocks = parseMarkdownToBlocks(md); + expect(blocks).toHaveLength(1); + expect(blocks[0].type).toBe("code"); + expect(blocks[0].content).toBe("```js\ncode\n```"); + }); + + /** + * Ensure the language tag is still extracted correctly when the fence uses + * more than three backticks. + */ + test("4-backtick fence preserves language tag", () => { + const md = "````markdown\nhello\n````"; + const blocks = parseMarkdownToBlocks(md); + expect(blocks).toHaveLength(1); + expect(blocks[0].language).toBe("markdown"); + }); + + /** + * 5-backtick fence should allow both ``` and ```` lines as content, + * only closing on a line of 5+ backticks. + */ + test("5-backtick fence treats 3- and 4-backtick lines as content", () => { + const md = "`````\n````\n```\n`````"; + const blocks = parseMarkdownToBlocks(md); + expect(blocks).toHaveLength(1); + expect(blocks[0].type).toBe("code"); + expect(blocks[0].content).toBe("````\n```"); + }); + + /** + * A closing fence may have more backticks than the opener (CommonMark §4.5). + * We support this so plans generated by tools that follow the spec parse + * correctly. + */ + test("closing fence with more backticks than opener still closes the block", () => { + const md = "````\ncontent\n`````"; + const blocks = parseMarkdownToBlocks(md); + expect(blocks).toHaveLength(1); + expect(blocks[0].content).toBe("content"); + }); + + /** + * Text before and after a nested-fence block must still parse into their own + * blocks — the nested-fence fix must not swallow surrounding content. + */ + test("nested-fence block does not swallow surrounding paragraphs", () => { + const md = "intro\n\n````\n```\nnested\n```\n````\n\noutro"; + const blocks = parseMarkdownToBlocks(md); + const types = blocks.map((b) => b.type); + expect(types).toEqual(["paragraph", "code", "paragraph"]); + expect(blocks[1].content).toBe("```\nnested\n```"); + }); + + /** + * An unclosed fence at end-of-file should produce a code block containing + * whatever content was seen (CommonMark §4.5: unclosed fences extend to EOF). + */ + test("unclosed fence at EOF produces a code block with seen content", () => { + const md = "```js\nconst x = 1;"; + const blocks = parseMarkdownToBlocks(md); + expect(blocks).toHaveLength(1); + expect(blocks[0].type).toBe("code"); + expect(blocks[0].language).toBe("js"); + expect(blocks[0].content).toBe("const x = 1;"); + }); + + /** + * A fence opener as the very last line of the document (no content, no + * closing fence) should produce an empty code block rather than crashing. + */ + test("fence opener as last line produces an empty code block", () => { + const md = "```ts"; + const blocks = parseMarkdownToBlocks(md); + expect(blocks).toHaveLength(1); + expect(blocks[0].type).toBe("code"); + expect(blocks[0].language).toBe("ts"); + expect(blocks[0].content).toBe(""); + }); +}); diff --git a/packages/ui/utils/parser.ts b/packages/ui/utils/parser.ts index e897457..58ae051 100644 --- a/packages/ui/utils/parser.ts +++ b/packages/ui/utils/parser.ts @@ -177,12 +177,15 @@ export const parseMarkdownToBlocks = (markdown: string): Block[] => { if (trimmed.startsWith('```')) { flush(); const codeStartLine = currentLineNum; + // Count backticks in opening fence to support nested fences (e.g. ```` wrapping ```) + const fenceLen = trimmed.match(/^`+/)?.[0].length ?? 3; + const closingFence = new RegExp('^`{' + fenceLen + ',}\\s*$'); // Extract language from fence (e.g., ```rust → "rust") - const language = trimmed.slice(3).trim() || undefined; + const language = trimmed.slice(fenceLen).trim() || undefined; // Fast forward until end of code block let codeContent = []; i++; // Skip start fence - while(i < lines.length && !lines[i].trim().startsWith('```')) { + while(i < lines.length && !closingFence.test(lines[i])) { codeContent.push(lines[i]); i++; }