diff --git a/app/src/constants.ts b/app/src/constants.ts index ac164f56d..8b60daa89 100644 --- a/app/src/constants.ts +++ b/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_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", - ".rst", ".adoc", ".textile", ".opml", ".org", ".wiki"]; + ".rst", ".adoc", ".textile", ".opml", ".org", ".wiki", ".epub"]; // protyle public static readonly SIYUAN_CONFIG_APPEARANCE_DARK_CODE: string[] = ["a11y-dark", "agate", "an-old-hope", "androidstudio", diff --git a/kernel/go.mod b/kernel/go.mod index a0e156d5d..7ac545190 100644 --- a/kernel/go.mod +++ b/kernel/go.mod @@ -149,6 +149,8 @@ require ( github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // 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/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect diff --git a/kernel/go.sum b/kernel/go.sum index 1e84cd9b2..27e0f0a15 100644 --- a/kernel/go.sum +++ b/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/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/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/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj09jdMlkY0aiA6+Skbtl3/c= diff --git a/kernel/model/asset_content.go b/kernel/model/asset_content.go index ff45540c7..5822cc835 100644 --- a/kernel/model/asset_content.go +++ b/kernel/model/asset_content.go @@ -39,6 +39,7 @@ import ( "github.com/siyuan-note/siyuan/kernel/sql" "github.com/siyuan-note/siyuan/kernel/task" "github.com/siyuan-note/siyuan/kernel/util" + "github.com/wmentor/epub" "github.com/xuri/excelize/v2" ) @@ -456,6 +457,7 @@ func NewAssetsSearcher() *AssetsSearcher { ".pptx": &PptxAssetParser{}, ".xlsx": &XlsxAssetParser{}, ".pdf": &PdfAssetParser{}, + ".epub": &EpubAssetParser{}, }, lock: &sync.Mutex{}, @@ -750,3 +752,41 @@ func (parser *PdfAssetParser) Parse(absPath string) (ret *AssetParseResult) { } 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 +}