Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

5.8 KB
Raw Blame
import * as assert from "node:assert/strict";
import {describe, it} from "node:test";
import {decodeTableCellRich, encodeTableCellRich} from "./tableCellRichValue";
import {configureAVRichTextLute} from "../render/av/richTextValue";

describe("table cell rich text wire format", () => {
    for (const [name, content, source] of [
        ["single break", "first<br>second", "first\nsecond\n"],
        ["formatted breaks", "a. <strong>first</strong><br>b. <strong>second</strong><br>c. <strong>third</strong>",
            "a. **first**\nb. **second**\nc. **third**\n"],
    ]) {
        it(`pastes paragraph-wrapped cells with ${name} without changing the table grid or rich source`, () => {
            if (typeof Lute === "undefined") {
                require("../../../stage/protyle/js/lute/lute.min.js");
            }
            const lute = Lute.New();
            lute.SetTextMark(true);
            lute.SetHTMLTag2TextMark(true);
            lute.SetKramdownIAL(true);
            lute.SetProtyleWYSIWYG(true);
            let dom = lute.HTML2BlockDOM("<table><thead><tr><th><p>title</p></th><th><p>details</p></th></tr></thead>" +
                `<tbody><tr><td><p>value</p></td><td><p>${content}</p></td></tr></tbody></table>`);
            for (let round = 0; round < 3; round++) {
                assert.equal((dom.match(/<tr>/g) || []).length, 2);
                assert.equal((dom.match(/<t[dh][\s>]/g) || []).length, 4);
                assert.doesNotMatch(dom, /\{: table-cell-rich/);
                const cells = Array.from(dom.matchAll(/data-sy-table-cell-rich="([^"]+)"/g));
                assert.equal(cells.length, 4);
                assert.equal(decodeTableCellRich(cells[3][1]).content, source);
                dom = lute.SpinBlockDOM(dom);
            }
            const markdown = lute.BlockDOM2StdMd(dom);
            assert.doesNotMatch(markdown, /table-cell-rich/);
            assert.equal((lute.Md2BlockDOM(markdown).match(/<tr>/g) || []).length, 2);
        });
    }

    it("copies styled table cells as standard Markdown without internal attributes", () => {
        if (typeof Lute === "undefined") {
            require("../../../stage/protyle/js/lute/lute.min.js");
        }
        const lute = Lute.New();
        lute.SetProtyleWYSIWYG(true);
        lute.SetKramdownIAL(true);
        const html = '<div data-type="NodeTable"><div contenteditable="true"><table><thead><tr>' +
            '<th style="text-align: left;">Header</th></tr></thead><tbody><tr>' +
            '<td style="text-align: center;"><strong>value</strong></td></tr></tbody></table></div></div>';
        const markdown = lute.BlockDOM2StdMd(html);
        assert.match(markdown, /\*\*value\*\*/);
        assert.doesNotMatch(markdown, /\{:\s|text-align|style=/);
        assert.match(lute.BlockDOM2Md(html), /text-align: center/);
    });

    it("preserves Unicode, literal Markdown, pipes, and line breaks without changing the source", () => {
        const source = "- **文本** 🧪\n\n```go\na | b\nc\n```\n\n\\*literal\\*";
        const encoded = encodeTableCellRich(source);
        assert.match(encoded, /^[A-Za-z0-9_-]+$/);
        assert.deepEqual(JSON.parse(Buffer.from(encoded, "base64url").toString("utf8")), {
            spec: 1, format: "kramdown", content: source,
        });
        assert.equal(decodeTableCellRich(encoded).content, source);
        assert.equal(decodeTableCellRich(encodeTableCellRich("")).content, "");
    });

    it("rejects missing, null, future, and corrupt payloads instead of treating them as plain text", () => {
        for (const value of [null, {}, [],
            {spec: 2, format: "kramdown", content: "source"},
            {spec: 1, format: "html", content: "source"},
            {spec: 1, format: "kramdown", content: null},
            {spec: 1, format: "kramdown", content: "before\u0000after"},
            {spec: 1, format: "kramdown", content: "source", unknown: true},
        ]) {
            assert.throws(() => decodeTableCellRich(Buffer.from(JSON.stringify(value)).toString("base64url")));
        }
        for (const encoded of ["", "invalid!", "_w", "e30="]) {
            assert.throws(() => decodeTableCellRich(encoded));
        }
    });

    it("round-trips rich cells through the bundled parser and exports structural HTML in Markdown", () => {
        if (typeof Lute === "undefined") {
            require("../../../stage/protyle/js/lute/lute.min.js");
        }
        const lute = Lute.New();
        lute.SetTextMark(true);
        lute.SetHTMLTag2TextMark(true);
        lute.SetKramdownIAL(true);
        lute.SetSpin(true);
        lute.SetProtyleWYSIWYG(true);
        lute.SetSanitize(true);
        configureAVRichTextLute(lute);
        const source = "- **first**\n- second\n\n```go\na | b\nc\n```\n\n![image](assets/image.png)";
        const encoded = encodeTableCellRich(source);
        let html = "<div data-type=\"NodeTable\" data-node-id=\"20260908000001-table01\"><div contenteditable=\"true\">" +
            `<table><thead><tr><th>Header</th></tr></thead><tbody><tr><td data-sy-table-cell-rich="${encoded}"></td>` +
            "</tr></tbody></table></div></div>";
        for (let index = 0; index < 3; index++) {
            html = lute.SpinBlockDOM(html);
            assert.equal(decodeTableCellRich(html.match(/data-sy-table-cell-rich="([^"]+)"/)[1]).content, source);
            assert.match(html, /<ul>/);
            assert.match(html, /assets\/image.png/);
            assert.equal((html.match(/data-node-id=/g) || []).length, 1);
        }
        const markdown = lute.BlockDOM2StdMd(html);
        assert.match(markdown, /first/);
        assert.match(markdown, /<ul>/);
        assert.match(markdown, /<pre><code/);
        assert.match(markdown, /a.*b/);
        assert.doesNotMatch(markdown, /table-cell-rich|```go/);
        assert.match(lute.BlockDOM2InlineBlockDOM(html), /a.*b/);
    });
});