From 8d9eafbfd8d5805b94f5008c3a4c5fa999a17f87 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Wed, 28 Feb 2024 21:08:46 +0800 Subject: [PATCH] :bug: Unable to search audio, video blocks by asset path https://github.com/siyuan-note/siyuan/issues/10468 --- kernel/model/assets.go | 34 +++++++--------------------------- kernel/model/export.go | 2 +- kernel/treenode/node.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 28 deletions(-) diff --git a/kernel/model/assets.go b/kernel/model/assets.go index 6786431d0..4c27560da 100644 --- a/kernel/model/assets.go +++ b/kernel/model/assets.go @@ -1104,21 +1104,21 @@ func assetsLinkDestsInNode(node *ast.Node) (ret []string) { } if ast.NodeLinkDest == n.Type { - if !isRelativePath(n.Tokens) { + if !treenode.IsRelativePath(n.Tokens) { return ast.WalkContinue } dest := strings.TrimSpace(string(n.Tokens)) ret = append(ret, dest) } else if n.IsTextMarkType("a") { - if !isRelativePath(gulu.Str.ToBytes(n.TextMarkAHref)) { + if !treenode.IsRelativePath(gulu.Str.ToBytes(n.TextMarkAHref)) { return ast.WalkContinue } dest := strings.TrimSpace(n.TextMarkAHref) ret = append(ret, dest) } else if n.IsTextMarkType("file-annotation-ref") { - if !isRelativePath(gulu.Str.ToBytes(n.TextMarkFileAnnotationRefID)) { + if !treenode.IsRelativePath(gulu.Str.ToBytes(n.TextMarkFileAnnotationRefID)) { return ast.WalkContinue } @@ -1136,24 +1136,14 @@ func assetsLinkDestsInNode(node *ast.Node) (ret []string) { // 兼容两种属性名 custom-data-assets 和 data-assets https://github.com/siyuan-note/siyuan/issues/4122#issuecomment-1154796568 dataAssets = n.IALAttr("data-assets") } - if "" == dataAssets || !isRelativePath([]byte(dataAssets)) { + if "" == dataAssets || !treenode.IsRelativePath([]byte(dataAssets)) { return ast.WalkContinue } ret = append(ret, dataAssets) } else { // HTMLBlock/InlineHTML/IFrame/Audio/Video - if index := bytes.Index(n.Tokens, []byte("src=\"")); 0 < index { - src := n.Tokens[index+len("src=\""):] - if index = bytes.Index(src, []byte("\"")); 0 < index { - src = src[:bytes.Index(src, []byte("\""))] - if !isRelativePath(src) { - return ast.WalkContinue - } - - dest := strings.TrimSpace(string(src)) - ret = append(ret, dest) - } else { - logging.LogWarnf("src is missing the closing double quote in tree [%s] ", node.Box+node.Path) - } + dest := treenode.GetNodeSrcTokens(n) + if "" != dest { + ret = append(ret, dest) } } } @@ -1169,16 +1159,6 @@ func assetsLinkDestsInNode(node *ast.Node) (ret []string) { return } -func isRelativePath(dest []byte) bool { - if 1 > len(dest) { - return false - } - if '/' == dest[0] { - return false - } - return !bytes.Contains(dest, []byte(":")) -} - // allAssetAbsPaths 返回 asset 相对路径(assets/xxx)到绝对路径(F:\SiYuan\data\assets\xxx)的映射。 func allAssetAbsPaths() (assetsAbsPathMap map[string]string, err error) { notebooks, err := ListNotebooks() diff --git a/kernel/model/export.go b/kernel/model/export.go index 93ba47f3c..763a49081 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -1620,7 +1620,7 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) ( case av.KeyTypeMAsset: // 导出资源文件列 https://github.com/siyuan-note/siyuan/issues/9919 for _, value := range keyValues.Values { for _, asset := range value.MAsset { - if !isRelativePath([]byte(asset.Content)) { + if !treenode.IsRelativePath([]byte(asset.Content)) { continue } diff --git a/kernel/treenode/node.go b/kernel/treenode/node.go index f9076b07c..a3f6bad5d 100644 --- a/kernel/treenode/node.go +++ b/kernel/treenode/node.go @@ -283,6 +283,9 @@ func NodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATi buf.WriteByte(lex.ItemBackslash) case ast.NodeBackslashContent: buf.Write(n.Tokens) + case ast.NodeAudio, ast.NodeVideo: + buf.WriteString(GetNodeSrcTokens(n)) + buf.WriteByte(' ') } lastSpace = false return ast.WalkContinue @@ -293,6 +296,34 @@ func NodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATi return buf.String() } +func GetNodeSrcTokens(n *ast.Node) (ret string) { + if index := bytes.Index(n.Tokens, []byte("src=\"")); 0 < index { + src := n.Tokens[index+len("src=\""):] + if index = bytes.Index(src, []byte("\"")); 0 < index { + src = src[:bytes.Index(src, []byte("\""))] + if !IsRelativePath(src) { + return + } + + ret = strings.TrimSpace(string(src)) + return + } + + logging.LogWarnf("src is missing the closing double quote in tree [%s] ", n.Box+n.Path) + } + return +} + +func IsRelativePath(dest []byte) bool { + if 1 > len(dest) { + return false + } + if '/' == dest[0] { + return false + } + return !bytes.Contains(dest, []byte(":")) +} + func FirstLeafBlock(node *ast.Node) (ret *ast.Node) { ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus { if !entering || n.IsMarker() {