An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {readFileSync} from "node:fs";
import {join} from "node:path";
import {runInNewContext} from "node:vm";
import * as ts from "typescript";
import {getAVRichTextSafeURL} from "../render/av/richTextValue";
import {normalizePictureImages} from "./pictureImages";
import {htmlMathPaste, markdownMathPaste} from "./mathPaste";
import {escapeMarkdownPlainText} from "../../util/escape";
// 执行粘贴入口,替换剪贴板和上传接口,检查图片不会在受限片段中被丢弃。
const source = ts.transpileModule(readFileSync(join(process.cwd(), "src/protyle/util/paste.ts"), "utf8"), {
compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022},
}).outputText;
const createHarness = (disabled = false, richPaste = false, lute?: Lute) => {
const uploads: Array<{files: unknown; options: {document: {rootID: string}; insertPosition: unknown}}> = [];
const position = {range: {startContainer: {}}};
let available = true;
let localFiles: unknown[] = [];
let convertedHTML = "";
let convertedBy = "";
let checkedBlockDOM = "";
let clipboardText = "";
const restrictedFallback = new Error("restricted fallback");
const mocks: Record<string, unknown> = {
"./pictureImages": {normalizePictureImages},
"./tableCellRichContext": {getTableCellRichContext: (): undefined => undefined},
"../../constants": {Constants: {SIYUAN_ASSETS_IMAGE: [".png", ".jpg"]}},
"../runtimeCapabilities": {
getProtyleBlockDOMSanitizer: () => (html: string) => html,
isProtyleRichHTMLPasteEnabled: () => richPaste,
isProtyleUploadDisabled: () => disabled,
areProtylePluginExtensionsEnabled: () => false,
getProtyleUnsupportedPasteBlocks: () => (blockDOM: string) => {
checkedBlockDOM = blockDOM;
throw restrictedFallback;
},
},
"../upload/insertPosition": {
createUploadInsertPosition: () => position,
captureUploadDocument: () => ({rootID: "document", notebookID: "notebook"}),
isUploadInsertPositionAvailable: () => available,
},
"./selection": {getEditorRange: () => position.range},
"./hasClosest": {hasClosestBlock: (): undefined => undefined},
"./wpsPresentation": {extractWPSPresentationClipboard: (): undefined => undefined},
"./markdownClipboard": {shouldPasteMarkdownFromHTML: () => false},
"./mathPaste": {htmlMathPaste, markdownMathPaste},
"../../util/escape": {escapeMarkdownPlainText},
"./compatibility": {getLocalFiles: async () => localFiles, readText: async () => clipboardText, isInHarmony: () => false,
getTextSiyuanFromTextHTML: (html: string) => ({textSiyuan: "", textHtml: html})},
"../upload": {
uploadFiles: (_protyle: unknown, files: unknown, _element: unknown, _success: unknown,
_complete: unknown, options: typeof uploads[number]["options"]) => uploads.push({files, options}),
uploadLocalFiles: (files: unknown, _protyle: unknown, _upload: boolean,
options: typeof uploads[number]["options"]) => uploads.push({files, options}),
},
};
const module = {exports: {}};
runInNewContext(source, {module, exports: module.exports, require: (id: string) => mocks[id] || {},
window: {siyuan: {config: {editor: {mathDelimiterCompatibility: true}}}},
document: {createElement: () => ({content: {querySelector: (): null => null}})},
});
const api = module.exports as {-readonly [K in keyof typeof import("./paste")]: typeof import("./paste")[K]};
const protyle = {
wysiwyg: {element: {}},
lute: lute || {SetLatexMath: () => {}, Md2BlockDOM: (html: string) => {
convertedHTML = html;
convertedBy = "markdown";
return `<div data-type="NodeParagraph">${html}</div>`;
}, HTML2BlockDOM: (html: string) => {
convertedHTML = html;
convertedBy = "html";
return `<div data-type="NodeParagraph">${html}</div>`;
}},
} as unknown as IProtyle;
const paste = (files: unknown[] = [], siyuanHTML = "", textPlain = "", textHTML = "") => api.paste(protyle, {
target: {}, stopPropagation() {}, preventDefault() {},
clipboardData: {files, types: [], getData: (type: string) => type === "text/siyuan" ? siyuanHTML :
type === "text/plain" ? textPlain : type === "text/html" ? textHTML : ""},
} as unknown as ClipboardEvent & {target: HTMLElement});
const pasteEscaped = async (text: string) => {
clipboardText = text;
const originalPaste = api.paste;
let pending: ReturnType<typeof api.paste> | undefined;
api.paste = (...args) => {
pending = originalPaste(...args);
void pending.catch(() => {});
return pending;
};
try {
await api.pasteEscaped(protyle, {} as HTMLElement);
assert.ok(pending, "escaped paste must reach the shared paste entry");
await assert.rejects(pending, restrictedFallback);
} finally {
api.paste = originalPaste;
}
};
return {paste, pasteEscaped, uploads, position, restrictedFallback, api,
getConvertedHTML: () => convertedHTML, getConvertedBy: () => convertedBy,
getCheckedBlockDOM: () => checkedBlockDOM,
setLocalFiles: (files: unknown[]) => { localFiles = files; },
invalidate: () => { available = false; }};
};
describe("escaped text paste", () => {
if (typeof Lute === "undefined") {
require("../../../stage/protyle/js/lute/lute.min.js");
}
for (const richPaste of [false, true]) {
it(`preserves literal links and math delimiters with math compatibility enabled (richPaste=${richPaste})`, async () => {
const lute = Lute.New();
const harness = createHarness(false, richPaste, lute);
const text = "**literal** [link](https://example.com) \\(x\\) \\[y\\]";
await harness.pasteEscaped(text);
const dom = harness.getCheckedBlockDOM();
assert.doesNotMatch(dom, /data-type="(?:inline-math|NodeMathBlock|strong|a)"/);
assert.equal(lute.BlockDOM2Content(dom).trim(), text);
assert.match(markdownMathPaste(lute, "\\(x\\)", true), /data-type="inline-math"/);
});
}
});
describe("restricted cell image paste", () => {
it("uploads screenshot clipboard files with the captured document and cursor", async () => {
const harness = createHarness();
const files = [{name: "image.png", type: "image/png"}];
await harness.paste(files);
assert.equal(harness.uploads.length, 1);
assert.equal(harness.uploads[0].files, files);
assert.equal(harness.uploads[0].options.document.rootID, "document");
assert.equal(harness.uploads[0].options.insertPosition, harness.position);
});
it("uploads image files copied from the desktop clipboard", async () => {
const harness = createHarness();
const files = [{path: "C:/images/photo.JPG", size: 10}];
harness.setLocalFiles(files);
await harness.paste();
assert.equal(harness.uploads.length, 1);
assert.equal(harness.uploads[0].files, files);
});
it("does not upload after the paste target disappears", async () => {
const harness = createHarness();
harness.setLocalFiles([{path: "C:/image.png"}]);
harness.invalidate();
await harness.paste();
assert.equal(harness.uploads.length, 0);
});
for (const [name, disabled, files, siyuanHTML] of [
["disabled uploads", true, [{name: "image.png"}], ""],
["unsupported files", false, [{name: "movie.mp4"}], ""],
["internal block content", false, [{name: "image.png"}], "internal"],
] as const) {
it(`preserves restricted handling for ${name}`, async () => {
const harness = createHarness(disabled);
await assert.rejects(harness.paste([...files], siyuanHTML), harness.restrictedFallback);
assert.equal(harness.uploads.length, 0);
});
}
it("converts pasted HTML source before validating table cell content", async () => {
const harness = createHarness(false, true);
const html = '<span data-type="custom_example" style="--custom-example: #abc;">text</span>';
await assert.rejects(harness.paste([], "", html), harness.restrictedFallback);
assert.equal(harness.getConvertedHTML(), html);
assert.equal(harness.getConvertedBy(), "markdown");
assert.equal(harness.getCheckedBlockDOM(), `<div data-type="NodeParagraph">${html}</div>`);
});
it("retains custom spans in HTML clipboard content without changing ordinary HTML conversion", async () => {
const custom = '<span data-type="custom_example" style="--custom-example: #abc;">text</span>';
const customHarness = createHarness(false, true);
await assert.rejects(customHarness.paste([], "", "text", custom), customHarness.restrictedFallback);
assert.equal(customHarness.getConvertedBy(), "markdown");
assert.equal(customHarness.getCheckedBlockDOM(), `<div data-type="NodeParagraph">${custom}</div>`);
const ordinaryHarness = createHarness(false, true);
await assert.rejects(ordinaryHarness.paste([], "", "bold", "<strong>bold</strong>"),
ordinaryHarness.restrictedFallback);
assert.equal(ordinaryHarness.getConvertedBy(), "html");
});
});
describe("strip pasted IAL data attributes", () => {
const harness = createHarness();
it("removes data-* and on* keys from pasted block IAL", () => {
assert.equal(harness.api.stripPastedIALDataAttributes(
'x\n{: id="20240101000000-abc123" data-subtype="x" autofocus" onmouseover="alert(1)" custom-foo="bar"}'),
'x\n{: id="20240101000000-abc123" custom-foo="bar"}');
});
it("keeps data-assets for legacy asset references", () => {
assert.equal(harness.api.stripPastedIALDataAttributes(
'x\n{: data-assets="assets/a.png"}'),
'x\n{: data-assets="assets/a.png"}');
});
it("leaves ordinary markdown and non-IAL lines untouched", () => {
const markdown = "paragraph\n\n{: not an ial line\n\n# heading\n{: id=\"20240101000000-abc123\"}";
assert.equal(harness.api.stripPastedIALDataAttributes(markdown), markdown);
});
});
describe("shouldPasteClipboardImage", () => {
const harness = createHarness();
const excelTableWithImage = "<table border=0 cellpadding=0 cellspacing=0 width=120 style='border-collapse:collapse'>" +
"<tr height=18><td>Cell A</td><td><img src=\"file:///C:/Temp/msohtmlclip1/01/clip_image002.png\" width=48 height=18></td></tr></table>";
it("keeps table data for database grid pastes when a bitmap preview coexists", () => {
assert.equal(harness.api.shouldPasteClipboardImage(excelTableWithImage, true), false);
});
it("keeps table data for database grid pastes without embedded images", () => {
assert.equal(harness.api.shouldPasteClipboardImage("<table><tr><td>a\tb</td></tr></table>", true), false);
});
it("still prefers the image outside database targets", () => {
assert.equal(harness.api.shouldPasteClipboardImage(excelTableWithImage, false), true);
});
it("uploads single clipboard images even for database targets", () => {
assert.equal(harness.api.shouldPasteClipboardImage("<img src=\"data:image/png;base64,xxx\">", true), true);
assert.equal(harness.api.shouldPasteClipboardImage("<img src=\"data:image/png;base64,xxx\">", false), true);
});
it("does not treat non-image tables as clipboard images", () => {
assert.equal(harness.api.shouldPasteClipboardImage("<table><tr><td>plain</td></tr></table>", false), false);
assert.equal(harness.api.shouldPasteClipboardImage("<div><p>text</p></div>", false), false);
assert.equal(harness.api.shouldPasteClipboardImage("", true), false);
});
});
describe("restricted cell selected text paste", () => {
if (typeof Lute === "undefined") {
require("../../../stage/protyle/js/lute/lute.min.js");
}
const lute = Lute.New();
const runPaste = async (text: string, options: {selected?: string, code?: boolean, inlineCode?: boolean,
unsupported?: boolean, tableCell?: boolean} = {}) => {
const selected = options.selected ?? "Selected & text";
const marks: Array<{type: string, value: ITextOption}> = [];
const inserted: string[] = [];
const messages: string[] = [];
const range = {startContainer: {parentElement: {tagName: "DIV"}}, toString: () => selected,
selectNodeContents: () => {}};
const block = {classList: {contains: () => false},
getAttribute: () => options.code ? "NodeCodeBlock" : "NodeParagraph"};
const mocks: Record<string, unknown> = {
"../../constants": {Constants: {ZWSP: "\u200b"}},
"./mathPaste": {htmlMathPaste, markdownMathPaste},
"../runtimeCapabilities": {
getProtyleBlockDOMSanitizer: () => (html: string) => html,
isProtyleRichHTMLPasteEnabled: () => false,
getProtyleUnsupportedPasteBlocks: () => () => options.unsupported ? ["Table"] : [],
getProtyleRestrictedPlainTextHTML: (text: string) => "plain:" + text,
isProtyleUploadDisabled: () => true,
areProtylePluginExtensionsEnabled: () => false,
},
"../upload/insertPosition": {
createUploadInsertPosition: () => ({range}),
captureUploadDocument: () => ({}),
getAvailableUploadInsertRange: () => range,
isUploadInsertPositionAvailable: () => true,
},
"./selection": {getEditorRange: () => range},
"./pictureImages": {normalizePictureImages},
"./tableCellRichContext": {getTableCellRichContext: () => options.tableCell ? {} : undefined},
"./hasClosest": {hasClosestBlock: () => block},
"./compatibility": {isInHarmony: () => false},
"./officeMath": {extractOfficeMathHTML: () => ""},
"../upload/htmlLocalAssets": {resolveHTMLAssetURLs: () => {}, getHTMLAssetSourceURL: () => ""},
"./pasteSource": {extractCrossBlockPasteContext: (html: string) => ({html})},
"../ui/hideElements": {hideElements: () => {}},
"./inlineElementMarker": {stripSemanticMarkersFromRangeText: () => selected},
"../toolbar/inlineRangeBoundary": {getSelectedInlineCodeElement: (): undefined => undefined},
"../../editor/pdfAssetLink": {getPdfAnnotationReference: (): undefined => undefined},
"../../util/functions": {isDynamicRef: (value: string) => /^\(\(\d{14}-\w{7} '.*'\)\)$/.test(value)},
"../toolbar/util": {resolveLinkDest: (value: string) => value.startsWith("assets/") ? value : lute.GetLinkDest(value)},
"../render/av/richTextValue": {getAVRichTextSafeURL},
"./normalizeText": {removeZWJ: (value: string) => value},
"./insertHTML": {insertHTML: (value: string) => inserted.push(value)},
"../../dialog/message": {showMessage: (value: string) => messages.push(value)},
"../../util/escape": {escapeHtml: (value: string) => value},
};
const module = {exports: {}};
runInNewContext(source, {module, exports: module.exports, require: (id: string) => mocks[id] || {},
DOMParser: class {parseFromString() {
return {querySelector: (): null => null, querySelectorAll: (): Element[] => [], body: {innerHTML: ""}};
}},
Lute: {Sanitize: (value: string) => value},
window: {siyuan: {config: {editor: {mathDelimiterCompatibility: true}},
languages: {cellPasteUnsupported: "Unsupported: ${x}"}}},
});
const protyle = {
lite: true,
lute: {SetLatexMath: () => {}, Md2BlockDOM: (value: string) => value},
hint: {enableExtend: false},
wysiwyg: {element: {querySelectorAll: (): Element[] => []}},
toolbar: {range, getCurrentType: () => options.inlineCode ? ["code"] : [],
setInlineMark: (_protyle: unknown, type: string, _action: string, value: ITextOption) => {
marks.push({type, value});
return [{}];
}},
} as unknown as IProtyle;
await (module.exports as typeof import("./paste")).paste(protyle, {
textPlain: text, textHTML: "", siyuanHTML: "", target: block as unknown as HTMLElement,
});
return {marks, inserted, messages};
};
it("applies a static reference with the selected anchor text", async () => {
const result = await runPaste("((20260921000000-abcdefg 'Document title'))");
assert.equal(result.marks.length, 1);
assert.equal(result.marks[0].type, "block-ref");
assert.equal(result.marks[0].value.color, "20260921000000-abcdefg\u200bs\u200bSelected & text");
assert.deepEqual(result.inserted, []);
});
for (const url of ["https://example.com", "assets/document.pdf"]) {
it(`attaches ${url} without replacing the selected text`, async () => {
const result = await runPaste(url);
assert.equal(result.marks[0].type, "a");
assert.equal(result.marks[0].value.color, url);
assert.deepEqual(result.inserted, []);
});
}
for (const [text, options] of [
["ordinary text", {}],
["https://example.com", {selected: ""}],
["https://example.com", {code: true}],
["https://example.com", {inlineCode: true}],
["file:///private/document", {}],
["D:\\基线测试\\文档.docx", {tableCell: true}],
] as const) {
it(`retains plain text fallback for ${text} ${JSON.stringify(options)}`, async () => {
const result = await runPaste(text, options);
assert.deepEqual(result.marks, []);
assert.deepEqual(result.inserted, ["plain:" + text]);
});
}
for (const url of ["file:///D:\\基线测试\\文档.docx", "file:///tmp/document"]) {
it(`attaches ${url} to selected table cell text`, async () => {
const result = await runPaste(url, {tableCell: true});
assert.equal(result.marks[0].type, "a");
assert.equal(result.marks[0].value.color, url);
assert.deepEqual(result.inserted, []);
});
}
it("rejects unsupported content before changing the selection", async () => {
const result = await runPaste("https://example.com", {unsupported: true});
assert.deepEqual(result.marks, []);
assert.deepEqual(result.inserted, []);
assert.equal(result.messages.length, 1);
});
});