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: Render legacy local HTML assets in IFrame blocks https://github.com/siyuan-note/siyuan/issues/18772
Daniel876fb237d181
diff --git a/app/src/asset/html.test.ts b/app/src/asset/html.test.tsindex dd4c0cf73f..380d013a5b 100644--- a/app/src/asset/html.test.ts+++ b/app/src/asset/html.test.ts@@ -1,6 +1,11 @@import * as assert from "node:assert/strict";import test from "node:test";-import {getHTMLAssetIFrameSrc, isHTMLFilePath, isLocalHTMLAssetPath} from "./html";+import {+ getHTMLAssetIFrameSrc,+ isHTMLFilePath,+ isLocalHTMLAssetPath,+ normalizeHTMLAssetIFrameSources+} from "./html";test("recognizes supported HTML file paths", () => {assert.equal(isHTMLFilePath("component.html"), true);@@ -32,3 +37,40 @@ test("keeps non-local IFrame sources unchanged", () => {assert.equal(getHTMLAssetIFrameSrc("component.html"), "component.html");assert.equal(getHTMLAssetIFrameSrc("assets/component.xhtml"), "assets/component.xhtml");});++test("normalizes legacy local HTML IFrame sources", () => {+ const sources = [+ "assets/legacy.html",+ "assets/current.html?iframe=true",+ "https://example.com/component.html",+ "assets/component.svg",+ null+ ];+ const iframes = sources.map((source) => {+ let value = source;+ return {+ get source() {+ return value;+ },+ getAttribute: () => value,+ setAttribute: (_name: string, newValue: string) => {+ value = newValue;+ }+ };+ });+ const root = {+ querySelectorAll: (selector: string) => {+ assert.equal(selector, '[data-type="NodeIFrame"] iframe');+ return iframes;+ }+ } as unknown as ParentNode;++ assert.equal(normalizeHTMLAssetIFrameSources(root), true);+ assert.deepEqual(iframes.map(item => item.source), [+ "assets/legacy.html?iframe=true",+ "assets/current.html?iframe=true",+ "https://example.com/component.html",+ "assets/component.svg",+ null+ ]);+});diff --git a/app/src/asset/html.ts b/app/src/asset/html.tsindex da02488305..4f2d5453b7 100644--- a/app/src/asset/html.ts+++ b/app/src/asset/html.ts@@ -23,3 +23,31 @@ export const getHTMLAssetIFrameSrc = (assetPath: string) => {params.set("iframe", "true");return `${path}?${params.toString()}${hash}`;};++export const normalizeHTMLAssetIFrameSources = (root: ParentNode) => {+ let changed = false;+ root.querySelectorAll<HTMLIFrameElement>('[data-type="NodeIFrame"] iframe').forEach(item => {+ const src = item.getAttribute("src");+ if (!src) {+ return;+ }+ const normalizedSrc = getHTMLAssetIFrameSrc(src);+ if (normalizedSrc !== src) {+ item.setAttribute("src", normalizedSrc);+ changed = true;+ }+ });+ return changed;+};++export const normalizeHTMLAssetIFrameBlockDOM = (html: string) => {+ if (!html.includes("NodeIFrame") || !html.includes("<iframe")) {+ return html;+ }+ const template = document.createElement("template");+ template.innerHTML = html;+ if (!normalizeHTMLAssetIFrameSources(template.content)) {+ return html;+ }+ return template.innerHTML;+};diff --git a/app/src/protyle/render/blockRender.ts b/app/src/protyle/render/blockRender.tsindex 325a9d3e86..968b90b748 100644--- a/app/src/protyle/render/blockRender.ts+++ b/app/src/protyle/render/blockRender.ts@@ -7,6 +7,7 @@ import {avRender} from "./av/render";import {genRenderFrame} from "./util";import {isEncryptedBox} from "../../util/pathName";import {disabledWYSIWYG} from "../util/disabledWYSIWYG";+import {normalizeHTMLAssetIFrameBlockDOM} from "../../asset/html";/*** 渲染嵌入块@@ -128,7 +129,7 @@ ${popover}${breadcrumbHTML}${blocksItem.block.content}</div>`;});if (blocks.length > 0) {- item.firstElementChild.insertAdjacentHTML("afterend", html);+ item.firstElementChild.insertAdjacentHTML("afterend", normalizeHTMLAssetIFrameBlockDOM(html));improveBreadcrumbAppearance(item.querySelector(".protyle-wysiwyg__embed"));} else {item.firstElementChild.insertAdjacentHTML("afterend", `<div class="protyle-wysiwyg__embed ft__smaller ft__secondary b3-form__space--small" contenteditable="false">${errorTip || window.siyuan.languages.refExpired}</div>`);diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.tsindex 9ef74e61ae..dc296a3ac6 100644--- a/app/src/protyle/util/onGet.ts+++ b/app/src/protyle/util/onGet.ts@@ -30,6 +30,7 @@ import {updateDocumentBottomEof} from "./documentRange";import {disabledWYSIWYG} from "./disabledWYSIWYG";import {getEmbeddedDocInfoResponse} from "./docInfo";import {updateWidgetCacheVersion} from "./widgetCache";+import {normalizeHTMLAssetIFrameSources} from "../../asset/html";/// #if MOBILEimport {updateMobileTitleReadonly} from "./setEditMode";/// #endif@@ -225,6 +226,7 @@ const setHTML = (options: {item.setAttribute("data-inline-memo-content", window.DOMPurify.sanitize(content));}});+ normalizeHTMLAssetIFrameSources(doc);updateWidgetCacheVersion(doc, Constants.SIYUAN_VERSION);options.content = doc.body.innerHTML;const REMOVED_OVER_HEIGHT = protyle.contentElement.clientHeight * 8;diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.tsindex e14b853f5d..8b74e2b6d0 100644--- a/app/src/protyle/wysiwyg/transaction.ts+++ b/app/src/protyle/wysiwyg/transaction.ts@@ -48,6 +48,7 @@ import {import {MERMAID_LAYOUT_ATTR} from "../render/mermaidLayout";import {getPartialUpdateCleanupElements, shouldDeferCodeBlockCaretRestore} from "./transactionUpdate";import {getMoveAffectedEmbedElements, shouldSyncMoveCopies} from "./transactionEmbed";+import {normalizeHTMLAssetIFrameBlockDOM} from "../../asset/html";const removeTopElement = (updateElement: Element, protyle: IProtyle) => {// 移动到其他文档中,该块需移除@@ -113,6 +114,7 @@ const syncFoldAndStyleAttrs = (element: Element, operation: IOperation) => {};const getVisibleFoldHeadingHTML = (html: string) => {+ html = normalizeHTMLAssetIFrameBlockDOM(html);if (!html.includes('data-type="NodeHeading"') || !html.includes('fold="1"')) {return html;}diff --git a/kernel/model/assets.go b/kernel/model/assets.goindex a5219b48b4..fc268f7787 100644--- a/kernel/model/assets.go+++ b/kernel/model/assets.go@@ -1034,19 +1034,30 @@ func HTMLAssetIFrameSrc(assetPath string) string {return parsed.String()}-// IsHTMLAssetIFrameSrc 判断资源地址是否为 HTML 文件 IFrame 渲染地址。-func IsHTMLAssetIFrameSrc(assetPath string) bool {+// IsLocalHTMLAssetPath 判断资源地址是否为本地 HTML 文件。+func IsLocalHTMLAssetPath(assetPath string) bool {parsed, err := url.Parse(assetPath)- if err != nil || !strings.EqualFold(parsed.Query().Get("iframe"), "true") {+ if err != nil || parsed.IsAbs() || parsed.Host != "" {return false}- if parsed.IsAbs() || parsed.Host != "" || !strings.HasPrefix(strings.TrimPrefix(parsed.Path, "/"), "assets/") {+ assetPath = strings.TrimPrefix(parsed.Path, "/")+ assetPath = strings.TrimPrefix(assetPath, "./")+ if !strings.HasPrefix(assetPath, "assets/") {return false}- ext := strings.ToLower(path.Ext(parsed.Path))+ ext := strings.ToLower(path.Ext(assetPath))return ext == ".html" || ext == ".htm"}+// IsHTMLAssetIFrameSrc 判断资源地址是否为 HTML 文件 IFrame 渲染地址。+func IsHTMLAssetIFrameSrc(assetPath string) bool {+ parsed, err := url.Parse(assetPath)+ if err != nil || !strings.EqualFold(parsed.Query().Get("iframe"), "true") {+ return false+ }+ return IsLocalHTMLAssetPath(assetPath)+}+func assetPathAndBox(relativePath, defaultBoxID string) (cleanPath, boxID string, err error) {relativePath = strings.TrimSpace(relativePath)boxID = defaultBoxIDdiff --git a/kernel/model/export.go b/kernel/model/export.goindex aee24d9fe6..3b9db163ec 100644--- a/kernel/model/export.go+++ b/kernel/model/export.go@@ -1536,7 +1536,7 @@ func processIFrame(tree *parse.Tree) {func processHTMLFileIFrame(tree *parse.Tree) {// 导出 HTML 时将 HTML 文件组件转换为链接。processIFrameWithFilter(tree, func(src string) bool {- return IsHTMLAssetIFrameSrc(src)+ return IsLocalHTMLAssetPath(src)})}diff --git a/kernel/model/export_asset_test.go b/kernel/model/export_asset_test.goindex a7a08eb6b7..4447de828c 100644--- a/kernel/model/export_asset_test.go+++ b/kernel/model/export_asset_test.go@@ -103,16 +103,26 @@ func TestResolveExportAssetPaths(t *testing.T) {func TestProcessHTMLFileIFrame(t *testing.T) {root := &ast.Node{Type: ast.NodeDocument}component := &ast.Node{Type: ast.NodeIFrame, Tokens: []byte(`<iframe src="assets/component.html?iframe=true"></iframe>`)}+ legacyComponent := &ast.Node{Type: ast.NodeIFrame, Tokens: []byte(`<iframe src="assets/legacy.html"></iframe>`)}external := &ast.Node{Type: ast.NodeIFrame, Tokens: []byte(`<iframe src="https://example.com/component.html?iframe=true"></iframe>`)}+ nonHTML := &ast.Node{Type: ast.NodeIFrame, Tokens: []byte(`<iframe src="assets/component.svg?iframe=true"></iframe>`)}root.AppendChild(component)+ root.AppendChild(legacyComponent)root.AppendChild(external)+ root.AppendChild(nonHTML)processHTMLFileIFrame(&parse.Tree{Root: root})if component.Type != ast.NodeParagraph || component.FirstChild == nil || component.FirstChild.Type != ast.NodeLink {t.Fatalf("HTML file IFrame was not converted to a link: %#v", component)}+ if legacyComponent.Type != ast.NodeParagraph || legacyComponent.FirstChild == nil || legacyComponent.FirstChild.Type != ast.NodeLink {+ t.Fatalf("legacy HTML file IFrame was not converted to a link: %#v", legacyComponent)+ }if external.Type != ast.NodeIFrame || external.FirstChild != nil {t.Fatalf("external IFrame must remain unchanged: %#v", external)}+ if nonHTML.Type != ast.NodeIFrame || nonHTML.FirstChild != nil {+ t.Fatalf("non-HTML IFrame must remain unchanged: %#v", nonHTML)+ }}diff --git a/kernel/model/html_asset_test.go b/kernel/model/html_asset_test.goindex b00f58e4b4..1f84c443bb 100644--- a/kernel/model/html_asset_test.go+++ b/kernel/model/html_asset_test.go@@ -34,6 +34,29 @@ func TestHTMLAssetIFrameSrc(t *testing.T) {}}+func TestIsLocalHTMLAssetPath(t *testing.T) {+ for _, src := range []string{+ "assets/component.html",+ "./assets/component.htm?box=box-id",+ "/assets/component.HTML?iframe=true#preview",+ } {+ if !IsLocalHTMLAssetPath(src) {+ t.Fatalf("IsLocalHTMLAssetPath(%q) = false", src)+ }+ }++ for _, src := range []string{+ "component.html",+ "assets/component.xhtml",+ "https://example.com/assets/component.html",+ "../assets/component.html",+ } {+ if IsLocalHTMLAssetPath(src) {+ t.Fatalf("IsLocalHTMLAssetPath(%q) = true", src)+ }+ }+}+func TestIsHTMLAssetIFrameSrcRejectsInvalidSources(t *testing.T) {for _, src := range []string{"assets/component.html",