Something went wrong. Try again.
Fork of backnotprop/plannotator with reduced surface area
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993import { describe, test, expect } from "bun:test";import { parseMarkdownToBlocks, computeListIndices, extractFrontmatter, exportAnnotations } from "./parser";import type { Block } from "../types";
/** Tiny factory for list-item blocks used by computeListIndices tests. */const li = (level: number, ordered: boolean, orderedStart?: number): Block => ({ id: `b${Math.random()}`, type: "list-item", content: "", level, ordered: ordered || undefined, orderedStart, order: 0, startLine: 1,});
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(""); });
/** * Indented code fences (e.g. inside list items in a plan) must close when * the closing fence is equally indented. Before the fix, the closing fence * regex required backticks at column 0, so indented ``` never matched and * the code block swallowed everything to EOF. */ test("indented closing fence closes the code block", () => { const md = "- Replace with reads:\n ```ts\n const x = 1;\n ```\n- Next item"; const blocks = parseMarkdownToBlocks(md); const types = blocks.map((b) => b.type); expect(types).toEqual(["list-item", "code", "list-item"]); expect(blocks[1].content).toBe(" const x = 1;"); expect(blocks[2].content).toBe("Next item"); });
/** * A closing fence with trailing text (e.g. ``` some comment) should still * close the block. Before the fix, the regex required only whitespace after * backticks, so trailing text caused the fence to swallow everything to EOF. */ test("closing fence with trailing text still closes the block", () => { const md = "```js\nconst x = 1;\n``` this is ignored\nafter"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("code"); expect(blocks[0].content).toBe("const x = 1;"); expect(blocks[1].type).toBe("paragraph"); expect(blocks[1].content).toBe("after"); });
test("deeply indented fence closes correctly", () => { const md = " ```py\n print('hi')\n ```\nafter"; const blocks = parseMarkdownToBlocks(md); expect(blocks[0].type).toBe("code"); expect(blocks[0].language).toBe("py"); expect(blocks[0].content).toBe(" print('hi')"); expect(blocks[1].type).toBe("paragraph"); expect(blocks[1].content).toBe("after"); });});
describe("parseMarkdownToBlocks — tables", () => { test("pipe-delimited table parses correctly", () => { const md = "| A | B |\n|---|---|\n| 1 | 2 |"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("table"); });
/** * Prose containing pipe characters (e.g. TypeScript union types in inline * code) must NOT be treated as a table. Before the fix, the regex * matched any line with 2+ pipes. */ test("paragraph with inline code containing pipes is not a table", () => { const md = "The type is `'scroll' | 'wrap'` and supports `'a' | 'b' | 'c'` values."; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("paragraph"); expect(blocks[0].content).toBe(md); });
test("paragraph with multiple pipes in prose is not a table", () => { const md = "Use option A | B | C depending on context."; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("paragraph"); });
test("real-world plan: prose with union types is not a table", () => { const md = "`@pierre/diffs` supports `overflow: 'scroll' | 'wrap'` plus options, but Plannotator doesn't expose any of them."; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("paragraph"); });});
describe("parseMarkdownToBlocks — real-world plan regression", () => { test("indented code fence inside list does not swallow rest of plan", () => { const md = [ "### 5. Migrate App.tsx", "", "- **Remove** `useState` for `diffStyle`", "- **Replace** with ConfigStore reads:", " ```ts", " const diffStyle = useConfigValue('diffStyle');", " ```", "- **Update** toolbar toggle handler", "", "### 6. Update DiffViewer", ].join("\n"); const blocks = parseMarkdownToBlocks(md); const types = blocks.map((b) => b.type); expect(types).toEqual([ "heading", // ### 5. Migrate App.tsx "list-item", // - **Remove** "list-item", // - **Replace** "code", // ```ts ... ``` "list-item", // - **Update** "heading", // ### 6. Update DiffViewer ]); expect(blocks[3].type).toBe("code"); expect(blocks[3].language).toBe("ts"); expect(blocks[4].type).toBe("list-item"); expect(blocks[5].type).toBe("heading"); });});
describe("parseMarkdownToBlocks — list continuation lines", () => { test("indented continuation line merges into preceding list item", () => { const md = "- First item with text\n that continues here\n- Second item"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].content).toBe("First item with text\nthat continues here"); expect(blocks[1].type).toBe("list-item"); expect(blocks[1].content).toBe("Second item"); });
test("multiple continuation lines merge into one list item", () => { const md = "- Line one\n line two\n line three\n- Next"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].content).toBe("Line one\nline two\nline three"); });
test("non-indented line after list item starts a new paragraph", () => { const md = "- Item\nNot indented"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("list-item"); expect(blocks[1].type).toBe("paragraph"); expect(blocks[1].content).toBe("Not indented"); });
test("blank line between list item and indented text merges as loose continuation", () => { const md = "- Item\n\n Indented paragraph"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].content).toBe("Item\n\nIndented paragraph"); });
test("loose continuation with multiple paragraphs", () => { const md = "- Item\n\n Para one\n\n Para two"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].content).toBe("Item\n\nPara one\n\nPara two"); });
test("loose continuation stops at non-indented line", () => { const md = "- Item\n\n Indented\n\nNot indented"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].content).toBe("Item\n\nIndented"); expect(blocks[1].type).toBe("paragraph"); expect(blocks[1].content).toBe("Not indented"); });
test("loose continuation stops at next list item", () => { const md = "- First\n\n Body of first\n\n- Second"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].content).toBe("First\n\nBody of first"); expect(blocks[1].type).toBe("list-item"); expect(blocks[1].content).toBe("Second"); });
test("loose continuation works with ordered lists", () => { const md = "1. First\n\n Body of first\n\n2. Second"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].ordered).toBe(true); expect(blocks[0].content).toBe("First\n\nBody of first"); expect(blocks[1].type).toBe("list-item"); expect(blocks[1].content).toBe("Second"); });
test("loose continuation with mixed tight and loose lines", () => { const md = "- Item\n\n Para one\n still para one\n\n Para two"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].content).toBe("Item\n\nPara one\nstill para one\n\nPara two"); });
test("single-space indent after blank line does not merge (insufficient indentation)", () => { const md = "- Item\n\n Barely indented"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("list-item"); expect(blocks[1].type).toBe("paragraph"); });
test("continuation does not swallow nested list items", () => { const md = "- Parent\n - Child\n- Sibling"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(3); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].content).toBe("Parent"); expect(blocks[1].type).toBe("list-item"); expect(blocks[1].content).toBe("Child"); expect(blocks[2].type).toBe("list-item"); expect(blocks[2].content).toBe("Sibling"); });
test("continuation works when list item follows a blank line", () => { const md = "Some paragraph\n\n- Item with continuation\n that continues here"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("paragraph"); expect(blocks[1].type).toBe("list-item"); expect(blocks[1].content).toBe("Item with continuation\nthat continues here"); });
test("block-level elements after list items are not swallowed", () => { const md = "- Item\n# Heading"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("list-item"); expect(blocks[1].type).toBe("heading"); });});
describe("parseMarkdownToBlocks — ordered lists", () => { test("numeric markers produce ordered list items with orderedStart", () => { const md = "1. first\n2. second"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("list-item"); expect(blocks[0].ordered).toBe(true); expect(blocks[0].orderedStart).toBe(1); expect(blocks[0].content).toBe("first"); expect(blocks[1].ordered).toBe(true); expect(blocks[1].orderedStart).toBe(2); expect(blocks[1].content).toBe("second"); });
test("ordered list can start at an arbitrary number", () => { const md = "5. five\n6. six"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].orderedStart).toBe(5); expect(blocks[1].orderedStart).toBe(6); });
test("bullet markers stay unordered (no ordered flag)", () => { const md = "- a\n* b"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].ordered).toBeUndefined(); expect(blocks[0].orderedStart).toBeUndefined(); expect(blocks[1].ordered).toBeUndefined(); });
test("mixed bullet/numeric/bullet run preserves per-item ordered flag", () => { const md = "- a\n1. b\n- c"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(3); expect(blocks[0].ordered).toBeUndefined(); expect(blocks[1].ordered).toBe(true); expect(blocks[1].orderedStart).toBe(1); expect(blocks[2].ordered).toBeUndefined(); });
test("nested ordered item inside an unordered parent keeps its ordered flag", () => { const md = "- parent\n 1. child\n 2. child two"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(3); expect(blocks[0].ordered).toBeUndefined(); expect(blocks[0].level).toBe(0); expect(blocks[1].ordered).toBe(true); expect(blocks[1].level).toBe(1); expect(blocks[1].orderedStart).toBe(1); expect(blocks[2].ordered).toBe(true); expect(blocks[2].level).toBe(1); expect(blocks[2].orderedStart).toBe(2); });
test("continuation line on an ordered item merges into content and preserves ordered flag", () => { const md = "1. first item\n continuation\n2. second"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].ordered).toBe(true); expect(blocks[0].content).toBe("first item\ncontinuation"); expect(blocks[1].ordered).toBe(true); expect(blocks[1].orderedStart).toBe(2); });
test("numeric checkbox sets both ordered and checked", () => { const md = "1. [ ] todo task\n2. [x] done task"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].ordered).toBe(true); expect(blocks[0].checked).toBe(false); expect(blocks[0].content).toBe("todo task"); expect(blocks[1].ordered).toBe(true); expect(blocks[1].checked).toBe(true); expect(blocks[1].content).toBe("done task"); });
test("'1.5 second' is not parsed as a list item (no whitespace after the dot)", () => { const md = "1.5 second response time"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("paragraph"); expect(blocks[0].ordered).toBeUndefined(); });
test("heading branch wins over list branch for '### 1. Foo'", () => { const md = "### 1. Foo"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("heading"); });});
describe("parseMarkdownToBlocks — blockquotes", () => { test("consecutive '>' lines merge into one blockquote block", () => { const md = "> line one\n> line two\n> line three"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].content).toBe("line one\nline two\nline three"); });
test("blank line between '>' lines starts a new blockquote block", () => { const md = "> first\n\n> second"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].content).toBe("first"); expect(blocks[1].type).toBe("blockquote"); expect(blocks[1].content).toBe("second"); });
test("blockquote followed by paragraph does not absorb the paragraph", () => { const md = "> quote\nparagraph"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].content).toBe("quote"); expect(blocks[1].type).toBe("paragraph"); });
test("paragraph followed by blockquote does not merge", () => { const md = "intro\n> quote"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("paragraph"); expect(blocks[1].type).toBe("blockquote"); expect(blocks[1].content).toBe("quote"); });
test("single-line blockquote still produces one block", () => { const md = "> just one"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].content).toBe("just one"); });
test("quoted ordered list does NOT merge — each '> N.' line stays as its own block", () => { // Regression guard for review comment #7. Merging would flatten the // markers into run-on inline text; keeping them as separate blockquote // blocks preserves each line's visual identity. const md = "> 1. First item\n> 2. Second item\n> 3. Third item"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(3); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].content).toBe("1. First item"); expect(blocks[1].type).toBe("blockquote"); expect(blocks[1].content).toBe("2. Second item"); expect(blocks[2].type).toBe("blockquote"); expect(blocks[2].content).toBe("3. Third item"); });
test("quoted unordered list does NOT merge", () => { const md = "> - First\n> - Second\n> - Third"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(3); expect(blocks.every(b => b.type === "blockquote")).toBe(true); expect(blocks.map(b => b.content)).toEqual(["- First", "- Second", "- Third"]); });
test("quoted heading does NOT merge into previous blockquote", () => { const md = "> intro text\n> # Heading inside quote\n> more text"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(3); expect(blocks[0].content).toBe("intro text"); expect(blocks[1].content).toBe("# Heading inside quote"); expect(blocks[2].content).toBe("more text"); });
test("quoted code fence line does NOT merge", () => { const md = "> some prose\n> ```js\n> more prose"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(3); expect(blocks[0].content).toBe("some prose"); expect(blocks[1].content).toBe("```js"); expect(blocks[2].content).toBe("more prose"); });
test("nested blockquote (> >) does NOT merge into the outer quote", () => { const md = "> outer quote\n> > nested quote\n> back to outer"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(3); expect(blocks[0].content).toBe("outer quote"); expect(blocks[1].content).toBe("> nested quote"); expect(blocks[2].content).toBe("back to outer"); });
test("wrapped prose quote still merges (regression guard for the merge fix)", () => { // This is the case the merge fix was added for — a single logical // paragraph wrapped across multiple source lines. Must still merge. const md = "> This is a long quoted paragraph\n> that wraps across several\n> source lines for readability."; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].content).toBe( "This is a long quoted paragraph\nthat wraps across several\nsource lines for readability." ); });
test("prose quote followed by a quoted list: prose merges, list lines stay separate", () => { const md = "> intro prose\n> that wraps here\n> 1. first step\n> 2. second step"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(3); expect(blocks[0].content).toBe("intro prose\nthat wraps here"); expect(blocks[1].content).toBe("1. first step"); expect(blocks[2].content).toBe("2. second step"); });
test("multi-paragraph blockquote encodes paragraph break as double newline", () => { // The empty `>` line sits between two quoted paragraphs. We merge all // three `>` lines into one block, but the blank `>` becomes an empty // string, leaving a `\n\n` in the content so the renderer can split on // paragraph breaks and emit two <p> children. const md = "> first paragraph\n>\n> second paragraph"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].content).toBe("first paragraph\n\nsecond paragraph"); expect(blocks[0].content.split(/\n\n+/)).toEqual([ "first paragraph", "second paragraph", ]); });});
describe("parseMarkdownToBlocks — GitHub alerts", () => { test("detects NOTE alert and strips marker from content", () => { const md = "> [!NOTE]\n> Useful information."; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].alertKind).toBe("note"); expect(blocks[0].content).toBe("Useful information."); });
test("detects each GitHub alert kind, case-insensitive", () => { for (const kind of ["NOTE", "TIP", "WARNING", "CAUTION", "IMPORTANT"]) { const blocks = parseMarkdownToBlocks(`> [!${kind}]\n> body`); expect(blocks[0].alertKind).toBe(kind.toLowerCase() as 'note' | 'tip' | 'warning' | 'caution' | 'important'); } const lower = parseMarkdownToBlocks("> [!note]\n> body"); expect(lower[0].alertKind).toBe("note"); });
test("alert marker alone (no body) still tags the block", () => { const md = "> [!TIP]"; const blocks = parseMarkdownToBlocks(md); expect(blocks[0].alertKind).toBe("tip"); expect(blocks[0].content).toBe(""); });
test("blockquote without marker has no alertKind", () => { const blocks = parseMarkdownToBlocks("> just a quote"); expect(blocks[0].alertKind).toBeUndefined(); });
test("alert body absorbs a list, producing one block with list-formatted content", () => { const md = "> [!NOTE]\n> - first\n> - second"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].alertKind).toBe("note"); expect(blocks[0].content).toBe("- first\n- second"); });
test("alert body absorbs a code fence", () => { const md = "> [!WARNING]\n> ```\n> danger\n> ```"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].alertKind).toBe("warning"); expect(blocks[0].content).toBe("```\ndanger\n```"); });
test("blank line ends an alert", () => { const md = "> [!TIP]\n> body line\n\nafter"; const blocks = parseMarkdownToBlocks(md); expect(blocks[0].type).toBe("blockquote"); expect(blocks[0].alertKind).toBe("tip"); expect(blocks[0].content).toBe("body line"); expect(blocks[1].type).toBe("paragraph"); });
test("marker-like text mid-quote is not treated as alert", () => { const md = "> intro\n> [!NOTE]"; const blocks = parseMarkdownToBlocks(md); expect(blocks[0].alertKind).toBeUndefined(); expect(blocks[0].content).toBe("intro\n[!NOTE]"); });});
describe("parseMarkdownToBlocks — directive containers", () => { test("captures body between :::kind and :::", () => { const md = ":::note\nBody line.\n:::"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("directive"); expect(blocks[0].directiveKind).toBe("note"); expect(blocks[0].content).toBe("Body line."); });
test("supports arbitrary kinds (info, success, danger)", () => { for (const kind of ["info", "success", "danger", "warning"]) { const blocks = parseMarkdownToBlocks(`:::${kind}\nbody\n:::`); expect(blocks[0].directiveKind).toBe(kind); } });
test("multi-paragraph body keeps blank-line separator", () => { const md = ":::tip\npara 1\n\npara 2\n:::"; const blocks = parseMarkdownToBlocks(md); expect(blocks[0].content).toBe("para 1\n\npara 2"); });
test("unterminated directive absorbs rest of document", () => { // Not ideal, but prevents silent loss — user sees the whole body styled. const md = ":::note\nbody\nmore body"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("directive"); expect(blocks[0].content).toBe("body\nmore body"); });
test(":::kind with extra spaces still parses", () => { const md = "::: note \nbody\n:::"; const blocks = parseMarkdownToBlocks(md); expect(blocks[0].type).toBe("directive"); expect(blocks[0].directiveKind).toBe("note"); });});
describe("parseMarkdownToBlocks — raw HTML blocks", () => { test("<details>/<summary> parsed as a single html block", () => { const md = "<details>\n<summary>Title</summary>\nBody text\n</details>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("html"); expect(blocks[0].content).toBe(md); });
test("blank line terminates the HTML block", () => { const md = "<details>\n<summary>T</summary>\n</details>\n\nAfter paragraph"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("html"); expect(blocks[0].content).toBe("<details>\n<summary>T</summary>\n</details>"); expect(blocks[1].type).toBe("paragraph"); expect(blocks[1].content).toBe("After paragraph"); });
test("EOF terminates the HTML block", () => { const md = "<details>\n<summary>T</summary>\n</details>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("html"); });
test("paragraph before HTML block is separated correctly", () => { const md = "Some intro\n\n<details>\n<summary>T</summary>\n</details>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("paragraph"); expect(blocks[0].content).toBe("Some intro"); expect(blocks[1].type).toBe("html"); });
test("non-allowlisted tag (<xyz>) falls through to paragraph — preserves prior behavior", () => { const md = "<xyz>not a block</xyz>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("paragraph"); expect(blocks[0].content).toBe("<xyz>not a block</xyz>"); });
test("inline HTML in the middle of a paragraph is NOT a block", () => { // Line does not start with `<tag`, so the paragraph path wins. const md = "Press <kbd>Ctrl</kbd> to submit"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("paragraph"); });
test("nested tags stay in one block", () => { const md = "<details>\n<summary>Outer</summary>\n<details>\n<summary>Inner</summary>\nnested\n</details>\n</details>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("html"); expect(blocks[0].content).toBe(md); });
test("case-insensitive tag detection", () => { const md = "<DETAILS>\n<SUMMARY>T</SUMMARY>\n</DETAILS>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("html"); });
test("multiple HTML blocks separated by blank lines produce multiple blocks", () => { const md = "<details>\n<summary>A</summary>\n</details>\n\n<details>\n<summary>B</summary>\n</details>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("html"); expect(blocks[1].type).toBe("html"); });
test("startLine points to first line of the HTML block", () => { const md = "intro\n\n<details>\n<summary>T</summary>\n</details>"; const blocks = parseMarkdownToBlocks(md); expect(blocks[1].type).toBe("html"); expect(blocks[1].startLine).toBe(3); });
test("single-line inline HTML block (open + close on one line) is captured", () => { const md = "<details><summary>T</summary>body</details>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("html"); expect(blocks[0].content).toBe(md); });
test("blank line inside <details> does NOT terminate the block (GitHub-flavored)", () => { const md = "<details>\n<summary>Title</summary>\n\nBody across blanks.\n\n</details>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("html"); expect(blocks[0].content).toBe(md); });
test("nested same-tag open/close is balanced (not terminated by first close)", () => { const md = "<details>\n<summary>Outer</summary>\n<details>\n<summary>Inner</summary>\n</details>\nouter tail\n</details>"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(1); expect(blocks[0].type).toBe("html"); expect(blocks[0].content).toBe(md); });
test("trailing paragraph after closed <details> is a separate block", () => { const md = "<details>\n<summary>T</summary>\n\nBody\n\n</details>\n\nAfter paragraph"; const blocks = parseMarkdownToBlocks(md); expect(blocks).toHaveLength(2); expect(blocks[0].type).toBe("html"); expect(blocks[1].type).toBe("paragraph"); expect(blocks[1].content).toBe("After paragraph"); });});
describe("computeListIndices", () => { test("all unordered → all null", () => { const blocks = [li(0, false), li(0, false), li(0, false)]; expect(computeListIndices(blocks)).toEqual([null, null, null]); });
test("simple ordered run numbers sequentially from orderedStart of first item", () => { const blocks = [li(0, true, 1), li(0, true, 2), li(0, true, 3)]; expect(computeListIndices(blocks)).toEqual([1, 2, 3]); });
test("ordered run starting at 5 numbers from 5", () => { const blocks = [li(0, true, 5), li(0, true, 6)]; expect(computeListIndices(blocks)).toEqual([5, 6]); });
test("renumbering ignores subsequent orderedStart values (CommonMark)", () => { // Source markdown `1. / 2. / 99.` should render as 1, 2, 3 const blocks = [li(0, true, 1), li(0, true, 2), li(0, true, 99)]; expect(computeListIndices(blocks)).toEqual([1, 2, 3]); });
test("unordered item breaks the ordered streak; next ordered restarts from its orderedStart", () => { const blocks = [ li(0, true, 1), li(0, true, 2), li(0, false), li(0, true, 1), ]; expect(computeListIndices(blocks)).toEqual([1, 2, null, 1]); });
test("unordered nested children do not break top-level numbering", () => { // 1. a // - bullet // - bullet // 2. b const blocks = [ li(0, true, 1), li(1, false), li(1, false), li(0, true, 2), ]; expect(computeListIndices(blocks)).toEqual([1, null, null, 2]); });
test("nested ordered sublists number independently and reset between siblings", () => { // 1. a // 1. a.1 // 2. a.2 // 2. b // 1. b.1 const blocks = [ li(0, true, 1), li(1, true, 1), li(1, true, 2), li(0, true, 2), li(1, true, 1), ]; expect(computeListIndices(blocks)).toEqual([1, 1, 2, 2, 1]); });
test("ordered sublist after an unordered sub-bullet restarts from its source orderedStart", () => { // 1. a // - bullet // 2. honored as 2 because the source said `2.` const blocks = [ li(0, true, 1), li(1, false), li(1, true, 2), ]; expect(computeListIndices(blocks)).toEqual([1, null, 2]); });
test("mixed sub-bullets between ordered top-level items keep top-level streak alive", () => { // 1. a // - sub // 2. b // - sub // 3. c const blocks = [ li(0, true, 1), li(1, false), li(0, true, 2), li(1, false), li(0, true, 3), ]; expect(computeListIndices(blocks)).toEqual([1, null, 2, null, 3]); });
test("empty input returns empty array", () => { expect(computeListIndices([])).toEqual([]); });
test("single ordered item with no orderedStart defaults to 1", () => { const blocks = [{ ...li(0, true), orderedStart: undefined }]; expect(computeListIndices(blocks)).toEqual([1]); });});
describe("extractFrontmatter — contentStartLine", () => { test("no frontmatter → contentStartLine is 1", () => { const { contentStartLine } = extractFrontmatter("# Hello\nworld"); expect(contentStartLine).toBe(1); });
test("standard frontmatter offsets correctly", () => { const md = "---\ntitle: foo\ndate: bar\n---\n# Hello"; const { contentStartLine, content } = extractFrontmatter(md); expect(content).toBe("# Hello"); expect(contentStartLine).toBe(5); });
test("frontmatter with blank line before content", () => { const md = "---\ntitle: foo\n---\n\n# Hello"; const { contentStartLine } = extractFrontmatter(md); expect(contentStartLine).toBe(5); });
test("unclosed frontmatter treated as no frontmatter", () => { const md = "---\ntitle: foo\n# Not closed"; const { contentStartLine, content } = extractFrontmatter(md); expect(contentStartLine).toBe(1); expect(content).toBe(md); });});
describe("parseMarkdownToBlocks — startLine accuracy", () => { test("basic blocks get correct startLine", () => { const md = "# Heading\n\nParagraph\n\n- Item"; const blocks = parseMarkdownToBlocks(md); expect(blocks[0].startLine).toBe(1); // # Heading expect(blocks[1].startLine).toBe(3); // Paragraph expect(blocks[2].startLine).toBe(5); // - Item });
test("code block startLine points to the opening fence", () => { const md = "intro\n\n```ts\nconst x = 1;\nconst y = 2;\n```\n\noutro"; const blocks = parseMarkdownToBlocks(md); expect(blocks[0].startLine).toBe(1); // intro expect(blocks[1].startLine).toBe(3); // ```ts expect(blocks[1].type).toBe("code"); expect(blocks[2].startLine).toBe(8); // outro });
test("frontmatter shifts all startLines", () => { const md = "---\ntitle: test\n---\n\n# Heading\n\nParagraph"; const blocks = parseMarkdownToBlocks(md); expect(blocks[0].type).toBe("heading"); expect(blocks[0].startLine).toBe(5); expect(blocks[1].type).toBe("paragraph"); expect(blocks[1].startLine).toBe(7); });
test("table gets correct startLine", () => { const md = "# Title\n\n| A | B |\n|---|---|\n| 1 | 2 |"; const blocks = parseMarkdownToBlocks(md); expect(blocks[1].type).toBe("table"); expect(blocks[1].startLine).toBe(3); });});
describe("exportAnnotations — line labels", () => { const blocks = parseMarkdownToBlocks("# Heading\n\nShort paragraph\n\n```ts\nline1\nline2\nline3\n```");
test("single-line block shows 'line N'", () => { const anns = [{ blockId: blocks[0].id, type: "COMMENT", text: "fix this", originalText: "Heading", startOffset: 0 }]; const output = exportAnnotations(blocks, anns); expect(output).toContain("(line 1)"); });
test("multi-line code block shows line range", () => { const codeBlock = blocks.find(b => b.type === "code")!; const anns = [{ blockId: codeBlock.id, type: "COMMENT", text: "refactor", originalText: "line1", startOffset: 0 }]; const output = exportAnnotations(blocks, anns); expect(output).toMatch(/\(lines 5–9\)/); });
test("GLOBAL_COMMENT has no line label", () => { const anns = [{ blockId: "global", type: "GLOBAL_COMMENT", text: "overall feedback", originalText: "", startOffset: 0 }]; const output = exportAnnotations(blocks, anns); expect(output).not.toMatch(/\(line/); });
test("diff-context annotation shows label instead of line number", () => { const anns = [{ blockId: blocks[0].id, type: "COMMENT", text: "change", originalText: "Heading", startOffset: 0, diffContext: "added" }]; const output = exportAnnotations(blocks, anns); expect(output).not.toMatch(/\(line/); expect(output).toContain("[In diff content]"); });
test("sourceConverted adds caveat", () => { const output = exportAnnotations(blocks, [{ blockId: blocks[0].id, type: "COMMENT", text: "ok", originalText: "Heading", startOffset: 0 }], [], "Feedback", "plan", { sourceConverted: true }); expect(output).toContain("converted markdown"); });
test("no sourceConverted means no caveat", () => { const output = exportAnnotations(blocks, [{ blockId: blocks[0].id, type: "COMMENT", text: "ok", originalText: "Heading", startOffset: 0 }]); expect(output).not.toContain("converted markdown"); });});