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

This commit is contained in:
Daniel 2024-12-13 19:58:13 +08:00
parent 45bd7be977
commit 1813d869b3
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 69 additions and 4 deletions

View file

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

View file

@ -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)
}
}
ChangeFileTreeSort(boxID, paths)
sortIDVals := map[string]int{}
for _, ps := range paths {
sortVal := 0
for _, p := range ps {
sortIDVals[util.GetTreeID(p)] = sortVal
sortVal++
}
}
box.setSort(sortIDVals)
}
IncSync()