Skip to content

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

:bug: 导出 PDF/HTML 时丢失自定义表情 Fix https://github.com/siyuan-note/siyuan/issues/5535

Liang Ding236ffd8e6497
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 8b60da697c..c49c7665a7 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -644,6 +644,27 @@ func UnusedAssets() (ret []string) {
return
}
+func emojisInTree(tree *parse.Tree) (ret []string) {
+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if !entering {
+ return ast.WalkContinue
+ }
+ if ast.NodeEmojiImg == n.Type {
+ tokens := n.Tokens
+ idx := bytes.Index(tokens, []byte("src=\""))
+ if -1 == idx {
+ return ast.WalkContinue
+ }
+ src := tokens[idx+len("src=\""):]
+ src = src[:bytes.Index(src, []byte("\""))]
+ ret = append(ret, string(src))
+ }
+ return ast.WalkContinue
+ })
+ ret = gulu.Str.RemoveDuplicatedElem(ret)
+ return
+}
+
func assetsLinkDestsInTree(tree *parse.Tree) (ret []string) {
ret = []string{}
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 6be4a96b19..dfc55ab737 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -250,11 +250,14 @@ func ExportMarkdownHTML(id, savePath string, docx bool) (name, dom string) {
}
// 复制自定义表情图片
- from := filepath.Join(util.DataDir, "emojis")
- to := filepath.Join(savePath, "emojis")
- if err := gulu.File.Copy(from, to); nil != err {
- logging.LogErrorf("copy emojis from [%s] to [%s] failed: %s", from, savePath, err)
- return
+ emojis := emojisInTree(tree)
+ for _, emoji := range emojis {
+ from := filepath.Join(util.DataDir, emoji)
+ to := filepath.Join(savePath, emoji)
+ if err := gulu.File.Copy(from, to); nil != err {
+ logging.LogErrorf("copy emojis from [%s] to [%s] failed: %s", from, savePath, err)
+ return
+ }
}
luteEngine := NewLute()
@@ -265,6 +268,16 @@ func ExportMarkdownHTML(id, savePath string, docx bool) (name, dom string) {
processIFrame(tree)
}
+ // 自定义表情图片地址去掉开头的 /
+ ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
+ if !entering {
+ return ast.WalkContinue
+ }
+ if ast.NodeEmojiImg == n.Type {
+ n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("src=\"/emojis"), []byte("src=\"emojis"))
+ }
+ return ast.WalkContinue
+ })
dom = luteEngine.ProtylePreview(tree, luteEngine.RenderOptions)
return
}
@@ -341,11 +354,14 @@ func ExportHTML(id, savePath string, pdf bool) (name, dom string) {
}
// 复制自定义表情图片
- from := filepath.Join(util.DataDir, "emojis")
- to := filepath.Join(savePath, "emojis")
- if err := gulu.File.Copy(from, to); nil != err {
- logging.LogErrorf("copy emojis from [%s] to [%s] failed: %s", from, savePath, err)
- return
+ emojis := emojisInTree(tree)
+ for _, emoji := range emojis {
+ from := filepath.Join(util.DataDir, emoji)
+ to := filepath.Join(savePath, emoji)
+ if err := gulu.File.Copy(from, to); nil != err {
+ logging.LogErrorf("copy emojis from [%s] to [%s] failed: %s", from, savePath, err)
+ return
+ }
}
} else { // 导出 PDF 需要将资源文件路径改为 HTTP 伺服
luteEngine.RenderOptions.LinkBase = "http://127.0.0.1:" + util.ServerPort + "/"