🎨 Add more file formats supporting snapshots comparing https://github.com/siyuan-note/siyuan/issues/8438

This commit is contained in:
Daniel 2023-06-02 11:02:57 +08:00
parent 1090fb00b6
commit 5ba0397a80
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -81,8 +81,23 @@ func OpenRepoSnapshotDoc(fileID string) (id, rootID, content string, isLargeDoc
if nil != err { if nil != err {
return return
} }
file, err := repo.GetFile(fileID)
if nil != err {
return
}
data, err := repo.OpenFile(file)
if nil != err {
return
}
updated = file.Updated
if strings.HasSuffix(file.Path, ".sy") {
luteEngine := NewLute() luteEngine := NewLute()
isLargeDoc, snapshotTree, updated, err := parseTreeInSnapshot(fileID, repo, luteEngine) var snapshotTree *parse.Tree
isLargeDoc, snapshotTree, err = parseTreeInSnapshot(data, luteEngine)
if nil != err { if nil != err {
logging.LogErrorf("parse tree from snapshot file [%s] failed", fileID) logging.LogErrorf("parse tree from snapshot file [%s] failed", fileID)
return return
@ -127,6 +142,14 @@ func OpenRepoSnapshotDoc(fileID string) (id, rootID, content string, isLargeDoc
} else { } else {
content = luteEngine.Tree2BlockDOM(snapshotTree, luteEngine.RenderOptions) content = luteEngine.Tree2BlockDOM(snapshotTree, luteEngine.RenderOptions)
} }
} else {
isLargeDoc = true
if strings.HasSuffix(file.Path, ".json") {
content = gulu.Str.FromBytes(data)
} else {
content = file.Path
}
}
return return
} }
@ -250,10 +273,8 @@ func parseTitleInSnapshot(fileID string, repo *dejavu.Repo, luteEngine *lute.Lut
return return
} }
if !strings.HasSuffix(file.Path, ".sy") { title = path.Base(file.Path)
return if strings.HasSuffix(file.Path, ".sy") {
}
var data []byte var data []byte
data, err = repo.OpenFile(file) data, err = repo.OpenFile(file)
if nil != err { if nil != err {
@ -269,27 +290,16 @@ func parseTitleInSnapshot(fileID string, repo *dejavu.Repo, luteEngine *lute.Lut
} }
title = tree.Root.IALAttr("title") title = tree.Root.IALAttr("title")
}
return return
} }
func parseTreeInSnapshot(fileID string, repo *dejavu.Repo, luteEngine *lute.Lute) (isLargeDoc bool, tree *parse.Tree, updated int64, err error) { func parseTreeInSnapshot(data []byte, luteEngine *lute.Lute) (isLargeDoc bool, tree *parse.Tree, err error) {
file, err := repo.GetFile(fileID)
if nil != err {
return
}
data, err := repo.OpenFile(file)
if nil != err {
return
}
isLargeDoc = 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 nil != err { if nil != err {
return return
} }
updated = file.Updated
return return
} }