瀏覽代碼

:art: Support searching EPUB asset content Fix https://github.com/siyuan-note/siyuan/issues/9000

Daniel 1 年之前
父節點
當前提交
04cf28c962
共有 4 個文件被更改,包括 47 次插入1 次删除
  1. 1 1
      app/src/constants.ts
  2. 2 0
      kernel/go.mod
  3. 4 0
      kernel/go.sum
  4. 40 0
      kernel/model/asset_content.go

+ 1 - 1
app/src/constants.ts

@@ -493,7 +493,7 @@ export abstract class Constants {
     public static readonly SIYUAN_ASSETS_EXTS: string[] = [".pdf"].concat(Constants.SIYUAN_ASSETS_IMAGE).concat(Constants.SIYUAN_ASSETS_AUDIO).concat(Constants.SIYUAN_ASSETS_VIDEO);
     public static readonly SIYUAN_ASSETS_EXTS: string[] = [".pdf"].concat(Constants.SIYUAN_ASSETS_IMAGE).concat(Constants.SIYUAN_ASSETS_AUDIO).concat(Constants.SIYUAN_ASSETS_VIDEO);
     public static readonly SIYUAN_ASSETS_SEARCH: string[] = [".txt", ".md", ".markdown", ".docx", ".xlsx", ".pptx", ".pdf", ".json", ".log", ".sql", ".html", ".xml", ".java", ".h", ".c",
     public static readonly SIYUAN_ASSETS_SEARCH: string[] = [".txt", ".md", ".markdown", ".docx", ".xlsx", ".pptx", ".pdf", ".json", ".log", ".sql", ".html", ".xml", ".java", ".h", ".c",
         ".cpp", ".go", ".rs", ".swift", ".kt", ".py", ".php", ".js", ".css", ".ts", ".sh", ".bat", ".cmd", ".ini", ".yaml",
         ".cpp", ".go", ".rs", ".swift", ".kt", ".py", ".php", ".js", ".css", ".ts", ".sh", ".bat", ".cmd", ".ini", ".yaml",
-        ".rst", ".adoc", ".textile", ".opml", ".org", ".wiki"];
+        ".rst", ".adoc", ".textile", ".opml", ".org", ".wiki", ".epub"];
 
 
     // protyle
     // protyle
     public static readonly SIYUAN_CONFIG_APPEARANCE_DARK_CODE: string[] = ["a11y-dark", "agate", "an-old-hope", "androidstudio",
     public static readonly SIYUAN_CONFIG_APPEARANCE_DARK_CODE: string[] = ["a11y-dark", "agate", "an-old-hope", "androidstudio",

+ 2 - 0
kernel/go.mod

@@ -149,6 +149,8 @@ require (
 	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 	github.com/ugorji/go/codec v1.2.11 // indirect
 	github.com/ugorji/go/codec v1.2.11 // indirect
 	github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
 	github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
+	github.com/wmentor/epub v1.0.1 // indirect
+	github.com/wmentor/html v1.0.1 // indirect
 	github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect
 	github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect
 	github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
 	github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
 	github.com/yusufpapurcu/wmi v1.2.3 // indirect
 	github.com/yusufpapurcu/wmi v1.2.3 // indirect

+ 4 - 0
kernel/go.sum

@@ -562,6 +562,10 @@ github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9
 github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
 github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
 github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
 github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
 github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
 github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
+github.com/wmentor/epub v1.0.1 h1:88Jod0B9g+7NxdbAHhmE/qHnK+i4Ns/NJjeyQ/V8YwI=
+github.com/wmentor/epub v1.0.1/go.mod h1:PydFqUA+hmJ9g1lI/3XRYAK2TPC5WVFEZh7lkV9ETtM=
+github.com/wmentor/html v1.0.1 h1:iIuDyH7pohHMMzdD5WQhvya5UyIecFDWTYzdM873Ook=
+github.com/wmentor/html v1.0.1/go.mod h1:nwnrzqG0xD0Q1mO+a4iNfgh40K8/tFuq6smFlHOBu10=
 github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
 github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
 github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
 github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
 github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj09jdMlkY0aiA6+Skbtl3/c=
 github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj09jdMlkY0aiA6+Skbtl3/c=

+ 40 - 0
kernel/model/asset_content.go

@@ -39,6 +39,7 @@ import (
 	"github.com/siyuan-note/siyuan/kernel/sql"
 	"github.com/siyuan-note/siyuan/kernel/sql"
 	"github.com/siyuan-note/siyuan/kernel/task"
 	"github.com/siyuan-note/siyuan/kernel/task"
 	"github.com/siyuan-note/siyuan/kernel/util"
 	"github.com/siyuan-note/siyuan/kernel/util"
+	"github.com/wmentor/epub"
 	"github.com/xuri/excelize/v2"
 	"github.com/xuri/excelize/v2"
 )
 )
 
 
@@ -456,6 +457,7 @@ func NewAssetsSearcher() *AssetsSearcher {
 			".pptx":     &PptxAssetParser{},
 			".pptx":     &PptxAssetParser{},
 			".xlsx":     &XlsxAssetParser{},
 			".xlsx":     &XlsxAssetParser{},
 			".pdf":      &PdfAssetParser{},
 			".pdf":      &PdfAssetParser{},
+			".epub":     &EpubAssetParser{},
 		},
 		},
 
 
 		lock: &sync.Mutex{},
 		lock: &sync.Mutex{},
@@ -750,3 +752,41 @@ func (parser *PdfAssetParser) Parse(absPath string) (ret *AssetParseResult) {
 	}
 	}
 	return
 	return
 }
 }
+
+type EpubAssetParser struct {
+}
+
+func (parser *EpubAssetParser) Parse(absPath string) (ret *AssetParseResult) {
+	if !strings.HasSuffix(strings.ToLower(absPath), ".epub") {
+		return
+	}
+
+	if !gulu.File.IsExist(absPath) {
+		return
+	}
+
+	tmp := copyTempAsset(absPath)
+	if "" == tmp {
+		return
+	}
+	defer os.RemoveAll(tmp)
+
+	f, err := os.Open(tmp)
+	if nil != err {
+		logging.LogErrorf("open [%s] failed: [%s]", tmp, err)
+		return
+	}
+	defer f.Close()
+
+	buf := bytes.Buffer{}
+	if err = epub.ToTxt(tmp, &buf); nil != err {
+		logging.LogErrorf("convert [%s] failed: [%s]", tmp, err)
+		return
+	}
+
+	content := normalizeAssetContent(buf.String())
+	ret = &AssetParseResult{
+		Content: content,
+	}
+	return
+}