🎨 All plain text formats are supported when comparing data snapshots https://github.com/siyuan-note/siyuan/issues/12975

This commit is contained in:
Daniel 2024-11-01 09:38:34 +08:00
parent 641550e1ed
commit 6db7b847e9
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
5 changed files with 19 additions and 37 deletions

View file

@ -86,7 +86,7 @@ const renderCompare = (app: App, element: HTMLElement) => {
textElement.previousElementSibling.classList.remove("fn__none"); textElement.previousElementSibling.classList.remove("fn__none");
textElement.classList.add("fn__none"); textElement.classList.add("fn__none");
leftElement.lastElementChild.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.value = response.data.content;
textElement.classList.remove("fn__none"); textElement.classList.remove("fn__none");
leftElement.lastElementChild.classList.add("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.previousElementSibling.classList.remove("fn__none");
textElement.classList.add("fn__none"); textElement.classList.add("fn__none");
rightElement.lastElementChild.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.value = response.data.content;
textElement.classList.remove("fn__none"); textElement.classList.remove("fn__none");
rightElement.lastElementChild.classList.add("fn__none"); rightElement.lastElementChild.classList.add("fn__none");

View file

@ -70,7 +70,7 @@ func openRepoSnapshotDoc(c *gin.Context) {
} }
id := arg["id"].(string) id := arg["id"].(string)
title, content, isProtyleDoc, updated, err := model.OpenRepoSnapshotDoc(id) title, content, displayInText, updated, err := model.OpenRepoSnapshotDoc(id)
if err != nil { if err != nil {
ret.Code = -1 ret.Code = -1
ret.Msg = err.Error() ret.Msg = err.Error()
@ -78,10 +78,10 @@ func openRepoSnapshotDoc(c *gin.Context) {
} }
ret.Data = map[string]interface{}{ ret.Data = map[string]interface{}{
"title": title, "title": title,
"content": content, "content": content,
"isProtyleDoc": isProtyleDoc, "displayInText": displayInText,
"updated": updated, "updated": updated,
} }
} }

View file

@ -98,10 +98,6 @@ func GetAssetAbsPath(asset string) (ret string) {
return return
} }
func GetMimeTypeByExt(ext string) string {
return util.GetMimeTypeByExt(ext)
}
func SetTimezone(container, appDir, timezoneID string) { func SetTimezone(container, appDir, timezoneID string) {
if "ios" == container { if "ios" == container {
os.Setenv("ZONEINFO", filepath.Join(appDir, "app", "zoneinfo.zip")) os.Setenv("ZONEINFO", filepath.Join(appDir, "app", "zoneinfo.zip"))

View file

@ -24,6 +24,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
"mime"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -79,7 +80,7 @@ func GetRepoFile(fileID string) (ret []byte, p string, err error) {
return 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) { if 1 > len(Conf.Repo.Key) {
err = errors.New(Conf.Language(26)) err = errors.New(Conf.Language(26))
return return
@ -105,16 +106,15 @@ func OpenRepoSnapshotDoc(fileID string) (title, content string, isProtyleDoc boo
if strings.HasSuffix(file.Path, ".sy") { if strings.HasSuffix(file.Path, ".sy") {
luteEngine := NewLute() luteEngine := NewLute()
var snapshotTree *parse.Tree var snapshotTree *parse.Tree
isProtyleDoc, snapshotTree, err = parseTreeInSnapshot(data, luteEngine) displayInText, snapshotTree, err = parseTreeInSnapshot(data, luteEngine)
if err != nil { if err != nil {
logging.LogErrorf("parse tree from snapshot file [%s] failed", fileID) logging.LogErrorf("parse tree from snapshot file [%s] failed", fileID)
return return
} }
title = snapshotTree.Root.IALAttr("title") title = snapshotTree.Root.IALAttr("title")
if !isProtyleDoc { if !displayInText {
renderTree := &parse.Tree{Root: &ast.Node{Type: ast.NodeDocument}} renderTree := &parse.Tree{Root: &ast.Node{Type: ast.NodeDocument}}
var unlinks []*ast.Node var unlinks []*ast.Node
ast.Walk(snapshotTree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { ast.Walk(snapshotTree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering { if !entering {
@ -142,7 +142,7 @@ func OpenRepoSnapshotDoc(fileID string) (title, content string, isProtyleDoc boo
} }
luteEngine.RenderOptions.ProtyleContenteditable = false luteEngine.RenderOptions.ProtyleContenteditable = false
if isProtyleDoc { if displayInText {
util.PushMsg(Conf.Language(36), 5000) util.PushMsg(Conf.Language(36), 5000)
formatRenderer := render.NewFormatRenderer(snapshotTree, luteEngine.RenderOptions) formatRenderer := render.NewFormatRenderer(snapshotTree, luteEngine.RenderOptions)
content = gulu.Str.FromBytes(formatRenderer.Render()) 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) content = luteEngine.Tree2BlockDOM(snapshotTree, luteEngine.RenderOptions)
} }
} else { } else {
isProtyleDoc = true displayInText = true
title = path.Base(file.Path) 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) content = gulu.Str.FromBytes(data)
} else { } else {
if strings.Contains(file.Path, "assets/") { // 剔除笔记本级或者文档级资源文件路径前缀 if strings.Contains(file.Path, "assets/") { // 剔除笔记本级或者文档级资源文件路径前缀
@ -328,8 +331,8 @@ func parseTitleInSnapshot(fileID string, repo *dejavu.Repo, luteEngine *lute.Lut
return return
} }
func parseTreeInSnapshot(data []byte, luteEngine *lute.Lute) (isProtyleDoc bool, tree *parse.Tree, err error) { func parseTreeInSnapshot(data []byte, luteEngine *lute.Lute) (isLargeDoc bool, tree *parse.Tree, err error) {
isProtyleDoc = 1024*1024*1 <= len(data) isLargeDoc = 1024*1024*1 <= len(data)
tree, err = filesys.ParseJSONWithoutFix(data, luteEngine.ParseOptions) tree, err = filesys.ParseJSONWithoutFix(data, luteEngine.ParseOptions)
if err != nil { if err != nil {
return return

View file

@ -20,7 +20,6 @@ import (
"bytes" "bytes"
"io" "io"
"io/fs" "io/fs"
"mime"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -30,7 +29,6 @@ import (
"github.com/88250/gulu" "github.com/88250/gulu"
"github.com/88250/lute/ast" "github.com/88250/lute/ast"
"github.com/gabriel-vasile/mimetype"
"github.com/siyuan-note/filelock" "github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging" "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 { func IsSymlinkPath(absPath string) bool {
fi, err := os.Lstat(absPath) fi, err := os.Lstat(absPath)
if err != nil { if err != nil {