Make Table Formulas a built-in feature

The formula engine, the rendered results, and the formula help now
come from src/lib/formulas/. The editor and the renderer call this
module directly. The tableFormulas contribution type is removed.

- Add the tableFormulas general setting (default on) and a switch in
  Settings > General. When it is off, formulas show as source text.
- Do not load an installed runestone.table-formulas package. Settings
  > Plug-ins marks it as included with RuneStone and tells the user
  that it can be removed. The user's files stay in place.
- Translate the formula help and the error descriptions into French,
  Japanese, and Simplified Chinese.
- Port the plugin unit tests. Test the setting and use a test-only
  contribution for the generic plugin lifecycle tests.
- Smoke tests load only Terminal. The formula and lifecycle tests use
  the built-in feature and its setting.
- Remove scripts/buildPlugins.mjs, the build:plugins script, and the
  plugin-packages requirement of the smoke runner.
- Remove RuneStone_Table_Formulas from the menu audit scope. It had no
  menu surfaces.
- Update the README, the user guides, and the developer references.
This commit is contained in:
Knotty Wyvern
2026-10-28 01:05:43 -07:00
parent 72671e5d01
commit 5e1533c371
43 changed files with 580 additions and 198 deletions
+46 -18
View File
@@ -1,24 +1,14 @@
import assert from "node:assert/strict";
import test, { before, after } from "node:test";
import { ExternalSession } from "../../../src/lib/plugins/externalSession.ts";
import { existsSync } from "node:fs";
// The Table Formulas package comes from a sibling repository. Run `pnpm build:plugins` to build it.
const tableFormulasEntry = new URL("../../../plugin-packages/table-formulas/main.js", import.meta.url);
const tableFormulas = existsSync(tableFormulasEntry) ? await import(tableFormulasEntry.href) : undefined;
const activate = tableFormulas?.activate;
const needsTableFormulas = { skip: tableFormulas ? false : "the Table Formulas package is not built; run pnpm build:plugins" };
const session = new ExternalSession("runestone.table-formulas", {
workspace: { list: async () => [], read: async () => "", write: async () => {} },
settings: { read: async () => null, write: async () => {} },
assets: { read: async () => new Uint8Array() },
}, () => {}, () => {});
before(() => activate && session.start({ activate }));
after(() => activate && session.stop());
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";
test("Markdown rendering isolates tables and preserves literal, formatted, and non-table text", needsTableFormulas, () => {
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>/);
@@ -34,9 +24,47 @@ test("Markdown rendering isolates tables and preserves literal, formatted, and n
assert.match(hostile, /#ERROR!/);
});
test("editing keeps formulas readable and code or explicit escapes literal", needsTableFormulas, () => {
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, /&quot;&lt;img&gt;&quot;/);
assert.match(html, /md-table-formula is-error/);
assert.match(html, /Invalid formula syntax or function arguments/);
});