From 7ed85dfe0b10fac7a16f0a89647c0cfaa51b2b2e Mon Sep 17 00:00:00 2001 From: Brian Malinconico Date: Fri, 20 Mar 2026 15:03:24 -0400 Subject: [PATCH] fix(parser): support nested markdown code fences (#355) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(parser): support nested markdown code fences Count backticks in the opening fence and only close on a line with at least that many backticks. Previously any ``` line would prematurely close a 4- or 5-backtick fence, corrupting blocks that embed markdown examples. Follows CommonMark §4.5. Adds parser.test.ts with 6 tests covering the fix and baseline triple-backtick behaviour. Also adds devbox.json to pin bun for running tests. Co-Authored-By: Claude Sonnet 4.6 * chore(devbox): wire up test script to bun test Co-Authored-By: Claude Sonnet 4.6 * fix(parser): pass raw line to closingFence regex; add edge-case tests The regex already anchors trailing whitespace with \s*$ so calling .trim() before testing was redundant. Passing the raw line is also more correct — it lets the regex reject a closing fence indented more than three spaces (CommonMark §4.5 semantics), not that the rest of the parser handles indented blocks today, but the intent is clearer. Also adds three previously untested edge cases: - Unclosed fence at EOF (extends to end of document per spec) - Fence opener as the last line (zero-content block, no crash) - toHaveLength(1) guard on the language-tag preservation test Co-Authored-By: Claude Sonnet 4.6 * Overcomit * Overcomit --------- Co-authored-by: Claude Sonnet 4.6 --- packages/ui/utils/parser.test.ts | 103 +++++++++++++++++++++++++++++++ packages/ui/utils/parser.ts | 7 ++- 2 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 packages/ui/utils/parser.test.ts 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++; } -- knotmirror 0.0.1