Skip to content

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

:bug: Preserve spaced media assets in Markdown exports https://github.com/siyuan-note/siyuan/issues/18351

Daniel9415b069a412
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 9697670a33..25c49eb977 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -4067,6 +4067,12 @@ func exportPandocConvertZip(boxID, baseFolderName string, docPaths, defBlockIDs
newAssets := getAssetsLinkDests(tree.Root, false)
for _, newAsset := range newAssets {
+ oldAsset, newAsset := resolveExportAssetPaths(newAsset, assetsOldNew, assetsNewOld)
+ if "" == oldAsset {
+ logging.LogWarnf("get asset old path for new asset [%s] failed", newAsset)
+ continue
+ }
+
newAsset = string(html.DecodeDestination([]byte(newAsset)))
cleanNewAsset := AssetPathWithoutQuery(newAsset)
@@ -4074,20 +4080,6 @@ func exportPandocConvertZip(boxID, baseFolderName string, docPaths, defBlockIDs
continue
}
- // 导出 Markdown 时链接路径中的空格被编码为 `%20`,需要替换回空格后才能正确获取原始资源路径
- // Improve export of Markdown hyperlink spaces https://github.com/siyuan-note/siyuan/issues/9792
- // No assets were exported when exporting Markdown https://github.com/siyuan-note/siyuan/issues/17046
- spaceEncodedNewAsset := strings.ReplaceAll(newAsset, " ", "%20")
- oldAsset := assetsNewOld[spaceEncodedNewAsset]
- if "" == oldAsset {
- spaceEncodedCleanNewAsset := strings.ReplaceAll(cleanNewAsset, " ", "%20")
- oldAsset = assetsNewOld[spaceEncodedCleanNewAsset]
- }
- if "" == oldAsset {
- logging.LogWarnf("get asset old path for new asset [%s] failed", spaceEncodedNewAsset)
- continue
- }
-
spaceDecodedOldAsset := strings.ReplaceAll(oldAsset, "%20", " ")
srcPath := ""
if IsEncryptedBox(treeBoxID) {
@@ -4178,6 +4170,17 @@ func exportPandocConvertZip(boxID, baseFolderName string, docPaths, defBlockIDs
return
}
+// 查表时使用重新解析后的原始路径,兼容 Markdown 链接中的 `%20` 和 HTML 资源路径中的字面空格。
+// 修复 Markdown.zip 遗漏文件名包含空格的音视频资源 https://github.com/siyuan-note/siyuan/issues/18351
+func resolveExportAssetPaths(asset string, assetsOldNew, assetsNewOld map[string]string) (oldAsset, newAsset string) {
+ newAsset = asset
+ if renamedAsset := assetsOldNew[asset]; "" != renamedAsset {
+ newAsset = renamedAsset
+ }
+ oldAsset = assetsNewOld[newAsset]
+ return
+}
+
func removeAssetsID(tree *parse.Tree, assetsOldNew, assetsNewOld map[string]string) {
assetNodes := getAssetsLinkDestsInTree(tree, false)
for _, node := range assetNodes {
diff --git a/kernel/model/export_asset_test.go b/kernel/model/export_asset_test.go
new file mode 100644
index 0000000000..f61c18fe3b
--- /dev/null
+++ b/kernel/model/export_asset_test.go
@@ -0,0 +1,101 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/conf"
+)
+
+func TestResolveExportAssetPaths(t *testing.T) {
+ const (
+ imageAsset = "assets/image%20file-20260724150608-42z1qwz.png"
+ videoAsset = "assets/video file-20260724150608-42z1qwz.webm"
+ audioAsset = "assets/audio file-20260724150608-42z1qwz.wav?box=20260724163822-tfrplrg"
+ iframeAsset = "assets/frame file-20260724150608-42z1qwz.html"
+ )
+
+ tests := []struct {
+ name string
+ removeID bool
+ expected map[string][2]string
+ }{
+ {
+ name: "preserve asset IDs",
+ expected: map[string][2]string{
+ imageAsset: {imageAsset, imageAsset},
+ videoAsset: {videoAsset, videoAsset},
+ audioAsset: {audioAsset, audioAsset},
+ iframeAsset: {iframeAsset, iframeAsset},
+ },
+ },
+ {
+ name: "remove asset IDs",
+ removeID: true,
+ expected: map[string][2]string{
+ "assets/image%20file.png": {imageAsset, "assets/image%20file.png"},
+ "assets/video file.webm": {videoAsset, "assets/video file.webm"},
+ "assets/audio file.wav?box=20260724163822-tfrplrg": {
+ audioAsset,
+ "assets/audio file.wav?box=20260724163822-tfrplrg",
+ },
+ iframeAsset: {iframeAsset, "assets/frame file.html"},
+ },
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ originalConf := Conf
+ Conf = NewAppConf()
+ Conf.Export = conf.NewExport()
+ Conf.Export.RemoveAssetsID = test.removeID
+ t.Cleanup(func() {
+ Conf = originalConf
+ })
+
+ root := &ast.Node{Type: ast.NodeDocument}
+ root.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte(imageAsset)})
+ root.AppendChild(&ast.Node{Type: ast.NodeVideo, Tokens: []byte(`<video src="` + videoAsset + `"></video>`)})
+ root.AppendChild(&ast.Node{Type: ast.NodeAudio, Tokens: []byte(`<audio src="` + audioAsset + `"></audio>`)})
+ root.AppendChild(&ast.Node{Type: ast.NodeIFrame, Tokens: []byte(`<iframe src="` + iframeAsset + `"></iframe>`)})
+
+ assetsOldNew, assetsNewOld := map[string]string{}, map[string]string{}
+ tree := &parse.Tree{Root: root}
+ removeAssetsID(tree, assetsOldNew, assetsNewOld)
+
+ assets := getAssetsLinkDests(root, false)
+ if len(assets) != len(test.expected) {
+ t.Fatalf("got %d rendered assets, want %d", len(assets), len(test.expected))
+ }
+ for _, asset := range assets {
+ oldAsset, newAsset := resolveExportAssetPaths(asset, assetsOldNew, assetsNewOld)
+ expected, ok := test.expected[asset]
+ if !ok {
+ t.Fatalf("unexpected rendered asset [%s]", asset)
+ }
+ if oldAsset != expected[0] || newAsset != expected[1] {
+ t.Fatalf("resolve asset [%s]: got [%s, %s], want [%s, %s]",
+ asset, oldAsset, newAsset, expected[0], expected[1])
+ }
+ }
+ })
+ }
+}