Browse Source

:art: Improved PDF asset file unreferenced detection https://github.com/siyuan-note/siyuan/issues/7964

Liang Ding 2 năm trước cách đây
mục cha
commit
204c3b469d
2 tập tin đã thay đổi với 20 bổ sung9 xóa
  1. 16 9
      kernel/model/assets.go
  2. 4 0
      kernel/model/export.go

+ 16 - 9
kernel/model/assets.go

@@ -634,17 +634,15 @@ func UnusedAssets() (ret []string) {
 
 		for _, dest := range linkDestFilePaths {
 			linkDestMap[dest] = true
+
+			if strings.HasSuffix(dest, ".pdf") {
+				linkDestMap[dest+".sya"] = true
+			}
 		}
 	}
 
 	var toRemoves []string
 	for asset, _ := range assetsPathMap {
-		if strings.HasSuffix(asset, ".sya") {
-			// 排除文件注解和对应文件
-			toRemoves = append(toRemoves, asset, strings.TrimSuffix(asset, ".sya"))
-			continue
-		}
-
 		if strings.HasSuffix(asset, "ocr-texts.json") {
 			// 排除 OCR 结果文本
 			toRemoves = append(toRemoves, asset)
@@ -656,8 +654,8 @@ func UnusedAssets() (ret []string) {
 	}
 
 	dataAssetsAbsPath := util.GetDataAssetsAbsPath()
-	for _, assetAbsPath := range assetsPathMap {
-		if _, ok := linkDestMap[assetAbsPath]; ok {
+	for dest, assetAbsPath := range assetsPathMap {
+		if _, ok := linkDestMap[dest]; ok {
 			continue
 		}
 
@@ -704,7 +702,7 @@ func assetsLinkDestsInTree(tree *parse.Tree) (ret []string) {
 		// 修改以下代码时需要同时修改 database 构造行级元素实现,增加必要的类型
 		if !entering || (ast.NodeLinkDest != n.Type && ast.NodeHTMLBlock != n.Type && ast.NodeInlineHTML != n.Type &&
 			ast.NodeIFrame != n.Type && ast.NodeWidget != n.Type && ast.NodeAudio != n.Type && ast.NodeVideo != n.Type &&
-			!n.IsTextMarkType("a")) {
+			!n.IsTextMarkType("a") && !n.IsTextMarkType("file-annotation-ref")) {
 			return ast.WalkContinue
 		}
 
@@ -722,6 +720,14 @@ func assetsLinkDestsInTree(tree *parse.Tree) (ret []string) {
 
 			dest := strings.TrimSpace(n.TextMarkAHref)
 			ret = append(ret, dest)
+		} else if n.IsTextMarkType("file-annotation-ref") {
+			if !isRelativePath(gulu.Str.ToBytes(n.TextMarkFileAnnotationRefID)) {
+				return ast.WalkContinue
+			}
+
+			dest := n.TextMarkFileAnnotationRefID[:strings.LastIndexByte(n.TextMarkFileAnnotationRefID, '/')]
+			dest = strings.TrimSpace(dest)
+			ret = append(ret, dest)
 		} else {
 			if ast.NodeWidget == n.Type {
 				dataAssets := n.IALAttr("custom-data-assets")
@@ -752,6 +758,7 @@ func assetsLinkDestsInTree(tree *parse.Tree) (ret []string) {
 		}
 		return ast.WalkContinue
 	})
+	ret = gulu.Str.RemoveDuplicatedElem(ret)
 	return
 }
 

+ 4 - 0
kernel/model/export.go

@@ -403,6 +403,10 @@ func ExportMarkdownHTML(id, savePath string, docx, merge bool) (name, dom string
 	assets := assetsLinkDestsInTree(tree)
 	for _, asset := range assets {
 		if strings.HasPrefix(asset, "assets/") {
+			if strings.Contains(asset, "?") {
+				asset = asset[:strings.LastIndex(asset, "?")]
+			}
+
 			srcAbsPath, err := GetAssetAbsPath(asset)
 			if nil != err {
 				logging.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)