Skip to content

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

:art: Normalize local HTML asset URLs in IFrame blocks https://github.com/siyuan-note/siyuan/issues/18127

Daniel83a164f94566
diff --git a/app/src/asset/html.test.ts b/app/src/asset/html.test.ts
index 59d7fc53e3..dd4c0cf73f 100644
--- a/app/src/asset/html.test.ts
+++ b/app/src/asset/html.test.ts
@@ -1,6 +1,6 @@
import * as assert from "node:assert/strict";
import test from "node:test";
-import {getHTMLAssetIFrameSrc, isHTMLFilePath} from "./html";
+import {getHTMLAssetIFrameSrc, isHTMLFilePath, isLocalHTMLAssetPath} from "./html";
test("recognizes supported HTML file paths", () => {
assert.equal(isHTMLFilePath("component.html"), true);
@@ -9,6 +9,15 @@ test("recognizes supported HTML file paths", () => {
assert.equal(isHTMLFilePath("component.js"), false);
});
+test("recognizes only local HTML asset paths", () => {
+ assert.equal(isLocalHTMLAssetPath("assets/component.html"), true);
+ assert.equal(isLocalHTMLAssetPath("./assets/component.htm?box=20260810000000-abcdefg"), true);
+ assert.equal(isLocalHTMLAssetPath("/assets/component.HTML#preview"), true);
+ assert.equal(isLocalHTMLAssetPath("component.html"), false);
+ assert.equal(isLocalHTMLAssetPath("https://example.com/assets/component.html"), false);
+ assert.equal(isLocalHTMLAssetPath("assets/component.xhtml"), false);
+});
+
test("adds the HTML IFrame query without losing existing parameters", () => {
assert.equal(getHTMLAssetIFrameSrc("assets/component.html"), "assets/component.html?iframe=true");
assert.equal(getHTMLAssetIFrameSrc("assets/component.html?box=20260810000000-abcdefg"),
@@ -16,3 +25,10 @@ test("adds the HTML IFrame query without losing existing parameters", () => {
assert.equal(getHTMLAssetIFrameSrc("assets/component.html?iframe=false#preview"),
"assets/component.html?iframe=true#preview");
});
+
+test("keeps non-local IFrame sources unchanged", () => {
+ assert.equal(getHTMLAssetIFrameSrc("https://example.com/component.html"),
+ "https://example.com/component.html");
+ assert.equal(getHTMLAssetIFrameSrc("component.html"), "component.html");
+ assert.equal(getHTMLAssetIFrameSrc("assets/component.xhtml"), "assets/component.xhtml");
+});
diff --git a/app/src/asset/html.ts b/app/src/asset/html.ts
index ace9d4ac6c..da02488305 100644
--- a/app/src/asset/html.ts
+++ b/app/src/asset/html.ts
@@ -3,7 +3,17 @@ export const isHTMLFilePath = (value: string) => {
return path.endsWith(".html") || path.endsWith(".htm");
};
+export const isLocalHTMLAssetPath = (value: string) => {
+ const path = value.split(/[?#]/, 1)[0];
+ const normalizedPath = path.startsWith("./") ? path.substring(2) : path;
+ return (normalizedPath.startsWith("assets/") || normalizedPath.startsWith("/assets/")) &&
+ isHTMLFilePath(normalizedPath);
+};
+
export const getHTMLAssetIFrameSrc = (assetPath: string) => {
+ if (!isLocalHTMLAssetPath(assetPath)) {
+ return assetPath;
+ }
const hashIndex = assetPath.indexOf("#");
const hash = hashIndex > -1 ? assetPath.substring(hashIndex) : "";
const pathAndQuery = hashIndex > -1 ? assetPath.substring(0, hashIndex) : assetPath;
diff --git a/app/src/menus/protyle.ts b/app/src/menus/protyle.ts
index 79332afd76..3b25890170 100644
--- a/app/src/menus/protyle.ts
+++ b/app/src/menus/protyle.ts
@@ -66,6 +66,7 @@ import {base64ToURL} from "../util/image";
import {setPosition} from "../util/setPosition";
import {setFold} from "../protyle/util/blockFold";
import {isEncryptedBox} from "../util/pathName";
+import {getHTMLAssetIFrameSrc} from "../asset/html";
const renderAssetList = (element: Element, k: string, position: IPosition, exts: string[] = []) => {
fetchPost("/api/search/searchAsset", {
@@ -2117,7 +2118,9 @@ export const iframeMenu = (protyle: IProtyle, nodeElement: Element) => {
iframeElement.style.width = "640px";
}
} else {
- iframeElement.setAttribute("src", value);
+ const src = getHTMLAssetIFrameSrc(value);
+ iframeElement.setAttribute("src", src);
+ (event.target as HTMLTextAreaElement).value = src;
}
updateTransaction(protyle, nodeElement, html);