Skip to content

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

:art: Improve sort when importing markdown files https://github.com/siyuan-note/siyuan/issues/13449

Daniel1813d869b3dd
diff --git a/kernel/model/file.go b/kernel/model/file.go
index d5b352348e..8124c8fdc1 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -2068,3 +2068,36 @@ func (box *Box) addSort(previousPath, id string) {
return
}
}
+
+func (box *Box) setSort(sortIDVals map[string]int) {
+ confPath := filepath.Join(util.DataDir, box.ID, ".siyuan", "sort.json")
+ if !filelock.IsExist(confPath) {
+ return
+ }
+
+ data, err := filelock.ReadFile(confPath)
+ if err != nil {
+ logging.LogErrorf("read sort conf failed: %s", err)
+ return
+ }
+
+ fullSortIDs := map[string]int{}
+ if err = gulu.JSON.UnmarshalJSON(data, &fullSortIDs); err != nil {
+ logging.LogErrorf("unmarshal sort conf failed: %s", err)
+ return
+ }
+
+ for sortID := range sortIDVals {
+ fullSortIDs[sortID] = sortIDVals[sortID]
+ }
+
+ data, err = gulu.JSON.MarshalJSON(fullSortIDs)
+ if err != nil {
+ logging.LogErrorf("marshal sort conf failed: %s", err)
+ return
+ }
+ if err = filelock.WriteFile(confPath, data); err != nil {
+ logging.LogErrorf("write sort conf failed: %s", err)
+ return
+ }
+}
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 7a587d5cee..d9ff764cc9 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -726,7 +726,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
targetPaths := map[string]string{}
assetsDone := map[string]string{}
-
+ count := 0
// md 转换 sy
filelock.Walk(localPath, func(currentPath string, d fs.DirEntry, err error) error {
if strings.HasPrefix(d.Name(), ".") {
@@ -897,6 +897,11 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
hPathsIDs[tree.HPath] = tree.ID
idPaths[tree.ID] = tree.Path
+
+ count++
+ if 0 == count%4 {
+ util.PushEndlessProgress(fmt.Sprintf(Conf.language(70), fmt.Sprintf("%s", tree.HPath)))
+ }
return nil
})
} else { // 导入单个文件
@@ -1018,6 +1023,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
convertWikiLinksAndTags()
buildBlockRefInText()
+ box := Conf.Box(boxID)
for i, tree := range importTrees {
indexWriteTreeIndexQueue(tree)
if 0 == i%4 {
@@ -1030,15 +1036,41 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
searchLinks = map[string]string{}
// 按照路径排序 Improve sort when importing markdown files https://github.com/siyuan-note/siyuan/issues/11390
- var paths, hPaths []string
+ var hPaths []string
for hPath := range hPathsIDs {
hPaths = append(hPaths, hPath)
}
sort.Strings(hPaths)
+ paths := map[string][]string{}
for _, hPath := range hPaths {
- paths = append(paths, idPaths[hPathsIDs[hPath]])
+ p := idPaths[hPathsIDs[hPath]]
+ parent := path.Dir(p)
+ for {
+ if baseTargetPath == parent {
+ break
+ }
+
+ if ps, ok := paths[parent]; !ok {
+ paths[parent] = []string{p}
+ } else {
+ ps = append(ps, p)
+ ps = gulu.Str.RemoveDuplicatedElem(ps)
+ paths[parent] = ps
+ }
+ p = parent
+ parent = path.Dir(parent)
+ }
+ }
+
+ sortIDVals := map[string]int{}
+ for _, ps := range paths {
+ sortVal := 0
+ for _, p := range ps {
+ sortIDVals[util.GetTreeID(p)] = sortVal
+ sortVal++
+ }
}
- ChangeFileTreeSort(boxID, paths)
+ box.setSort(sortIDVals)
}
IncSync()