mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-09-22 08:30:42 +02:00
🎨 Improve assets searching https://github.com/siyuan-note/siyuan/issues/15251
This commit is contained in:
parent
03a10b7c88
commit
ed6a90ab84
1 changed files with 18 additions and 8 deletions
|
@ -272,10 +272,10 @@ func NetAssets2LocalAssets(rootID string, onlyImg bool, originalURL string) (err
|
|||
|
||||
func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
|
||||
ret = []*cache.Asset{}
|
||||
keywords := strings.Split(keyword, " ")
|
||||
|
||||
var keywords []string
|
||||
keywords = append(keywords, keyword)
|
||||
keywords = append(keywords, strings.Split(keyword, " ")...)
|
||||
pathHitCount := map[string]int{}
|
||||
count := 0
|
||||
filterByExt := 0 < len(exts)
|
||||
for _, asset := range cache.GetAssets() {
|
||||
if filterByExt {
|
||||
|
@ -295,8 +295,18 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
|
|||
lowerHName := strings.ToLower(asset.HName)
|
||||
lowerPath := strings.ToLower(asset.Path)
|
||||
var hitNameCount, hitPathCount int
|
||||
for _, k := range keywords {
|
||||
for i, k := range keywords {
|
||||
lowerKeyword := strings.ToLower(k)
|
||||
if 0 == i {
|
||||
// 第一个是完全匹配,权重最高
|
||||
if strings.Contains(lowerHName, lowerKeyword) {
|
||||
hitNameCount += 64
|
||||
}
|
||||
if strings.Contains(lowerPath, lowerKeyword) {
|
||||
hitPathCount += 64
|
||||
}
|
||||
}
|
||||
|
||||
hitNameCount += strings.Count(lowerHName, lowerKeyword)
|
||||
hitPathCount += strings.Count(lowerPath, lowerKeyword)
|
||||
if 1 > hitNameCount && 1 > hitPathCount {
|
||||
|
@ -318,10 +328,6 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
|
|||
Path: asset.Path,
|
||||
Updated: asset.Updated,
|
||||
})
|
||||
count++
|
||||
if Conf.Search.Limit <= count {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if 0 < len(pathHitCount) {
|
||||
|
@ -333,6 +339,10 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
|
|||
return ret[i].Updated > ret[j].Updated
|
||||
})
|
||||
}
|
||||
|
||||
if Conf.Search.Limit <= len(ret) {
|
||||
ret = ret[:Conf.Search.Limit]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue