From 7eb597652dcdee47ee6c01a69fb012a73e4b0d25 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Wed, 29 Nov 2023 16:28:35 +0800 Subject: [PATCH 1/2] :art: Automatically download network assets when the cloud inbox is moved to docs https://github.com/siyuan-note/siyuan/issues/9775 --- kernel/model/assets.go | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/kernel/model/assets.go b/kernel/model/assets.go index 8dee9e5fc..7e19a826a 100644 --- a/kernel/model/assets.go +++ b/kernel/model/assets.go @@ -244,15 +244,23 @@ func NetAssets2LocalAssets(rootID string) (err error) { SetProxy(httpclient.ProxyFromEnvironment) ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { - if !entering || (ast.NodeLinkDest != n.Type && !n.IsTextMarkType("a")) { + if !entering || (ast.NodeLinkDest != n.Type && !n.IsTextMarkType("a") && ast.NodeAudio != n.Type && ast.NodeVideo != n.Type) { return ast.WalkContinue } var dest []byte if ast.NodeLinkDest == n.Type { dest = n.Tokens - } else { + } else if n.IsTextMarkType("a") { dest = []byte(n.TextMarkAHref) + } else if ast.NodeAudio == n.Type || ast.NodeVideo == n.Type { + if srcIndex := bytes.Index(n.Tokens, []byte("src=\"")); 0 < srcIndex { + src := n.Tokens[srcIndex+len("src=\""):] + if srcIndex = bytes.Index(src, []byte("\"")); 0 < srcIndex { + src = src[:bytes.Index(src, []byte("\""))] + dest = bytes.TrimSpace(src) + } + } } if util.IsAssetLinkDest(dest) { @@ -278,8 +286,10 @@ func NetAssets2LocalAssets(rootID string) (err error) { if ast.NodeLinkDest == n.Type { n.Tokens = []byte("assets/" + name) - } else { + } else if n.IsTextMarkType("a") { n.TextMarkAHref = "assets/" + name + } else if ast.NodeAudio == n.Type || ast.NodeVideo == n.Type { + n.Tokens = bytes.ReplaceAll(n.Tokens, dest, []byte("assets/"+name)) } files++ return ast.WalkContinue @@ -360,8 +370,10 @@ func NetAssets2LocalAssets(rootID string) (err error) { if ast.NodeLinkDest == n.Type { n.Tokens = []byte("assets/" + name) - } else { + } else if n.IsTextMarkType("a") { n.TextMarkAHref = "assets/" + name + } else if ast.NodeAudio == n.Type || ast.NodeVideo == n.Type { + n.Tokens = bytes.ReplaceAll(n.Tokens, dest, []byte("assets/"+name)) } files++ } @@ -790,7 +802,7 @@ func UnusedAssets() (ret []string) { } var linkDestFolderPaths, linkDestFilePaths []string - for dest, _ := range dests { + for dest := range dests { if !strings.HasPrefix(dest, "assets/") { continue } @@ -812,7 +824,7 @@ func UnusedAssets() (ret []string) { // 排除文件夹链接 var toRemoves []string - for asset, _ := range assetsPathMap { + for asset := range assetsPathMap { for _, linkDestFolder := range linkDestFolderPaths { if strings.HasPrefix(asset, linkDestFolder) { toRemoves = append(toRemoves, asset) @@ -838,7 +850,7 @@ func UnusedAssets() (ret []string) { } var toRemoves []string - for asset, _ := range assetsPathMap { + for asset := range assetsPathMap { if strings.HasSuffix(asset, "ocr-texts.json") { // 排除 OCR 结果文本 toRemoves = append(toRemoves, asset) @@ -868,7 +880,7 @@ func UnusedAssets() (ret []string) { return } - for asset, _ := range assetsPathMap { + for asset := range assetsPathMap { if bytes.Contains(data, []byte(asset)) { toRemoves = append(toRemoves, asset) } @@ -946,7 +958,7 @@ func MissingAssets() (ret []string) { } } - for dest, _ := range dests { + for dest := range dests { if !strings.HasPrefix(dest, "assets/") { continue } From 4f76030b1adc831a5309b6cfcf7bbd6808b1e746 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Wed, 29 Nov 2023 16:28:54 +0800 Subject: [PATCH 2/2] :art: Convert mp3 and mp4 hyperlinks to audio and video when moving cloud inbox to docs https://github.com/siyuan-note/siyuan/issues/9778 --- kernel/model/file.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/kernel/model/file.go b/kernel/model/file.go index 84b932749..6ec99b314 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -1593,6 +1593,38 @@ func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) { tree.Root.AppendChild(treenode.NewParagraph()) } + // 如果段落块中仅包含一个 mp3/mp4 超链接,则将其转换为音视频块 + // Convert mp3 and mp4 hyperlinks to audio and video when moving cloud inbox to docs https://github.com/siyuan-note/siyuan/issues/9778 + var unlinks []*ast.Node + ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { + if !entering { + return ast.WalkContinue + } + + if ast.NodeParagraph == n.Type { + link := n.FirstChild + if nil != link && link.IsTextMarkType("a") { + if strings.HasSuffix(link.TextMarkAHref, ".mp3") { + unlinks = append(unlinks, n) + audio := &ast.Node{ID: n.ID, Type: ast.NodeAudio, Tokens: []byte("")} + audio.SetIALAttr("id", n.ID) + audio.SetIALAttr("updated", util.TimeFromID(n.ID)) + n.InsertBefore(audio) + } else if strings.HasSuffix(link.TextMarkAHref, ".mp4") { + unlinks = append(unlinks, n) + video := &ast.Node{ID: n.ID, Type: ast.NodeVideo, Tokens: []byte("")} + video.SetIALAttr("id", n.ID) + video.SetIALAttr("updated", util.TimeFromID(n.ID)) + n.InsertBefore(video) + } + } + } + return ast.WalkContinue + }) + for _, unlink := range unlinks { + unlink.Unlink() + } + transaction := &Transaction{DoOperations: []*Operation{{Action: "create", Data: tree}}} PerformTransactions(&[]*Transaction{transaction}) WaitForWritingFiles()