mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01: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) {
|
func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
|
||||||
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{}
|
pathHitCount := map[string]int{}
|
||||||
count := 0
|
|
||||||
filterByExt := 0 < len(exts)
|
filterByExt := 0 < len(exts)
|
||||||
for _, asset := range cache.GetAssets() {
|
for _, asset := range cache.GetAssets() {
|
||||||
if filterByExt {
|
if filterByExt {
|
||||||
|
|
@ -295,8 +295,18 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
|
||||||
lowerHName := strings.ToLower(asset.HName)
|
lowerHName := strings.ToLower(asset.HName)
|
||||||
lowerPath := strings.ToLower(asset.Path)
|
lowerPath := strings.ToLower(asset.Path)
|
||||||
var hitNameCount, hitPathCount int
|
var hitNameCount, hitPathCount int
|
||||||
for _, k := range keywords {
|
for i, k := range keywords {
|
||||||
lowerKeyword := strings.ToLower(k)
|
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)
|
hitNameCount += strings.Count(lowerHName, lowerKeyword)
|
||||||
hitPathCount += strings.Count(lowerPath, lowerKeyword)
|
hitPathCount += strings.Count(lowerPath, lowerKeyword)
|
||||||
if 1 > hitNameCount && 1 > hitPathCount {
|
if 1 > hitNameCount && 1 > hitPathCount {
|
||||||
|
|
@ -318,10 +328,6 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
|
||||||
Path: asset.Path,
|
Path: asset.Path,
|
||||||
Updated: asset.Updated,
|
Updated: asset.Updated,
|
||||||
})
|
})
|
||||||
count++
|
|
||||||
if Conf.Search.Limit <= count {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if 0 < len(pathHitCount) {
|
if 0 < len(pathHitCount) {
|
||||||
|
|
@ -333,6 +339,10 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
|
||||||
return ret[i].Updated > ret[j].Updated
|
return ret[i].Updated > ret[j].Updated
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if Conf.Search.Limit <= len(ret) {
|
||||||
|
ret = ret[:Conf.Search.Limit]
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue