Explorar el Código

:art: 改进 `/模板` 搜索排序 Fix https://github.com/siyuan-note/siyuan/issues/7027

Liang Ding hace 2 años
padre
commit
4629a3f72f
Se han modificado 1 ficheros con 49 adiciones y 16 borrados
  1. 49 16
      kernel/model/template.go

+ 49 - 16
kernel/model/template.go

@@ -61,33 +61,66 @@ func SearchTemplate(keyword string) (ret []*Block) {
 	ret = []*Block{}
 	ret = []*Block{}
 
 
 	templates := filepath.Join(util.DataDir, "templates")
 	templates := filepath.Join(util.DataDir, "templates")
+	groups, err := os.ReadDir(templates)
+	if nil != err {
+		logging.LogErrorf("read templates failed: %s", err)
+		return
+	}
+
+	sort.Slice(ret, func(i, j int) bool {
+		return util.PinYinCompare(filepath.Base(groups[i].Name()), filepath.Base(groups[j].Name()))
+	})
+
 	k := strings.ToLower(keyword)
 	k := strings.ToLower(keyword)
-	filepath.Walk(templates, func(path string, info fs.FileInfo, err error) error {
-		name := strings.ToLower(info.Name())
-		if !strings.HasSuffix(name, ".md") {
-			return nil
+	for _, group := range groups {
+		if strings.HasPrefix(group.Name(), ".") {
+			continue
 		}
 		}
 
 
-		if strings.HasPrefix(name, ".") || "readme.md" == name {
-			if info.IsDir() {
-				return filepath.SkipDir
+		if group.IsDir() {
+			var templateBlocks []*Block
+			templateDir := filepath.Join(templates, group.Name())
+			filepath.Walk(templateDir, func(path string, info fs.FileInfo, err error) error {
+				name := strings.ToLower(info.Name())
+				if strings.HasPrefix(name, ".") {
+					if info.IsDir() {
+						return filepath.SkipDir
+					}
+					return nil
+				}
+
+				if !strings.HasSuffix(name, ".md") || "readme.md" == name || !strings.Contains(name, k) {
+					return nil
+				}
+
+				content := strings.TrimPrefix(path, templates)
+				content = strings.TrimSuffix(content, ".md")
+				content = filepath.ToSlash(content)
+				content = content[1:]
+				_, content = search.MarkText(content, keyword, 32, Conf.Search.CaseSensitive)
+				b := &Block{Path: path, Content: content}
+				templateBlocks = append(templateBlocks, b)
+				return nil
+			})
+			sort.Slice(templateBlocks, func(i, j int) bool {
+				return util.PinYinCompare(filepath.Base(templateBlocks[i].Path), filepath.Base(templateBlocks[j].Path))
+			})
+			ret = append(ret, templateBlocks...)
+		} else {
+			name := strings.ToLower(group.Name())
+			if strings.HasPrefix(name, ".") || !strings.HasSuffix(name, ".md") || "readme.md" == name || !strings.Contains(name, k) {
+				continue
 			}
 			}
-			return nil
-		}
 
 
-		if strings.Contains(name, k) {
-			content := strings.TrimPrefix(path, templates)
-			content = strings.ReplaceAll(content, "templates"+string(os.PathSeparator), "")
+			content := group.Name()
 			content = strings.TrimSuffix(content, ".md")
 			content = strings.TrimSuffix(content, ".md")
 			content = filepath.ToSlash(content)
 			content = filepath.ToSlash(content)
 			content = content[1:]
 			content = content[1:]
 			_, content = search.MarkText(content, keyword, 32, Conf.Search.CaseSensitive)
 			_, content = search.MarkText(content, keyword, 32, Conf.Search.CaseSensitive)
-			b := &Block{Path: path, Content: content}
+			b := &Block{Path: filepath.Join(templates, group.Name()), Content: content}
 			ret = append(ret, b)
 			ret = append(ret, b)
 		}
 		}
-		return nil
-	})
-	sort.Slice(ret, func(i, j int) bool { return util.PinYinCompare(filepath.Base(ret[i].Path), filepath.Base(ret[j].Path)) })
+	}
 	return
 	return
 }
 }