Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2024-11-01 09:40:35 +08:00
commit a6c70cbd46
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.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");

View file

@ -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,
}
}

View file

@ -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"))

View file

@ -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

View file

@ -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 {