RuneStone 1.0.0
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
// Ported from the Table Formulas plugin (tests/formulas.test.ts). The engine is now part of the host.
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { cellAddress, evaluateTableFormulas } from "../../../src/lib/formulas/engine.ts";
|
||||
|
||||
const calculate = (formula: string) => evaluateTableFormulas([[formula]])[0][0]!.text;
|
||||
test("aggregates distinguish separate cells and ranges from an arithmetic argument", () => {
|
||||
const evaluate = (formula: string) => evaluateTableFormulas([["Name", "B", "C", "Result"], ["First", "9", "10", formula]])[1][3]!;
|
||||
for (const [name, separate, combined] of [["SUM", "19", "19"], ["AVERAGE", "9.5", "19"], ["MIN", "9", "19"], ["MAX", "10", "19"], ["COUNT", "2", "1"]]) {
|
||||
for (const separator of [",", ":"]) {
|
||||
const result = evaluate(`=${name}(B2${separator}C2)`);
|
||||
assert.equal(result.text, separate);
|
||||
}
|
||||
const result = evaluate(`=${name}(B2+C2)`);
|
||||
assert.equal(result.text, combined);
|
||||
}
|
||||
assert.equal(evaluate("=(B2+C2)/2").text, "9.5");
|
||||
assert.equal(evaluate("=ROUND(AVERAGE(B2,C2),0)").text, "10");
|
||||
assert.equal(evaluate("=ABS(B2-C2)").text, "1");
|
||||
assert.equal(evaluate("=AVERAGE((B2+C2))").text, "19");
|
||||
});
|
||||
|
||||
test("arithmetic, precedence, parentheses, percentages and rounding", () => {
|
||||
for (const [formula, result] of [
|
||||
["=SUM(2+3)", "5"], ["=2+3*4", "14"], ["=(2+3)*4", "20"],
|
||||
["=2^3^2", "512"], ["=-2^2", "-4"], ["=2^-2", "0.25"],
|
||||
["=200*10%", "20"], ["=0.1+0.2", "0.3"], ["=1e2 + .5", "100.5"],
|
||||
["=ROUND(-1.25,1)", "-1.3"], ["=ROUND(1.005,2)", "1.01"],
|
||||
["=ROUND(125,-1)", "130"], ["=ABS(-4)", "4"], ["=sum(1,2,3)", "6"],
|
||||
]) assert.equal(calculate(formula), result, formula);
|
||||
});
|
||||
|
||||
test("same-table references, ranges, chained dependencies, and empty/text cells", () => {
|
||||
const results = evaluateTableFormulas([
|
||||
["Name", "Appearance", "Cha", "Combined"],
|
||||
["First", "9", "10", "=AVERAGE(B2:C2)"],
|
||||
["Second", "7", "9", "=SUM(B3,C3)/2"],
|
||||
["Total", "", "=B4+1", "=SUM(D2:D3)"],
|
||||
["Count", "=COUNT(A1:C4)", "=MAX(B2:C3)-MIN(B2:C3)", "=ROUND(D4/3,2)"],
|
||||
["", "=SUM($B$2:$C$3)", "=AVERAGE(B1:B4)", "=SUM(C3:B2)"],
|
||||
]);
|
||||
assert.equal(results[1][3]?.text, "9.5");
|
||||
assert.equal(results[2][3]?.text, "8");
|
||||
assert.equal(results[3][3]?.text, "17.5");
|
||||
assert.equal(results[3][2]?.text, "1");
|
||||
assert.equal(results[4][1]?.text, "5");
|
||||
assert.equal(results[4][2]?.text, "3");
|
||||
assert.equal(results[4][3]?.text, "5.83");
|
||||
assert.equal(results[5][1]?.text, "35");
|
||||
assert.equal(results[5][2]?.text, "8");
|
||||
assert.equal(results[5][3]?.text, "35");
|
||||
assert.equal(results[0][0], null);
|
||||
assert.equal(cellAddress(1, 26), "AA2");
|
||||
assert.equal(evaluateTableFormulas([Array.from({ length: 27 }, (_, c) => c === 0 ? "=AA1" : "3")])[0][0]?.text, "3");
|
||||
});
|
||||
|
||||
test("errors are visible, propagated, bounded, and never executed", () => {
|
||||
for (const [formula, result] of [
|
||||
["=1/0", "#DIV/0!"], ["=AVERAGE()", "#DIV/0!"], ["=A2", "#REF!"],
|
||||
["=B1:B999999999", "#REF!"], ["=A0", "#REF!"], ["=1+", "#ERROR!"],
|
||||
["=1 2", "#ERROR!"], ["=ROUND(1,2,3)", "#ERROR!"], ["=BOGUS(1)", "#NAME?"],
|
||||
["=A1", "#CYCLE!"], ["=1e999", "#NUM!"], ["=9^9999", "#NUM!"],
|
||||
["=globalThis.alert(1)", "#ERROR!"], ["=" + "(".repeat(100) + "1" + ")".repeat(100), "#LIMIT!"],
|
||||
]) assert.equal(calculate(formula), result, formula);
|
||||
const cycle = evaluateTableFormulas([["=B1", "=A1", "=SUM(A1:B1)", "=2+3"]]);
|
||||
assert.deepEqual(cycle[0].map(cell => cell?.text), ["#CYCLE!", "#CYCLE!", "#CYCLE!", "5"]);
|
||||
assert.equal(evaluateTableFormulas([["text", "=A1+1"]])[0][1]?.text, "#VALUE!");
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test, { afterEach } from "node:test";
|
||||
import { get } from "svelte/store";
|
||||
import MarkdownIt from "markdown-it";
|
||||
import { activeTableFormulas, renderTableFormulas, setTableFormulasEnabled, tableFormulaHtml, tableFormulas, tableFormulasEnabled } from "../../../src/lib/formulas/index.ts";
|
||||
import { tableCellMarkdown } from "../../../src/lib/editor/tableCellMarkdown.ts";
|
||||
import { renderMarkdownHtml, renderMarkdownSections } from "../../../src/lib/workspace/noteHtml.ts";
|
||||
|
||||
afterEach(() => setTableFormulasEnabled(true));
|
||||
|
||||
test("Markdown rendering isolates tables and preserves literal, formatted, and non-table text", () => {
|
||||
const source = "| X | Y | Result |\r\n| --- | --- | --- |\r\n| 9 | 10 | =AVERAGE(A2:B2) |\r\n\r\n| X | Y | Result |\r\n| --- | --- | --- |\r\n| 1 | 3 | =A2*B2 |\r\n";
|
||||
const html = renderMarkdownHtml(source, new Map());
|
||||
assert.match(html, /aria-label="=AVERAGE\(A2:B2\) → 9.5">9.5<\/span>/);
|
||||
assert.match(html, /aria-label="=A2\*B2 → 3">3<\/span>/);
|
||||
const sections = renderMarkdownSections(source, new Map());
|
||||
assert.equal(sections.map(s => source.slice(s.start, s.end)).join(""), source);
|
||||
assert.equal(sections.map(s => s.html).join(""), html);
|
||||
const literal = renderMarkdownHtml("=1+2\n\n| A | B | C |\n| --- | --- | --- |\n| `=1+2` | \\=1+2 | ==highlight== |", new Map());
|
||||
assert.doesNotMatch(literal, /md-table-formula/);
|
||||
assert.match(literal, /<mark>highlight<\/mark>/);
|
||||
const hostile = renderMarkdownHtml('| A |\n| --- |\n| ="<img src=x onerror=alert(1)>" |', new Map());
|
||||
assert.doesNotMatch(hostile, /<img/);
|
||||
assert.match(hostile, /#ERROR!/);
|
||||
});
|
||||
|
||||
test("editing keeps formulas readable and code or explicit escapes literal", () => {
|
||||
const doc = (text: string, marks?: { type: string }[]) => ({ type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text, marks }] }] });
|
||||
assert.equal(tableCellMarkdown(doc("=SUM($B$2:C2)*50%")), "=SUM($B$2:C2)*50%");
|
||||
assert.equal(tableCellMarkdown(doc("\\=1+2")), "\\=1+2");
|
||||
assert.equal(tableCellMarkdown(doc("=1+2", [{ type: "code" }])), "`=1+2`");
|
||||
});
|
||||
|
||||
test("the setting turns formulas off and back on without a change to the source", () => {
|
||||
const source = "| A | B |\n| --- | --- |\n| 2 | =A2*3 |\n";
|
||||
assert.equal(get(tableFormulasEnabled), true, "Formulas are on by default");
|
||||
assert.equal(activeTableFormulas(), tableFormulas);
|
||||
const enabled = renderMarkdownHtml(source, new Map());
|
||||
assert.match(enabled, />6<\/span>/);
|
||||
setTableFormulasEnabled(false);
|
||||
assert.equal(activeTableFormulas(), undefined);
|
||||
for (const html of [renderMarkdownHtml(source, new Map()), ...renderMarkdownSections(source, new Map()).map(section => section.html)]) {
|
||||
assert.doesNotMatch(html, /md-table-formula/);
|
||||
assert.match(html, /=A2\*3/);
|
||||
assert.match(html, /<table>/);
|
||||
}
|
||||
setTableFormulasEnabled(true);
|
||||
assert.equal(renderMarkdownHtml(source, new Map()), enabled);
|
||||
});
|
||||
|
||||
// Ported from the Table Formulas plugin (tests/render.test.ts).
|
||||
test("rendering separates tables and preserves literal expressions", () => {
|
||||
const markdown = new MarkdownIt();
|
||||
const source = "| A | B |\n| --- | --- |\n| 3 | =A2*2 |\n\n| A | B |\n| --- | --- |\n| 4 | =A2+1 |\n\n| Literal |\n| --- |\n| `=1+2` |\n| \\=1+2 |\n";
|
||||
const tokens = markdown.parse(source, {});
|
||||
renderTableFormulas(tokens);
|
||||
const html = markdown.renderer.render(tokens, markdown.options, {});
|
||||
assert.match(html, /aria-label="=A2\*2 → 6">6<\/span>/);
|
||||
assert.match(html, /aria-label="=A2\+1 → 5">5<\/span>/);
|
||||
assert.match(html, /<code>=1\+2<\/code>/);
|
||||
assert.equal((html.match(/class="md-table-formula"/g) || []).length, 2);
|
||||
});
|
||||
|
||||
test("formula labels escape markup and report errors", () => {
|
||||
const html = tableFormulaHtml({ formula: '=SUM("<img>")', text: "#ERROR!", error: "#ERROR!" });
|
||||
assert.doesNotMatch(html, /<img>/);
|
||||
assert.match(html, /"<img>"/);
|
||||
assert.match(html, /md-table-formula is-error/);
|
||||
assert.match(html, /Invalid formula syntax or function arguments/);
|
||||
});
|
||||
@@ -0,0 +1,120 @@
|
||||
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, renderMarkdownSections } from "../../../src/lib/workspace/noteHtml.ts";
|
||||
|
||||
const table = "| Ticker | Notes |\n| :--- | ---: |\n| IBKR.US | First<br>Second |";
|
||||
|
||||
test("prose directly after a table stays outside the table and its edit range", () => {
|
||||
for (const eol of ["\n", "\r\n"]) {
|
||||
const body = table.replaceAll("\n", eol);
|
||||
const tail = `${eol}Plain text${eol}More text with an escaped \\| pipe${eol}# Heading`;
|
||||
const source = body + tail;
|
||||
assert.equal(parseMarkdownTable(source)?.end, body.length);
|
||||
assert.equal(parseMarkdownTable(source)?.rows.length, 3);
|
||||
assert.equal(appendTableRow(source), body + `${eol}| | |` + tail);
|
||||
assert.equal(appendTableColumn(source), appendTableColumn(body) + tail);
|
||||
assert.equal(replaceTableCell(source, 3, 0, "changed"), source);
|
||||
const html = renderMarkdownHtml(source, new Map());
|
||||
assert.equal((html.match(/<tr>/g) || []).length, 2);
|
||||
assert.match(html, /<\/table>[\s\S]*<p>Plain text/);
|
||||
const sections = renderMarkdownSections(source, new Map());
|
||||
assert.equal(sections[0].end, body.length + eol.length);
|
||||
assert.equal(source.slice(sections[1].start).startsWith("Plain text"), true);
|
||||
}
|
||||
});
|
||||
|
||||
test("table boundaries distinguish escaped pipes from delimiters and work in nested blocks", () => {
|
||||
const body = "| A | B |\n| --- | --- |\nOne | Two\n| short |\n| escaped \\| pipe | value |\n| even \\\\| delimiter |";
|
||||
assert.equal(parseMarkdownTable(body + "\nProse \\| literal")?.rows.length, 6);
|
||||
assert.equal((renderMarkdownHtml(body + "\nProse \\| literal", new Map()).match(/<tr>/g) || []).length, 5);
|
||||
assert.match(renderMarkdownHtml("> | A |\n> | --- |\n> | B |\n> Paragraph", new Map()), /<\/table>[\s\S]*<p>Paragraph<\/p>/);
|
||||
assert.match(renderMarkdownHtml("- Item\n\n | A |\n | --- |\n | B |\n Paragraph", new Map()), /<\/table>[\s\S]*<p>Paragraph<\/p>/);
|
||||
});
|
||||
|
||||
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: "<br>", marks: [{ type: "code" }] },
|
||||
]), "**Title**\n`<br>`");
|
||||
});
|
||||
|
||||
test("table column insertion retains alignment and existing cell contents", () => {
|
||||
assert.equal(appendTableColumn(table), "| Ticker | Notes | |\n| :--- | ---: | --- |\n| IBKR.US | First<br>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<br />~~old~~ |\r\n\r\nTail";
|
||||
assert.equal(replaceTableCell(source, 2, 1, "next<br>line\\|pipe"), source.replace("first<br />~~old~~", "next<br>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* <br> & 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("<s>old</s>"));
|
||||
assert.ok(html.includes("<strong>bold</strong>"));
|
||||
assert.ok(html.includes("| *literal* <br> & value"));
|
||||
assert.ok(html.includes("<code>a|`b</code>"));
|
||||
assert.ok(html.includes('href="note.md" data-kind="wiki"'));
|
||||
assert.ok(html.includes('href="https://example.com/a(b)"'));
|
||||
assert.equal((html.match(/<br>/g) || []).length, 3);
|
||||
});
|
||||
@@ -0,0 +1,83 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { alignTableCells, clipboardCells, copyTableCells, copyTableMarkdown, deleteTableCells, formatTableCells, mapTableCells, pasteTableCells } from "../../../src/lib/editor/tableSelection.ts";
|
||||
import { htmlToMarkdown } from "../../../src/lib/workspace/htmlToMarkdown.ts";
|
||||
import { parseMarkdownTable } from "../../../src/lib/editor/markdownTable.ts";
|
||||
import { renderMarkdownHtml, plainInlineMarkdown } from "../../../src/lib/workspace/noteHtml.ts";
|
||||
import { tableCellMarkdown } from "../../../src/lib/editor/tableCellMarkdown.ts";
|
||||
|
||||
const source = "| A | B | C |\r\n| --- | :---: | ---: |\r\n| one | two | three |\r\n| four | five | six |\r\n\r\nTail";
|
||||
const range = { anchor: { row: 2, column: 1 }, head: { row: 1, column: 0 } };
|
||||
test("selected cells retain table structure and line breaks through HTML clipboard", () => {
|
||||
const source = "| Name | Notes |\n| --- | --- |\n| **Alpha** | First<br>Second |";
|
||||
const range = { anchor: { row: 0, column: 0 }, head: { row: 1, column: 1 } };
|
||||
const copied = copyTableMarkdown(source, range);
|
||||
const pasted = htmlToMarkdown(renderMarkdownHtml(copied, new Map()));
|
||||
const table = parseMarkdownTable(pasted)!;
|
||||
assert.equal(table.columns, 2);
|
||||
assert.equal(table.rows.length, 3);
|
||||
assert.equal(table.rows[2].cells[0].text, "**Alpha**");
|
||||
assert.equal(table.rows[2].cells[1].text, "First<br>Second");
|
||||
});
|
||||
test("reversed rectangular selections only change selected cells and preserve CRLF", () => {
|
||||
const changed = formatTableCells(source, range, "bold");
|
||||
assert.equal(changed, source.replace("one", "**one**").replace("two", "**two**").replace("four", "**four**").replace("five", "**five**"));
|
||||
assert.equal(formatTableCells(changed, range, "bold"), source);
|
||||
assert.equal(formatTableCells(source.replace("one", "**one**"), range, "bold"), changed);
|
||||
assert.equal(copyTableCells(source, range), "one\ttwo\nfour\tfive");
|
||||
assert.equal(alignTableCells(source, range, "left"), source.replace("| --- | :---: | ---: |", "| :--- | :--- | ---: |"));
|
||||
});
|
||||
test("all formats render and clear without interpreting escaped punctuation", () => {
|
||||
const cell = { anchor: { row: 1, column: 0 }, head: { row: 1, column: 0 } };
|
||||
for (const [format, html] of [["bold", "<strong>one</strong>"], ["italic", "<em>one</em>"], ["strike", "<s>one</s>"], ["highlight", "<mark>one</mark>"], ["code", "<code>one</code>"], ["math", 'class="md-inline-math">one</span>'], ["comment", 'class="md-inline-comment">one</span>']] as const) {
|
||||
const changed = formatTableCells(source, cell, format);
|
||||
assert.ok(renderMarkdownHtml(changed, new Map()).includes(html), format);
|
||||
assert.equal(formatTableCells(changed, cell, "clear"), source);
|
||||
}
|
||||
assert.equal(plainInlineMarkdown("**bold** ==*highlight*== ~~strike~~ `a|b`<br>\\*literal\\*"), "bold highlight strike a\\|b<br>\\*literal\\*");
|
||||
assert.equal(plainInlineMarkdown("**Photo** "), "Photo ");
|
||||
assert.ok(renderMarkdownHtml("`==not highlighted==` \\==literal\\==", new Map()).includes("<code>==not highlighted==</code>"));
|
||||
for (const type of ["highlight", "math", "comment"]) {
|
||||
const text = tableCellMarkdown({ type: "text", text: "a|b", marks: [{ type }] });
|
||||
assert.equal(parseMarkdownTable(`| H |\n| --- |\n| ${text} |`)?.rows[2].cells.length, 1);
|
||||
}
|
||||
});
|
||||
test("clipboard accepts Markdown and quoted TSV without splitting escaped pipes", () => {
|
||||
assert.deepEqual(clipboardCells('"a\tb"\t"line 1\nline 2"\r\n"say ""hi"""\tx|y\r\n'), [["a\tb", "line 1<br>line 2"], ['say "hi"', "x\\|y"]]);
|
||||
assert.deepEqual(clipboardCells("| A | B |\n| --- | --- |\n| a\\|b | `code` |"), [["A", "B"], ["a\\|b", "`code`"]]);
|
||||
const pasted = pasteTableCells(source, range, "value");
|
||||
assert.equal(copyTableCells(pasted, range), "value\tvalue\nvalue\tvalue");
|
||||
const large = pasteTableCells(source, { anchor: { row: 2, column: 2 }, head: { row: 2, column: 2 } }, "a\tb\nc\td");
|
||||
assert.equal(parseMarkdownTable(large)?.columns, 4);
|
||||
assert.equal(parseMarkdownTable(large)?.rows.length, 5);
|
||||
assert.ok(large.endsWith("\r\n\r\nTail"));
|
||||
assert.equal(copyTableCells(large, { anchor: { row: 2, column: 2 }, head: { row: 3, column: 3 } }), "a\tb\nc\td");
|
||||
});
|
||||
test("clear preserves structure; delete removes whole rows/columns or shifts partial cells", () => {
|
||||
assert.equal(copyTableCells(mapTableCells(source, range, () => ""), range), "\t\n\t");
|
||||
assert.equal(copyTableCells(deleteTableCells(source, range), range), "three\t\nsix\t");
|
||||
const rows = deleteTableCells(source, { anchor: { row: 1, column: 0 }, head: { row: 1, column: 2 } });
|
||||
assert.equal(parseMarkdownTable(rows)?.rows.length, 3);
|
||||
assert.ok(rows.includes("four")); assert.ok(!rows.includes("one"));
|
||||
const columns = deleteTableCells(source, { anchor: { row: 0, column: 1 }, head: { row: 2, column: 1 } });
|
||||
assert.equal(parseMarkdownTable(columns)?.columns, 2);
|
||||
assert.ok(columns.includes("| one | three |"));
|
||||
const header = deleteTableCells(source, { anchor: { row: 0, column: 0 }, head: { row: 0, column: 2 } });
|
||||
assert.ok(header.startsWith("| one | two | three |\r\n| --- | :---: | ---: |"));
|
||||
assert.equal(deleteTableCells(source, { anchor: { row: 0, column: 0 }, head: { row: 2, column: 2 } }), "\r\n\r\nTail");
|
||||
});
|
||||
|
||||
test("deleting the empty columns shown on the card removes their structure", () => {
|
||||
const emptyColumns = "| Column 2 | Column 1 | | | | |\n| --- | --- | --- | --- | --- | --- |\n| 2cm | Value | | | | |\n| text | 123 | | | | |\n| | | | | | |\n| | | | | | |\n\nAfter.";
|
||||
const result = deleteTableCells(emptyColumns, { anchor: { row: 4, column: 5 }, head: { row: 0, column: 3 } });
|
||||
assert.equal(parseMarkdownTable(result)?.columns, 3);
|
||||
assert.equal(parseMarkdownTable(result)?.rows.length, 6);
|
||||
assert.ok(result.includes("| 2cm | Value | |"));
|
||||
assert.ok(result.endsWith("\n\nAfter."));
|
||||
});
|
||||
|
||||
test("deleting the last body rows retains a valid header and alignment", () => {
|
||||
const result = deleteTableCells(source, { anchor: { row: 2, column: 2 }, head: { row: 1, column: 0 } });
|
||||
assert.equal(result, "| A | B | C |\r\n| --- | :---: | ---: |\r\n\r\nTail");
|
||||
assert.equal(parseMarkdownTable(result)?.rows.length, 2);
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { changeTableStructure, moveTableAxis, sortTableColumn } from "../../../src/lib/editor/tableStructure.ts";
|
||||
import { parseMarkdownTable } from "../../../src/lib/editor/markdownTable.ts";
|
||||
|
||||
const source = "| Name | Value |\r\n| :--- | ---: |\r\n| Zebra | `2` |\r\n| **Apple** | 10 |\r\n| apple | 3 |\r\n| | 1 |\r\n\r\nTail";
|
||||
const values = (text: string) => parseMarkdownTable(text)!.rows.map(row => row.cells.map(cell => cell.text));
|
||||
test("dragging rows across multiple positions preserves raw Markdown, header, CRLF and suffix", () => {
|
||||
const moved = moveTableAxis(source, "row", 1, 4);
|
||||
assert.deepEqual(values(moved).slice(2).map(row => row[0]), ["**Apple**", "apple", "", "Zebra"]);
|
||||
assert.ok(moved.includes("| Zebra | `2` |\r\n\r\nTail"));
|
||||
assert.equal(moveTableAxis(moved, "row", 4, 1), source);
|
||||
for (const [from, to] of [[0, 2], [2, 0], [1, 5], [-1, 2], [1, 1], [1.5, 2], [1, NaN]]) {
|
||||
assert.equal(moveTableAxis(source, "row", from, to), source);
|
||||
}
|
||||
});
|
||||
test("dragging columns carries alignment and rich contents across ragged rows", () => {
|
||||
const text = " | A | B | C |\r\n | :--- | :---: | ---: |\r\n | a\\|b | **bold**<br>line | [[note]] | extra |\r\n | short |\r\n\r\nTail";
|
||||
const moved = moveTableAxis(text, "column", 0, 2);
|
||||
assert.deepEqual(values(moved), [["B", "C", "A"], [":---:", "---:", ":---"], ["**bold**<br>line", "[[note]]", "a\\|b", "extra"], ["", "", "short"]]);
|
||||
assert.ok(moved.endsWith("\r\n\r\nTail"));
|
||||
assert.deepEqual(values(moveTableAxis(moved, "column", 2, 0)).slice(0, 3), values(text).slice(0, 3));
|
||||
assert.equal(moveTableAxis(text, "column", 0, 3), text);
|
||||
assert.equal(moveTableAxis("| H |\n| --- |", "column", 0, 0), "| H |\n| --- |");
|
||||
assert.equal(moveTableAxis("invalid", "row", 1, 2), "invalid");
|
||||
});
|
||||
test("row insertion, movement and duplication preserve header, raw cells and CRLF", () => {
|
||||
const inserted = changeTableStructure(source, "row", 1, "before");
|
||||
assert.equal(inserted, source.replace("| Zebra", "| | |\r\n| Zebra"));
|
||||
assert.equal(changeTableStructure(source, "row", 0, "after"), inserted);
|
||||
assert.equal(changeTableStructure(source, "row", 0, "before"), source);
|
||||
const moved = changeTableStructure(source, "row", 2, "previous");
|
||||
assert.equal(moved, source.replace("| Zebra | `2` |\r\n| **Apple** | 10 |", "| **Apple** | 10 |\r\n| Zebra | `2` |"));
|
||||
assert.equal(changeTableStructure(moved, "row", 1, "next"), source);
|
||||
assert.equal(changeTableStructure(source, "row", 1, "previous"), source);
|
||||
assert.equal(changeTableStructure(source, "row", 0, "next"), source);
|
||||
assert.equal(changeTableStructure(source, "row", 4, "next"), source);
|
||||
assert.equal(changeTableStructure(source, "row", 1, "duplicate"), source.replace("| Zebra | `2` |", "| Zebra | `2` |\r\n| Zebra | `2` |"));
|
||||
assert.ok(changeTableStructure(source, "row", 1, "delete").endsWith("\r\n\r\nTail"));
|
||||
});
|
||||
test("column movement and duplication carry header, contents and alignment together", () => {
|
||||
const moved = changeTableStructure(source, "column", 1, "previous");
|
||||
assert.deepEqual(values(moved), values(source).map(row => row.toReversed()));
|
||||
assert.deepEqual(values(changeTableStructure(moved, "column", 0, "next")), values(source));
|
||||
assert.deepEqual(values(changeTableStructure(source, "column", 0, "duplicate")), values(source).map(row => [row[0], ...row]));
|
||||
assert.deepEqual(values(changeTableStructure(source, "column", 1, "before")), values(source).map((row, i) => [row[0], i === 1 ? "---" : "", row[1]]));
|
||||
assert.deepEqual(values(changeTableStructure(source, "column", 1, "delete")), values(source).map(row => [row[0]]));
|
||||
assert.equal(changeTableStructure(source, "column", 0, "previous"), source);
|
||||
assert.equal(changeTableStructure(source, "column", 1, "next"), source);
|
||||
});
|
||||
test("sort uses visible text and natural numbers, is stable, leaves blanks last and header intact", () => {
|
||||
assert.deepEqual(values(sortTableColumn(source, 0)).slice(2).map(row => row[0]), ["**Apple**", "apple", "Zebra", ""]);
|
||||
assert.deepEqual(values(sortTableColumn(source, 0, true)).slice(2).map(row => row[0]), ["Zebra", "**Apple**", "apple", ""]);
|
||||
assert.deepEqual(values(sortTableColumn(source, 1)).slice(2).map(row => row[1]), ["1", "`2`", "3", "10"]);
|
||||
const sorted = sortTableColumn(source, 1, true);
|
||||
assert.ok(sorted.startsWith("| Name | Value |\r\n| :--- | ---: |"));
|
||||
assert.ok(sorted.includes("| Zebra | `2` |"));
|
||||
assert.ok(sorted.endsWith("\r\n\r\nTail"));
|
||||
});
|
||||
test("header-only, ragged and single-column tables remain valid through operations", () => {
|
||||
const header = "| H |\n| --- |";
|
||||
assert.equal(changeTableStructure(header, "row", 0, "duplicate"), header + "\n| H |");
|
||||
assert.equal(changeTableStructure(header, "column", 0, "delete"), "");
|
||||
assert.equal(changeTableStructure(header, "row", 0, "delete"), "");
|
||||
assert.equal(sortTableColumn(header, 0), header);
|
||||
const ragged = "| A | B |\n| --- | --- |\n| short |\n| a\\|b | two | extra |";
|
||||
assert.deepEqual(values(changeTableStructure(ragged, "column", 1, "previous"))[3], ["two", "a\\|b", "extra"]);
|
||||
assert.equal(changeTableStructure("invalid", "row", 0, "after"), "invalid");
|
||||
});
|
||||
Reference in New Issue
Block a user