Skip to content

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

:bug: Preserve internal previews for asset URL fragments https://github.com/siyuan-note/siyuan/issues/20838

Weidong Qif5ee9ff4fbe1
diff --git a/app/src/protyle/render/av/asset.test.ts b/app/src/protyle/render/av/asset.test.ts
index a4f4e4a21d..310fe5ea72 100644
--- a/app/src/protyle/render/av/asset.test.ts
+++ b/app/src/protyle/render/av/asset.test.ts
@@ -1,7 +1,7 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {readFileSync} from "node:fs";
-import {join} from "node:path";
+import * as path from "node:path";
import {runInNewContext} from "node:vm";
import * as ts from "typescript";
import * as selectionState from "./selectionState";
@@ -11,6 +11,14 @@ import * as uploadResult from "../../upload/uploadResult";
import * as viewType from "./viewType";
import * as insertPosition from "../../upload/insertPosition";
+const pathNames = {} as typeof import("../../../util/pathName");
+runInNewContext(ts.transpileModule(readFileSync(path.join(__dirname, "../../../util/pathName.ts"), "utf8"), {
+ compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022},
+}).outputText, {
+ exports: pathNames,
+ require: (name: string) => name === "path" ? path : {},
+});
+
// 使用完整资源消费者,控制上传完成与虚拟行移除的顺序,记录实际生成的事务。
const createHarness = (layout = "table") => {
const column = {id: "assets", type: "mAsset"} as IAVColumn;
@@ -46,7 +54,7 @@ const createHarness = (layout = "table") => {
"./col": {getColId: () => "assets"},
"./row": {getFieldIdByCellElement: () => "row"},
"./batchValue": {getAVBatchEditMode: () => "replace"},
- "../../../util/pathName": {getAssetExtension: () => ".png"},
+ "../../../util/pathName": pathNames,
"../../../constants": {Constants: {SIYUAN_ASSETS_IMAGE: [".png"]}},
"../../../dialog/message": {showMessage: () => {}},
"../../util/hasClosest": {hasClosestBlock: () => block},
@@ -65,7 +73,7 @@ const createHarness = (layout = "table") => {
},
};
const module = {exports: {}};
- runInNewContext(ts.transpileModule(readFileSync(join(__dirname, "asset.ts"), "utf8"), {
+ runInNewContext(ts.transpileModule(readFileSync(path.join(__dirname, "asset.ts"), "utf8"), {
compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022},
}).outputText, {
module, exports: module.exports, require: (name: string) => mocks[name] || {},
@@ -116,6 +124,15 @@ describe("database resource upload consumers", () => {
assert.deepEqual(harness.cell.value.mAsset.map(value => value.content), ["original.png", "new.png"]);
assert.deepEqual(harness.cell.value.mAsset.map(value => value.name), ["original", "new.png"]);
});
+ it(`${kind}: recognizes image filenames containing a literal hash`, () => {
+ for (const name of ["image#copy.png", "image#copy.PNG"]) {
+ const harness = createHarness();
+ harness.start(kind);
+ harness.complete(name);
+ assert.equal(harness.cell.value.mAsset[1].type, "image", name);
+ assert.equal(harness.cell.value.mAsset[1].name, name);
+ }
+ });
it(`${kind}: appends both uploads while the row is not rendered`, () => {
const harness = createHarness();
harness.start(kind);
diff --git a/app/src/protyle/render/av/asset.ts b/app/src/protyle/render/av/asset.ts
index 632e9b5173..1ff1d59a23 100644
--- a/app/src/protyle/render/av/asset.ts
+++ b/app/src/protyle/render/av/asset.ts
@@ -5,7 +5,7 @@ import {updateAttrViewCellAnimation} from "./action";
import {isMobile} from "../../../util/functions";
import {Constants} from "../../../constants";
import {uploadFiles, uploadLocalFiles} from "../../upload";
-import {getAssetExtension} from "../../../util/pathName";
+import {pathPosix} from "../../../util/pathName";
import {openMenu} from "../../../menus/commonMenuItem";
import {MenuItem} from "../../../menus/Menu";
import {copyPNGByLink, exportAsset, writeAssetToClipboard} from "../../../menus/util";
@@ -534,7 +534,7 @@ const getUploadedAssetValues = (result: Omit<IAssetUploadResult, "requestId" | "
successes.sort((a, b) => a.name.localeCompare(b.name, undefined, {numeric: true}) || a.index - b.index);
}
successes.forEach(success => {
- const type = getAssetExtension(success.name).toLowerCase();
+ const type = pathPosix().extname(success.name).toLowerCase();
const filename = formatName(success.name);
values.push({
type: Constants.SIYUAN_ASSETS_IMAGE.includes(type) ? "image" : "file",
diff --git a/app/src/util/pathName.test.ts b/app/src/util/pathName.test.ts
index 850a5de694..73644de872 100644
--- a/app/src/util/pathName.test.ts
+++ b/app/src/util/pathName.test.ts
@@ -35,3 +35,18 @@ test("physical file display names retain their separate basename and extension b
assert.equal(names.getDisplayName("folder/note.sy", false), "folder/note.sy");
assert.equal(names.getDisplayName("assets/image.png"), "image.png");
});
+
+test("asset names and extensions ignore query strings and fragments", () => {
+ for (const [assetPath, extension, name] of [
+ ["assets/image.png", ".png", "image"],
+ ["assets/image.png#preview", ".png", "image"],
+ ["assets/image.png?box=notebook#preview", ".png", "image"],
+ ["assets/image.png#preview?download=true", ".png", "image"],
+ ["assets/folder.with.dots/video.mp4#t=10", ".mp4", "video"],
+ ["assets/image-20261024000000-abcdefg.PNG#preview", ".PNG", "image"],
+ ["assets/image%23copy%3F1.png#preview", ".png", "image%23copy%3F1"],
+ ]) {
+ assert.equal(names.getAssetExtension(assetPath), extension, assetPath);
+ assert.equal(names.getAssetName(assetPath), name, assetPath);
+ }
+});
diff --git a/app/src/util/pathName.ts b/app/src/util/pathName.ts
index 1da25dc9a5..9f77edb9ad 100644
--- a/app/src/util/pathName.ts
+++ b/app/src/util/pathName.ts
@@ -169,7 +169,7 @@ export const getDocDisplayName = (name: string, titleEmpty?: boolean, escape?: b
};
export const getAssetPathWithoutQuery = (assetPath: string) => {
- return assetPath.split("?", 1)[0];
+ return assetPath.split(/[?#]/, 1)[0];
};
export const getAssetExtension = (assetPath: string) => {