Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2023-11-29 16:41:58 +08:00
commit 447fc0f881
2 changed files with 53 additions and 9 deletions

View file

@ -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
}

View file

@ -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 controls=\"controls\" src=\"" + link.TextMarkAHref + "\" data-src=\"" + link.TextMarkAHref + "\"></audio>")}
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 controls=\"controls\" src=\"" + link.TextMarkAHref + "\" data-src=\"" + link.TextMarkAHref + "\"></video>")}
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()