An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve links when pasting tables into databases
Daniel496c9ac7b6a8
diff --git a/app/src/protyle/render/av/paste.test.ts b/app/src/protyle/render/av/paste.test.tsindex 5a3b37d073..549004dd34 100644--- a/app/src/protyle/render/av/paste.test.ts+++ b/app/src/protyle/render/av/paste.test.ts@@ -1,6 +1,8 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";import {+ getAVPasteCellValue,+ getAVPasteValueForType,getAVPasteMatrixWidth,getUniqueAVPasteColumnName,inferAVPasteColumnType,@@ -52,6 +54,74 @@ describe("inferAVPasteColumnType", () => {});describe("AV paste matrix helpers", () => {+ it("preserves link names and URLs as assets when links fill the table cell", () => {+ assert.deepEqual(getAVPasteCellValue("Issue 18354", [{+ content: "Issue 18354",+ href: "https://github.com/siyuan-note/siyuan/issues/18354",+ }]), {+ type: "mAsset",+ mAsset: [{+ type: "file",+ content: "https://github.com/siyuan-note/siyuan/issues/18354",+ name: "Issue 18354",+ }],+ });+ assert.deepEqual(getAVPasteCellValue("#18354、 #11928", [{+ content: "#18354",+ href: "https://github.com/siyuan-note/siyuan/issues/18354",+ }, {+ content: "#11928",+ href: "https://github.com/siyuan-note/siyuan/issues/11928",+ }], "、 "), {+ type: "mAsset",+ mAsset: [{+ type: "file",+ content: "https://github.com/siyuan-note/siyuan/issues/18354",+ name: "#18354",+ }, {+ type: "file",+ content: "https://github.com/siyuan-note/siyuan/issues/11928",+ name: "#11928",+ }],+ });+ assert.deepEqual(getAVPasteCellValue("#15049 click-editorcontent 返回错误 ID", [{+ content: "#15049 ",+ href: "https://github.com/siyuan-note/siyuan/issues/15049",+ }, {+ content: "\u200Bclick-editorcontent",+ href: "https://github.com/siyuan-note/siyuan/issues/15049",+ }, {+ content: " 返回错误 ID",+ href: "https://github.com/siyuan-note/siyuan/issues/15049",+ }], "\u200B"), {+ type: "mAsset",+ mAsset: [{+ type: "file",+ content: "https://github.com/siyuan-note/siyuan/issues/15049",+ name: "#15049 click-editorcontent 返回错误 ID",+ }],+ });+ assert.equal(getAVPasteCellValue("See Issue 18354", [{+ content: "Issue 18354",+ href: "https://github.com/siyuan-note/siyuan/issues/18354",+ }], "See "), "See Issue 18354");+ assert.equal(getAVPasteCellValue("Email", [{+ content: "Email",+ href: "mailto:[email protected]",+ }]), "Email");+ });++ it("infers asset columns from rich link values", () => {+ const value: IAVCellValue = {+ type: "mAsset",+ mAsset: [{type: "file", content: "https://example.com", name: "Example"}],+ };+ assert.equal(inferAVPasteColumnType(["", "https://example.org", value]), "mAsset");+ assert.deepEqual(getAVPasteValueForType(value, "mAsset"), value);+ assert.equal(getAVPasteValueForType(value, "url"), "https://example.com");+ assert.equal(getAVPasteValueForType(value, "text"), "Example");+ });+it("uses the widest header or data row", () => {assert.equal(getAVPasteMatrixWidth([["1"], ["2", "3"]], ["a", "b", "c"]), 3);assert.equal(getAVPasteMatrixWidth([["1", "2", "3"]], ["a"]), 3);diff --git a/app/src/protyle/render/av/paste.ts b/app/src/protyle/render/av/paste.tsindex 09d846e843..a304cce8b9 100644--- a/app/src/protyle/render/av/paste.ts+++ b/app/src/protyle/render/av/paste.ts@@ -44,6 +44,46 @@ const isURL = (value: string) => {}};+export const getAVPasteCellValue = (content: string, links: Array<{content: string, href: string}>,+ unlinkedContent = ""): string | IAVCellValue => {+ if (links.length === 0 || /[^\s\u200B-\u200D\uFEFF,,、;;|/]/u.test(unlinkedContent) ||+ links.some(link => !isURL(link.href))) {+ return content;+ }+ const assets: IAVCellAssetValue[] = [];+ links.forEach(link => {+ const href = link.href.trim();+ const name = link.content.replace(/[\u200B-\u200D\uFEFF]/g, "");+ const asset = assets.find(item => item.content === href);+ if (asset) {+ asset.name += name;+ } else {+ assets.push({+ type: "file",+ content: href,+ name,+ });+ }+ });+ assets.forEach(asset => {+ asset.name = asset.name.trim() || asset.content;+ });+ return {+ type: "mAsset",+ mAsset: assets,+ };+};++export const getAVPasteValueForType = (value: string | IAVCellValue, type: TAVCol) => {+ if (typeof value === "string" || value.type !== "mAsset" || type === "mAsset") {+ return value;+ }+ if (type === "url" && value.mAsset?.length === 1) {+ return value.mAsset[0].content;+ }+ return (value.mAsset || []).map(item => item.name || item.content).join(", ");+};+const isSingleSelect = (values: string[]) => {if (values.length < 4 || values.some(value => value.length > 64 || /[\r\n]/.test(value))) {return false;@@ -52,21 +92,29 @@ const isSingleSelect = (values: string[]) => {return optionCount > 1 && optionCount <= 20 && optionCount / values.length <= 0.5;};-export const inferAVPasteColumnType = (values: string[]): TAVCol => {- const nonEmptyValues = values.map(value => value.trim()).filter(Boolean);+export const inferAVPasteColumnType = (values: Array<string | IAVCellValue>): TAVCol => {+ const nonEmptyValues = values.filter(value => typeof value !== "string" || value.trim());if (nonEmptyValues.length === 0) {return "text";}- if (nonEmptyValues.every(isStrictNumber)) {+ const hasAssetValue = nonEmptyValues.some(value => typeof value !== "string" && value.type === "mAsset");+ if (hasAssetValue && nonEmptyValues.every(value => typeof value === "string" ? isURL(value) : value.type === "mAsset")) {+ return "mAsset";+ }+ if (nonEmptyValues.some(value => typeof value !== "string")) {+ return "text";+ }+ const stringValues = nonEmptyValues as string[];+ if (stringValues.every(isStrictNumber)) {return "number";}- if (nonEmptyValues.every(isFullDate)) {+ if (stringValues.every(isFullDate)) {return "date";}- if (nonEmptyValues.every(isURL)) {+ if (stringValues.every(isURL)) {return "url";}- if (isSingleSelect(nonEmptyValues)) {+ if (isSingleSelect(stringValues)) {return "select";}return "text";diff --git a/app/src/protyle/util/insertHTML.ts b/app/src/protyle/util/insertHTML.tsindex 7dc5383828..1a13f4ab50 100644--- a/app/src/protyle/util/insertHTML.ts+++ b/app/src/protyle/util/insertHTML.ts@@ -39,6 +39,8 @@ import {setFold} from "./blockFold";import {removeFoldHeading} from "./heading";import {AV_PASTE_READONLY_TYPES,+ getAVPasteCellValue,+ getAVPasteValueForType,getAVPasteMatrixWidth,getUniqueAVPasteColumnName,inferAVPasteColumnType,@@ -350,9 +352,18 @@ const pasteAVMatrix = async (options: {for (let sourceIndex = 0; sourceIndex < sourceWidth; sourceIndex++) {const headerName = options.header?.[sourceIndex]?.trim() || "";- const sourceValues = options.values.flatMap(row =>- sourceIndex < row.length && typeof row[sourceIndex] === "string" ? [row[sourceIndex] as string] : []);- const inferredType = options.header ? inferAVPasteColumnType(sourceValues) : "text";+ const sourceCellValues = options.values.flatMap(row =>+ sourceIndex < row.length ? [row[sourceIndex]] : []);+ const sourceValues = sourceCellValues.flatMap(value => {+ if (typeof value === "string") {+ return [value];+ }+ if (value.type === "mAsset") {+ return [(value.mAsset || []).map(item => item.name || item.content).join(", ")];+ }+ return [];+ });+ const inferredType = options.header ? inferAVPasteColumnType(sourceCellValues) : "text";const currentColumn = availableColumns[sourceIndex];if (currentColumn) {const readonly = AV_PASTE_READONLY_TYPES.has(currentColumn.type);@@ -546,7 +557,8 @@ const pasteAVMatrix = async (options: {continue;}const isNewRow = newRowIDSet.has(pasteRows[i].id);- const operations = await updateCellsValue(options.protyle, options.blockElement, options.values[i][j],+ const pasteValue = getAVPasteValueForType(options.values[i][j], targetColumn.column.type);+ const operations = await updateCellsValue(options.protyle, options.blockElement, pasteValue,[cellElement], options.columns, options.cellHTML?.[i]?.[j] || options.html,true, isNewRow || targetColumn.isNew || targetColumn.typeChanged, true, undefined, false);if (operations.doOperations.length > 0) {@@ -649,11 +661,19 @@ const processAV = (range: Range, html: string, protyle: IProtyle, blockElement:if (item.closest("table") !== tableElement) {return;}- const rowValues: string[] = [];+ const rowValues: TAVPasteValue[] = [];const rowHTML: string[] = [];Array.from(item.children).forEach(cell => {if (cell.tagName === "TD" || cell.tagName === "TH") {- rowValues.push(cell.textContent);+ const links = Array.from(cell.querySelectorAll<HTMLElement>(+ 'a[href], [data-type~="a"][data-href]',+ )).map(link => ({+ content: link.textContent,+ href: link.getAttribute("data-href") || link.getAttribute("href") || "",+ }));+ const unlinkedCell = cell.cloneNode(true) as HTMLElement;+ unlinkedCell.querySelectorAll('a[href], [data-type~="a"][data-href]').forEach(link => link.remove());+ rowValues.push(getAVPasteCellValue(cell.textContent, links, unlinkedCell.textContent));rowHTML.push(cell.outerHTML);}});