import assert from "node:assert/strict"; import test from "node:test"; import { appendTableColumn, appendTableRow, parseMarkdownTable, replaceTableCell } from "../src/lib/editor/markdownTable.ts"; import { tableCellMarkdown, extractedCellMarkdown } from "../src/lib/editor/tableCellMarkdown.ts"; import { renderMarkdownHtml } from "../src/lib/workspace/noteHtml.ts"; const table = "| Ticker | Notes |\n| :--- | ---: |\n| IBKR.US | First
Second |"; test("extracted cell text keeps formatting and literal code while converting actual line breaks", () => { assert.equal(extractedCellMarkdown([ { type: "text", text: "Title", marks: [{ type: "bold" }] }, { type: "hardBreak" }, { type: "text", text: "
", marks: [{ type: "code" }] }, ]), "**Title**\n`
`"); }); test("table column insertion retains alignment and existing cell contents", () => { assert.equal(appendTableColumn(table), "| Ticker | Notes | |\n| :--- | ---: | --- |\n| IBKR.US | First
Second | |"); }); test("table row insertion keeps existing source byte-for-byte", () => { assert.equal(appendTableRow(table + "\n\n"), table + "\n| | |\n\n"); }); test("table edits preserve CRLF and optional outer pipes", () => { const source = "Name | Link\r\n--- | :---:\r\nOne | [note](note.md)\r\n"; assert.equal(appendTableRow(source), "Name | Link\r\n--- | :---:\r\nOne | [note](note.md)\r\n| | |\r\n"); assert.equal(appendTableColumn(source), "| Name | Link | |\r\n| --- | :---: | --- |\r\n| One | [note](note.md) | |\r\n"); }); test("escaped pipes in text, wiki aliases, and inline code stay inside cells", () => { const source = "| One | Two |\n| --- | --- |\n| [[note\\|Alias]] | `a\\|b` and text\\|value |"; const parsed = parseMarkdownTable(source)!; assert.equal(parsed.rows[2].cells.length, 2); const changed = appendTableColumn(source); assert.ok(changed.includes("| [[note\\|Alias]] | `a\\|b` and text\\|value | |")); for (const row of parsed.rows) for (const cell of row.cells) assert.equal(source.slice(cell.from, cell.to), cell.text); }); test("adding columns pads short rows without losing extra source cells", () => { const source = "| A | B |\n| --- | --- |\n| short |\n| a | b | extra |"; assert.equal(appendTableColumn(source), "| A | B | |\n| --- | --- | --- |\n| short | | |\n| a | b | | extra |"); }); test("table controls handle header-only and single-column tables", () => { assert.equal(appendTableRow("| Header |\n| --- |"), "| Header |\n| --- |\n| |"); assert.equal(parseMarkdownTable(appendTableColumn("| Header |\n| --- |"))?.columns, 2); }); test("invalid tables are unchanged", () => { for (const source of ["Just text", "| A | B |\n| --- |", "| A |\n| not a delimiter |"] ) { assert.equal(parseMarkdownTable(source), null); assert.equal(appendTableColumn(source), source); assert.equal(appendTableRow(source), source); } }); test("editing one cell preserves padding, alignment, other cells and CRLF", () => { const source = " | Name | Notes |\r\n | :--- | ---: |\r\n | One | first
~~old~~ |\r\n\r\nTail"; assert.equal(replaceTableCell(source, 2, 1, "next
line\\|pipe"), source.replace("first
~~old~~", "next
line\\|pipe")); assert.equal(replaceTableCell(source, 0, 0, "Title"), source.replace("Name", "Title")); assert.equal(replaceTableCell(source, 1, 0, "bad"), source); assert.equal(replaceTableCell(source, 2, 1, "break\nrow"), source); }); test("editing an empty or omitted cell does not overwrite its neighbor", () => { assert.equal(replaceTableCell("| A | B |\n| --- | --- |\n| short |", 2, 1, "new"), "| A | B |\n| --- | --- |\n| short | new |"); const source = "| A | B |\n| --- | --- |\n| | value |"; assert.equal(replaceTableCell(source, 2, 0, "new"), source.replace("| | value", "| new| value")); }); test("rich cell serialization retains breaks, formatting, links and literal punctuation", () => { const text = tableCellMarkdown({ type: "doc", content: [{ type: "paragraph", content: [ { type: "text", text: "old", marks: [{ type: "strike" }] }, { type: "hardBreak" }, { type: "text", text: "bold", marks: [{ type: "bold" }] }, { type: "text", text: " | *literal*
& value" }, { type: "hardBreak" }, { type: "text", text: "a|`b", marks: [{ type: "code" }] }, { type: "text", text: "note", marks: [{ type: "link", attrs: { href: "note.md", "data-kind": "wiki" } }] }, { type: "text", text: "web", marks: [{ type: "link", attrs: { href: "https://example.com/a(b)" } }] }, ] }, { type: "paragraph", content: [{ type: "text", text: "Pasted paragraph" }] }] }); const source = `| A |\n| --- |\n| ${text} |`; assert.equal(parseMarkdownTable(source)?.rows[2].cells.length, 1); const html = renderMarkdownHtml(source, new Map()); assert.ok(html.includes("old")); assert.ok(html.includes("bold")); assert.ok(html.includes("| *literal* <br> & value")); assert.ok(html.includes("a|`b")); assert.ok(html.includes('href="note.md" data-kind="wiki"')); assert.ok(html.includes('href="https://example.com/a(b)"')); assert.equal((html.match(/
/g) || []).length, 3); });