Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:recycle: Extract method util.GetTreeID

Daniel6f7e106be49f
diff --git a/kernel/api/block_op.go b/kernel/api/block_op.go
index 2e8578e536..8e86a36b2d 100644
--- a/kernel/api/block_op.go
+++ b/kernel/api/block_op.go
@@ -19,8 +19,6 @@ package api
import (
"errors"
"net/http"
- "path"
- "strings"
"github.com/88250/gulu"
"github.com/88250/lute"
@@ -113,7 +111,7 @@ func appendDailyNoteBlock(c *gin.Context) {
return
}
- parentID := strings.TrimSuffix(path.Base(p), ".sy")
+ parentID := util.GetTreeID(p)
transactions := []*model.Transaction{
{
DoOperations: []*model.Operation{
@@ -166,7 +164,7 @@ func prependDailyNoteBlock(c *gin.Context) {
return
}
- parentID := strings.TrimSuffix(path.Base(p), ".sy")
+ parentID := util.GetTreeID(p)
transactions := []*model.Transaction{
{
DoOperations: []*model.Operation{
diff --git a/kernel/model/box.go b/kernel/model/box.go
index d54912ad33..2425af4e24 100644
--- a/kernel/model/box.go
+++ b/kernel/model/box.go
@@ -708,7 +708,7 @@ func getBoxesByPaths(paths []string) (ret map[string]*Box) {
ret = map[string]*Box{}
var ids []string
for _, p := range paths {
- ids = append(ids, strings.TrimSuffix(path.Base(p), ".sy"))
+ ids = append(ids, util.GetTreeID(p))
}
bts := treenode.GetBlockTrees(ids)
diff --git a/kernel/model/export.go b/kernel/model/export.go
index a223d517e7..7d36e85cd1 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -404,7 +404,7 @@ func ExportSY(id string) (name, zipPath string) {
docPaths = append(docPaths, docFile.path)
}
zipPath = exportSYZip(boxID, path.Dir(rootPath), baseFolderName, docPaths)
- name = strings.TrimSuffix(filepath.Base(block.Path), ".sy")
+ name = util.GetTreeID(block.Path)
return
}
@@ -1409,7 +1409,7 @@ func BatchExportPandocConvertZip(ids []string, pandocTo, ext string) (name, zipP
docPaths = util.FilterSelfChildDocs(docPaths)
zipPath = exportPandocConvertZip(false, box.ID, baseFolderName, docPaths, "gfm+footnotes+hard_line_breaks", pandocTo, ext)
- name = strings.TrimSuffix(filepath.Base(block.Path), ".sy")
+ name = util.GetTreeID(block.Path)
return
}
@@ -1433,7 +1433,7 @@ func ExportPandocConvertZip(id, pandocTo, ext string) (name, zipPath string) {
}
zipPath = exportPandocConvertZip(false, boxID, baseFolderName, docPaths, "gfm+footnotes+hard_line_breaks", pandocTo, ext)
- name = strings.TrimSuffix(filepath.Base(block.Path), ".sy")
+ name = util.GetTreeID(block.Path)
return
}
@@ -2627,7 +2627,7 @@ func resolveFootnotesDefs(refFootnotes *[]*refAsFootnotes, currentTree *parse.Tr
continue
}
defNode := treenode.GetNodeInTree(t, foot.defID)
- docID := strings.TrimSuffix(path.Base(defNode.Path), ".sy")
+ docID := util.GetTreeID(defNode.Path)
var nodes []*ast.Node
if ast.NodeHeading == defNode.Type {
nodes = append(nodes, defNode)
@@ -2721,7 +2721,7 @@ func resolveFootnotesDefs(refFootnotes *[]*refAsFootnotes, currentTree *parse.Tr
return ast.WalkContinue
}
- docID := strings.TrimSuffix(path.Base(n.Path), ".sy")
+ docID := util.GetTreeID(n.Path)
if currentTree.ID == docID {
// 同文档块引转脚注缩略定义 https://github.com/siyuan-note/siyuan/issues/3299
if text := sql.GetRefText(n.ID); 64 < utf8.RuneCountInString(text) {
diff --git a/kernel/model/file.go b/kernel/model/file.go
index a2b6d0cb6d..e2ad3c25b2 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -310,7 +310,7 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo
}
if flashcard {
- rootID := strings.TrimSuffix(filepath.Base(parentDocPath), ".sy")
+ rootID := util.GetTreeID(parentDocPath)
newFlashcardCount, dueFlashcardCount, flashcardCount := countTreeFlashcard(rootID, deck, deckBlockIDs)
if 0 < flashcardCount {
doc.NewFlashcardCount = newFlashcardCount
@@ -339,7 +339,7 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo
doc := box.docFromFileInfo(file, ial)
if flashcard {
- rootID := strings.TrimSuffix(filepath.Base(file.path), ".sy")
+ rootID := util.GetTreeID(file.path)
newFlashcardCount, dueFlashcardCount, flashcardCount := countTreeFlashcard(rootID, deck, deckBlockIDs)
if 0 < flashcardCount {
doc.NewFlashcardCount = newFlashcardCount
@@ -1833,7 +1833,7 @@ func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) {
}
baseName := strings.TrimSpace(path.Base(p))
- if "" == strings.TrimSuffix(baseName, ".sy") {
+ if "" == util.GetTreeID(baseName) {
err = errors.New(Conf.Language(16))
return
}
@@ -1849,7 +1849,7 @@ func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) {
return
}
- id := strings.TrimSuffix(path.Base(p), ".sy")
+ id := util.GetTreeID(p)
var hPath string
folder := path.Dir(p)
if "/" != folder {
@@ -2006,7 +2006,7 @@ func ChangeFileTreeSort(boxID string, paths []string) {
sortIDs := map[string]int{}
max := 0
for i, p := range paths {
- id := strings.TrimSuffix(path.Base(p), ".sy")
+ id := util.GetTreeID(p)
sortIDs[id] = i + 1
if i == len(paths)-1 {
max = i + 2
diff --git a/kernel/model/heading.go b/kernel/model/heading.go
index e256adb0b7..5c4f8cdda1 100644
--- a/kernel/model/heading.go
+++ b/kernel/model/heading.go
@@ -394,7 +394,7 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath, previousPath string) (sr
newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
newTree.Root.Spec = "1"
if "" != previousPath {
- box.addSort(strings.TrimSuffix(path.Base(previousPath), ".sy"), newTree.ID)
+ box.addSort(util.GetTreeID(previousPath), newTree.ID)
} else {
box.addMinSort(path.Dir(newTargetPath), newTree.ID)
}
diff --git a/kernel/model/history.go b/kernel/model/history.go
index 7004986545..a49c1cc05b 100644
--- a/kernel/model/history.go
+++ b/kernel/model/history.go
@@ -22,6 +22,7 @@ import (
"io/fs"
"math"
"os"
+ "path"
"path/filepath"
"sort"
"strconv"
@@ -229,9 +230,7 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
srcPath := historyPath
var destPath, parentHPath string
- baseName := filepath.Base(historyPath)
- id := strings.TrimSuffix(baseName, ".sy")
-
+ id := util.GetTreeID(historyPath)
workingDoc := treenode.GetBlockTree(id)
if nil != workingDoc {
if err = filelock.Remove(filepath.Join(util.DataDir, boxID, workingDoc.Path)); err != nil {
@@ -334,7 +333,7 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
func getRollbackDockPath(boxID, historyPath string) (destPath, parentHPath string, err error) {
baseName := filepath.Base(historyPath)
- parentID := strings.TrimSuffix(filepath.Base(filepath.Dir(historyPath)), ".sy")
+ parentID := path.Base(filepath.Dir(historyPath))
parentWorkingDoc := treenode.GetBlockTree(parentID)
if nil != parentWorkingDoc {
// 父路径如果是文档,则恢复到父路径下
diff --git a/kernel/model/import.go b/kernel/model/import.go
index d3215cfff5..47edb77573 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -758,7 +758,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
targetPaths[curRelPath] = targetPath
} else {
targetPath = targetPaths[curRelPath]
- id = strings.TrimSuffix(path.Base(targetPath), ".sy")
+ id = util.GetTreeID(targetPath)
}
if d.IsDir() {
diff --git a/kernel/model/listitem.go b/kernel/model/listitem.go
index ebf54cbc7e..7cc87f34cd 100644
--- a/kernel/model/listitem.go
+++ b/kernel/model/listitem.go
@@ -128,7 +128,7 @@ func ListItem2Doc(srcListItemID, targetBoxID, targetPath, previousPath string) (
newTree.Root.SetIALAttr("updated", util.CurrentTimeSecondsStr())
newTree.Root.Spec = "1"
if "" != previousPath {
- box.addSort(strings.TrimSuffix(path.Base(previousPath), ".sy"), newTree.ID)
+ box.addSort(util.GetTreeID(previousPath), newTree.ID)
} else {
box.addMinSort(path.Dir(newTargetPath), newTree.ID)
}
diff --git a/kernel/model/sync.go b/kernel/model/sync.go
index 625ddebe11..e9892ab6e0 100644
--- a/kernel/model/sync.go
+++ b/kernel/model/sync.go
@@ -21,7 +21,6 @@ import (
"fmt"
"net/http"
"os"
- "path"
"path/filepath"
"runtime"
"strings"
@@ -293,7 +292,7 @@ func removeIndexes(removeFilePaths []string) (removeRootIDs []string) {
continue
}
- id := strings.TrimSuffix(filepath.Base(removeFile), ".sy")
+ id := util.GetTreeID(removeFile)
removeRootIDs = append(removeRootIDs, id)
block := treenode.GetBlockTree(id)
if nil != block {
@@ -338,7 +337,7 @@ func upsertIndexes(upsertFilePaths []string) (upsertRootIDs []string) {
box := upsertFile[:idx]
p := strings.TrimPrefix(upsertFile, box)
- msg := fmt.Sprintf(Conf.Language(40), strings.TrimSuffix(path.Base(p), ".sy"))
+ msg := fmt.Sprintf(Conf.Language(40), util.GetTreeID(p))
util.IncBootProgress(bootProgressPart, msg)
util.PushStatusBar(msg)
diff --git a/kernel/treenode/tree.go b/kernel/treenode/tree.go
index b6f0ccd536..1275cc8c6e 100644
--- a/kernel/treenode/tree.go
+++ b/kernel/treenode/tree.go
@@ -20,7 +20,6 @@ import (
"crypto/sha256"
"fmt"
"io/fs"
- "path"
"path/filepath"
"sort"
"strings"
@@ -61,7 +60,7 @@ func TreeRoot(node *ast.Node) *ast.Node {
}
func NewTree(boxID, p, hp, title string) *parse.Tree {
- id := strings.TrimSuffix(path.Base(p), ".sy")
+ id := util.GetTreeID(p)
root := &ast.Node{Type: ast.NodeDocument, ID: id, Spec: "1", Box: boxID, Path: p}
root.SetIALAttr("title", title)
root.SetIALAttr("id", id)
diff --git a/kernel/util/path.go b/kernel/util/path.go
index 1c7e9602b2..b8cf52d5e4 100644
--- a/kernel/util/path.go
+++ b/kernel/util/path.go
@@ -37,6 +37,10 @@ var (
UserAgent = "SiYuan/" + Ver
)
+func GetTreeID(treePath string) string {
+ return strings.TrimSuffix(path.Base(treePath), ".sy")
+}
+
func ShortPathForBootingDisplay(p string) string {
if 25 > len(p) {
return p