diff --git a/kernel/model/box.go b/kernel/model/box.go index 0d89c81cf..4c2c3c272 100644 --- a/kernel/model/box.go +++ b/kernel/model/box.go @@ -148,11 +148,11 @@ func ListNotebooks() (ret []*Box, err error) { switch Conf.FileTree.Sort { case util.SortModeNameASC: sort.Slice(ret, func(i, j int) bool { - return util.PinYinCompare(ret[i].Name, ret[j].Name) + return util.PinYinCompare4FileTree(ret[i].Name, ret[j].Name) }) case util.SortModeNameDESC: sort.Slice(ret, func(i, j int) bool { - return util.PinYinCompare(ret[j].Name, ret[i].Name) + return util.PinYinCompare4FileTree(ret[j].Name, ret[i].Name) }) case util.SortModeAlphanumASC: sort.Slice(ret, func(i, j int) bool { diff --git a/kernel/model/file.go b/kernel/model/file.go index 60b568695..bd3c51ddf 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -372,11 +372,11 @@ func ListDocTree(boxID, listPath string, sortMode int, flashcard, showHidden boo switch sortMode { case util.SortModeNameASC: sort.Slice(docs, func(i, j int) bool { - return util.PinYinCompare(docs[i].Name, docs[j].Name) + return util.PinYinCompare4FileTree(docs[i].Name, docs[j].Name) }) case util.SortModeNameDESC: sort.Slice(docs, func(i, j int) bool { - return util.PinYinCompare(docs[j].Name, docs[i].Name) + return util.PinYinCompare4FileTree(docs[j].Name, docs[i].Name) }) case util.SortModeUpdatedASC: sort.Slice(docs, func(i, j int) bool { return docs[i].Mtime < docs[j].Mtime }) diff --git a/kernel/util/rhy.go b/kernel/util/rhy.go index 7997fa677..bbb37ac92 100644 --- a/kernel/util/rhy.go +++ b/kernel/util/rhy.go @@ -60,5 +60,12 @@ func GetRhyResult(force bool) (map[string]interface{}, error) { } func RefreshRhyResultJob() { - GetRhyResult(true) + _, err := GetRhyResult(true) + if nil != err { + // 系统唤醒后可能还没有网络连接,这里等待后再重试 + go func() { + time.Sleep(7 * time.Second) + GetRhyResult(true) + }() + } } diff --git a/kernel/util/sort.go b/kernel/util/sort.go index 2a6afe0f3..3e0dc6c8e 100644 --- a/kernel/util/sort.go +++ b/kernel/util/sort.go @@ -64,6 +64,40 @@ func PinYinCompare(str1, str2 string) bool { return true } +func PinYinCompare4FileTree(str1, str2 string) bool { + // 文档树字母排序不复用 PinYinCompare 而是单独实现 + // Improve doc tree Name Alphabet sorting https://github.com/siyuan-note/siyuan/issues/14773 + + str1 = RemoveEmojiInvisible(str1) + str1 = strings.TrimSuffix(str1, ".sy") + str2 = RemoveEmojiInvisible(str2) + str2 = strings.TrimSuffix(str2, ".sy") + + // Doc tree, backlinks, tags and templates ignores case when sorting alphabetically by name https://github.com/siyuan-note/siyuan/issues/8360 + str1 = strings.ToLower(str1) + str2 = strings.ToLower(str2) + + a, _ := UTF82GBK(str1) + b, _ := UTF82GBK(str2) + + // 长度相等的情况下,直接比较字节数组 + if len(a) == len(b) { + return bytes.Compare(a, b) < 0 + } + + // 长度不相等的情况下,比较前面相等的部分 + if len(a) < len(b) { + if 0 == bytes.Compare(a, b[:len(a)]) { // 前面相等的情况下,比较长度 + return len(a) > len(b[:len(a)]) + } + return bytes.Compare(a, b[:len(a)]) < 0 + } + if 0 == bytes.Compare(a[:len(b)], b) { + return len(a[:len(b)]) > len(b) + } + return bytes.Compare(a[:len(b)], b) < 0 +} + // UTF82GBK transform UTF8 rune into GBK byte array. func UTF82GBK(src string) ([]byte, error) { GB18030 := simplifiedchinese.All[0]