mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
🎨 Improve assets search https://github.com/siyuan-note/siyuan/issues/13249
This commit is contained in:
parent
4335a464f6
commit
497b7829f1
1 changed files with 26 additions and 10 deletions
|
|
@ -249,7 +249,9 @@ 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, " ")
|
||||||
|
|
||||||
|
pathHitCount := map[string]int{}
|
||||||
count := 0
|
count := 0
|
||||||
filterByExt := 0 < len(exts)
|
filterByExt := 0 < len(exts)
|
||||||
for _, asset := range cache.GetAssets() {
|
for _, asset := range cache.GetAssets() {
|
||||||
|
|
@ -269,16 +271,24 @@ 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)
|
||||||
lowerKeyword := strings.ToLower(keyword)
|
var hitNameCount, hitPathCount int
|
||||||
hitName := strings.Contains(lowerHName, lowerKeyword)
|
for _, k := range keywords {
|
||||||
hitPath := strings.Contains(lowerPath, lowerKeyword)
|
lowerKeyword := strings.ToLower(k)
|
||||||
if !hitName && !hitPath {
|
hitNameCount += strings.Count(lowerHName, lowerKeyword)
|
||||||
|
hitPathCount += strings.Count(lowerPath, lowerKeyword)
|
||||||
|
if 1 > hitNameCount && 1 > hitPathCount {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if 1 > hitNameCount+hitPathCount {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pathHitCount[asset.Path] += hitNameCount + hitPathCount
|
||||||
|
|
||||||
hName := asset.HName
|
hName := asset.HName
|
||||||
if hitName {
|
if 0 < hitNameCount {
|
||||||
_, hName = search.MarkText(asset.HName, keyword, 64, Conf.Search.CaseSensitive)
|
_, hName = search.MarkText(asset.HName, strings.Join(keywords, search.TermSep), 64, Conf.Search.CaseSensitive)
|
||||||
}
|
}
|
||||||
ret = append(ret, &cache.Asset{
|
ret = append(ret, &cache.Asset{
|
||||||
HName: hName,
|
HName: hName,
|
||||||
|
|
@ -291,9 +301,15 @@ func SearchAssetsByName(keyword string, exts []string) (ret []*cache.Asset) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if 0 < len(pathHitCount) {
|
||||||
|
sort.Slice(ret, func(i, j int) bool {
|
||||||
|
return pathHitCount[ret[i].Path] > pathHitCount[ret[j].Path]
|
||||||
|
})
|
||||||
|
} else {
|
||||||
sort.Slice(ret, func(i, j int) bool {
|
sort.Slice(ret, func(i, j int) bool {
|
||||||
return ret[i].Updated > ret[j].Updated
|
return ret[i].Updated > ret[j].Updated
|
||||||
})
|
})
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue