From 6db7b847e9a42e34712f4e02bacb3eb8d4346de3 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Fri, 1 Nov 2024 09:38:34 +0800 Subject: [PATCH] :art: All plain text formats are supported when comparing data snapshots https://github.com/siyuan-note/siyuan/issues/12975 --- app/src/history/diff.ts | 4 ++-- kernel/api/repo.go | 10 +++++----- kernel/mobile/kernel.go | 4 ---- kernel/model/repository.go | 21 ++++++++++++--------- kernel/util/file.go | 17 ----------------- 5 files changed, 19 insertions(+), 37 deletions(-) diff --git a/app/src/history/diff.ts b/app/src/history/diff.ts index 3b6c1017b..3e1b01a44 100644 --- a/app/src/history/diff.ts +++ b/app/src/history/diff.ts @@ -86,7 +86,7 @@ const renderCompare = (app: App, element: HTMLElement) => { textElement.previousElementSibling.classList.remove("fn__none"); textElement.classList.add("fn__none"); leftElement.lastElementChild.classList.add("fn__none"); - } else if (response.data.isProtyleDoc) { + } else if (response.data.displayInText) { textElement.value = response.data.content; textElement.classList.remove("fn__none"); leftElement.lastElementChild.classList.add("fn__none"); @@ -117,7 +117,7 @@ const renderCompare = (app: App, element: HTMLElement) => { textElement.previousElementSibling.classList.remove("fn__none"); textElement.classList.add("fn__none"); rightElement.lastElementChild.classList.add("fn__none"); - } else if (response.data.isProtyleDoc) { + } else if (response.data.displayInText) { textElement.value = response.data.content; textElement.classList.remove("fn__none"); rightElement.lastElementChild.classList.add("fn__none"); diff --git a/kernel/api/repo.go b/kernel/api/repo.go index f0b917e85..cea78bff8 100644 --- a/kernel/api/repo.go +++ b/kernel/api/repo.go @@ -70,7 +70,7 @@ func openRepoSnapshotDoc(c *gin.Context) { } id := arg["id"].(string) - title, content, isProtyleDoc, updated, err := model.OpenRepoSnapshotDoc(id) + title, content, displayInText, updated, err := model.OpenRepoSnapshotDoc(id) if err != nil { ret.Code = -1 ret.Msg = err.Error() @@ -78,10 +78,10 @@ func openRepoSnapshotDoc(c *gin.Context) { } ret.Data = map[string]interface{}{ - "title": title, - "content": content, - "isProtyleDoc": isProtyleDoc, - "updated": updated, + "title": title, + "content": content, + "displayInText": displayInText, + "updated": updated, } } diff --git a/kernel/mobile/kernel.go b/kernel/mobile/kernel.go index 900238fcf..6278a0b22 100644 --- a/kernel/mobile/kernel.go +++ b/kernel/mobile/kernel.go @@ -98,10 +98,6 @@ func GetAssetAbsPath(asset string) (ret string) { return } -func GetMimeTypeByExt(ext string) string { - return util.GetMimeTypeByExt(ext) -} - func SetTimezone(container, appDir, timezoneID string) { if "ios" == container { os.Setenv("ZONEINFO", filepath.Join(appDir, "app", "zoneinfo.zip")) diff --git a/kernel/model/repository.go b/kernel/model/repository.go index 86e916e17..f2d646214 100644 --- a/kernel/model/repository.go +++ b/kernel/model/repository.go @@ -24,6 +24,7 @@ import ( "errors" "fmt" "math" + "mime" "net/http" "os" "path" @@ -79,7 +80,7 @@ func GetRepoFile(fileID string) (ret []byte, p string, err error) { return } -func OpenRepoSnapshotDoc(fileID string) (title, content string, isProtyleDoc bool, updated int64, err error) { +func OpenRepoSnapshotDoc(fileID string) (title, content string, displayInText bool, updated int64, err error) { if 1 > len(Conf.Repo.Key) { err = errors.New(Conf.Language(26)) return @@ -105,16 +106,15 @@ func OpenRepoSnapshotDoc(fileID string) (title, content string, isProtyleDoc boo if strings.HasSuffix(file.Path, ".sy") { luteEngine := NewLute() var snapshotTree *parse.Tree - isProtyleDoc, snapshotTree, err = parseTreeInSnapshot(data, luteEngine) + displayInText, snapshotTree, err = parseTreeInSnapshot(data, luteEngine) if err != nil { logging.LogErrorf("parse tree from snapshot file [%s] failed", fileID) return } title = snapshotTree.Root.IALAttr("title") - if !isProtyleDoc { + if !displayInText { renderTree := &parse.Tree{Root: &ast.Node{Type: ast.NodeDocument}} - var unlinks []*ast.Node ast.Walk(snapshotTree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { if !entering { @@ -142,7 +142,7 @@ func OpenRepoSnapshotDoc(fileID string) (title, content string, isProtyleDoc boo } luteEngine.RenderOptions.ProtyleContenteditable = false - if isProtyleDoc { + if displayInText { util.PushMsg(Conf.Language(36), 5000) formatRenderer := render.NewFormatRenderer(snapshotTree, luteEngine.RenderOptions) content = gulu.Str.FromBytes(formatRenderer.Render()) @@ -150,9 +150,12 @@ func OpenRepoSnapshotDoc(fileID string) (title, content string, isProtyleDoc boo content = luteEngine.Tree2BlockDOM(snapshotTree, luteEngine.RenderOptions) } } else { - isProtyleDoc = true + displayInText = true title = path.Base(file.Path) - if strings.HasSuffix(file.Path, ".json") { + + if mimeType := mime.TypeByExtension(filepath.Ext(file.Path)); strings.HasPrefix(mimeType, "text/") { + // 如果是文本文件,直接返回文本内容 + // All plain text formats are supported when comparing data snapshots https://github.com/siyuan-note/siyuan/issues/12975 content = gulu.Str.FromBytes(data) } else { if strings.Contains(file.Path, "assets/") { // 剔除笔记本级或者文档级资源文件路径前缀 @@ -328,8 +331,8 @@ func parseTitleInSnapshot(fileID string, repo *dejavu.Repo, luteEngine *lute.Lut return } -func parseTreeInSnapshot(data []byte, luteEngine *lute.Lute) (isProtyleDoc bool, tree *parse.Tree, err error) { - isProtyleDoc = 1024*1024*1 <= len(data) +func parseTreeInSnapshot(data []byte, luteEngine *lute.Lute) (isLargeDoc bool, tree *parse.Tree, err error) { + isLargeDoc = 1024*1024*1 <= len(data) tree, err = filesys.ParseJSONWithoutFix(data, luteEngine.ParseOptions) if err != nil { return diff --git a/kernel/util/file.go b/kernel/util/file.go index 3aab9852d..560c62d90 100644 --- a/kernel/util/file.go +++ b/kernel/util/file.go @@ -20,7 +20,6 @@ import ( "bytes" "io" "io/fs" - "mime" "os" "path" "path/filepath" @@ -30,7 +29,6 @@ import ( "github.com/88250/gulu" "github.com/88250/lute/ast" - "github.com/gabriel-vasile/mimetype" "github.com/siyuan-note/filelock" "github.com/siyuan-note/logging" ) @@ -75,21 +73,6 @@ func GetUniqueFilename(filePath string) string { } } -func GetMimeTypeByExt(filePath string) (ret string) { - ret = mime.TypeByExtension(filepath.Ext(filePath)) - if "" == ret { - m, err := mimetype.DetectFile(filePath) - if err != nil { - logging.LogErrorf("detect mime type of [%s] failed: %s", filePath, err) - return - } - if nil != m { - ret = m.String() - } - } - return -} - func IsSymlinkPath(absPath string) bool { fi, err := os.Lstat(absPath) if err != nil {