RuneStone 1.0.0
This commit is contained in:
@@ -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);
|
||||
});
|
||||
Reference in New Issue
Block a user