import assert from "node:assert/strict"; import test from "node:test"; import { formatText, paragraphText, insertBlock, type MarkdownEdit, type InlineFormat } from "../../../src/lib/editor/markdownCommands.ts"; const apply = (source: string, edit: MarkdownEdit) => source.slice(0, edit.from) + edit.insert + source.slice(edit.to); test("inline formatting toggles without losing surrounding prose or the selected text", () => { for (const format of ["bold", "italic", "strike", "highlight", "code", "math", "comment"] as InlineFormat[]) { const source = "before words after", selection = { from: 7, to: 12 }; const edit = formatText(source, selection, format), changed = apply(source, edit); assert.equal(changed.slice(edit.anchor, edit.head), "words"); assert.equal(apply(changed, formatText(changed, { from: edit.anchor, to: edit.head }, format)), source, format); assert.equal(apply(changed, formatText(changed, { from: edit.from, to: edit.from + edit.insert.length }, format)), source, format); } assert.equal(apply("word ", formatText("word ", { from: 0, to: 5 }, "bold")), "**word** "); assert.equal(apply("**bold**", formatText("**bold**", { from: 0, to: 8 }, "italic")), "***bold***"); const empty = formatText("", { from: 0, to: 0 }, "bold"); assert.equal(empty.insert, "****"); assert.equal(empty.anchor, 2); assert.equal(empty.head, 2); }); test("code delimiters survive backticks and clear formatting preserves literal punctuation and newlines", () => { const source = "a ` b", edit = formatText(source, { from: 0, to: source.length }, "code"); assert.equal(edit.insert, "``a ` b``"); assert.equal(apply(edit.insert, formatText(edit.insert, { from: edit.anchor, to: edit.head }, "code")), source); assert.equal(apply(edit.insert, formatText(edit.insert, { from: 0, to: edit.insert.length }, "code")), source); const rich = "**bold**\r\n==mark== and \\*literal\\*"; assert.equal(formatText(rich, { from: 0, to: rich.length }, "clear").insert, "bold\r\nmark and \\*literal\\*"); assert.equal(apply("***bold***", formatText("***bold***", { from: 3, to: 7 }, "clear")), "bold"); }); test("paragraph commands cover selected lines, omit an unselected next line, and preserve indentation and CRLF", () => { const source = "before\r\n - [x] first\r\n 2. second\r\nafter"; const selected = { from: source.indexOf("first"), to: source.indexOf("after") }; assert.equal(apply(source, paragraphText(source, selected, 3)), "before\r\n ### first\r\n ### second\r\nafter"); assert.equal(apply(source, paragraphText(source, selected, "body")), "before\r\n first\r\n second\r\nafter"); assert.equal(paragraphText("one\ntwo\n\nthree", { from: 0, to: 14 }, "number").insert, "1. one\n2. two\n\n1. three"); assert.equal(paragraphText("", { from: 0, to: 0 }, "task").insert, "- [ ] "); assert.equal(apply("one\ntwo", paragraphText("one\ntwo", { from: 4, to: 4 }, "quote")), "one\n> two"); }); test("block inserts retain adjacent text, escape code fences, and select editable placeholders", () => { const source = "left right", selection = { from: 5, to: 10 }; for (const kind of ["table", "rule"] as const) { const result = apply(source, insertBlock(source, selection, kind)); assert.ok(result.startsWith(source + "\n\n")); } const code = insertBlock("```", { from: 0, to: 3 }, "code"); assert.equal(code.insert, "````\n```\n````\n\n"); const table = insertBlock("", { from: 0, to: 0 }, "table"); assert.equal(table.insert.slice(table.anchor, table.head), "Column 1"); assert.equal(insertBlock("a\r\nb", { from: 3, to: 4 }, "callout").insert, "\r\n> [!note]\r\n> b\r\n\r\n"); }); test("footnotes get unique references, retain the document tail, and select the definition", () => { const source = "One[^1] and words.\n\n[^1]: existing\n"; const from = source.indexOf("words"), edit = insertBlock(source, { from, to: from + 5 }, "footnote"); const changed = apply(source, edit); assert.equal(changed, "One[^1] and [^2].\n\n[^1]: existing\n\n[^2]: words\n"); assert.equal(changed.slice(edit.anchor, edit.head), "words"); });