Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
commit
0643cbc4ff
5 changed files with 21 additions and 81 deletions
|
@ -215,7 +215,6 @@ func setSearch(c *gin.Context) {
|
|||
if s.CaseSensitive != oldCaseSensitive {
|
||||
model.FullReindex()
|
||||
}
|
||||
sql.ClearVirtualRefKeywords()
|
||||
ret.Data = s
|
||||
}
|
||||
|
||||
|
|
|
@ -672,52 +672,25 @@ func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, text string, keywords []
|
|||
|
||||
// markReplaceSpanWithSplit 用于处理虚拟引用和反链提及高亮。
|
||||
func markReplaceSpanWithSplit(text string, keywords []string, replacementStart, replacementEnd string) (ret string) {
|
||||
// 调用该函数前参数 keywords 必须使用 prepareMarkKeywords 函数进行预处理
|
||||
tmp := search.EncloseHighlighting(text, keywords, replacementStart, replacementEnd, Conf.Search.CaseSensitive)
|
||||
parts := strings.Split(tmp, replacementEnd)
|
||||
buf := bytes.Buffer{}
|
||||
for i := 0; i < len(parts); i++ {
|
||||
if i >= len(parts)-1 {
|
||||
buf.WriteString(parts[i])
|
||||
break
|
||||
}
|
||||
|
||||
parts := strings.Split(text, " ")
|
||||
for i, part := range parts {
|
||||
if "" == part {
|
||||
if nextPart := parts[i+1]; 0 < len(nextPart) && lex.IsASCIILetter(nextPart[0]) {
|
||||
// 取消已经高亮的部分
|
||||
part := strings.ReplaceAll(parts[i], replacementStart, "")
|
||||
buf.WriteString(part)
|
||||
continue
|
||||
}
|
||||
|
||||
var hitKeywords []string
|
||||
for _, k := range keywords {
|
||||
tmpPart := part
|
||||
tmpK := k
|
||||
if !Conf.Search.CaseSensitive {
|
||||
tmpPart = strings.ToLower(part)
|
||||
tmpK = strings.ToLower(k)
|
||||
}
|
||||
|
||||
if gulu.Str.IsASCII(tmpK) {
|
||||
if gulu.Str.IsASCII(tmpPart) {
|
||||
if tmpPart == tmpK {
|
||||
hitKeywords = append(hitKeywords, k)
|
||||
}
|
||||
} else {
|
||||
if strings.Contains(tmpPart, tmpK) {
|
||||
hitKeywords = append(hitKeywords, k)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if strings.Contains(tmpPart, tmpK) {
|
||||
hitKeywords = append(hitKeywords, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
if 0 < len(hitKeywords) {
|
||||
parts[i] = search.EncloseHighlighting(part, hitKeywords, replacementStart, replacementEnd, Conf.Search.CaseSensitive)
|
||||
}
|
||||
}
|
||||
|
||||
ret = strings.Join(parts, " ")
|
||||
if ret != text {
|
||||
return
|
||||
}
|
||||
|
||||
// 非 ASCII 文本并且不包含空格时再试试不分词匹配
|
||||
if !gulu.Str.IsASCII(text) && !strings.Contains(text, " ") {
|
||||
ret = search.EncloseHighlighting(text, keywords, replacementStart, replacementEnd, Conf.Search.CaseSensitive)
|
||||
buf.WriteString(parts[i])
|
||||
buf.WriteString(replacementEnd)
|
||||
}
|
||||
ret = buf.String()
|
||||
return
|
||||
}
|
||||
|
|
|
@ -109,20 +109,17 @@ func getVirtualRefKeywords(docName string) (ret []string) {
|
|||
// 虚拟引用排除当前文档名 https://github.com/siyuan-note/siyuan/issues/4537
|
||||
ret = gulu.Str.ExcludeElem(ret, []string{docName})
|
||||
ret = prepareMarkKeywords(ret)
|
||||
// 虚拟引用搜索关键字最多支持 `512` 个 https://github.com/siyuan-note/siyuan/issues/6603
|
||||
if 512 < len(ret) {
|
||||
ret = ret[:512]
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func prepareMarkKeywords(keywords []string) (ret []string) {
|
||||
keywords = gulu.Str.RemoveDuplicatedElem(keywords)
|
||||
for _, k := range keywords {
|
||||
if strings.ContainsAny(k, "?*!@#$%^&()[]{}\\|;:'\",.<>~`") {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, k)
|
||||
}
|
||||
|
||||
ret = gulu.Str.RemoveDuplicatedElem(keywords)
|
||||
sort.SliceStable(ret, func(i, j int) bool {
|
||||
return len(ret[i]) < len(ret[j])
|
||||
return len(ret[i]) > len(ret[j])
|
||||
})
|
||||
return
|
||||
}
|
||||
|
|
|
@ -28,11 +28,6 @@ import (
|
|||
)
|
||||
|
||||
func QueryVirtualRefKeywords(name, alias, anchor, doc bool) (ret []string) {
|
||||
ret, ok := getVirtualRefKeywordsCache()
|
||||
if ok {
|
||||
return ret
|
||||
}
|
||||
|
||||
if name {
|
||||
ret = append(ret, queryNames()...)
|
||||
}
|
||||
|
@ -49,7 +44,6 @@ func QueryVirtualRefKeywords(name, alias, anchor, doc bool) (ret []string) {
|
|||
sort.SliceStable(ret, func(i, j int) bool {
|
||||
return len(ret[i]) >= len(ret[j])
|
||||
})
|
||||
setVirtualRefKeywords(ret)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -70,29 +70,6 @@ func removeBlockCache(id string) {
|
|||
removeRefCacheByDefID(id)
|
||||
}
|
||||
|
||||
func getVirtualRefKeywordsCache() ([]string, bool) {
|
||||
if disabled {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if val, ok := memCache.Get("virtual_ref"); ok {
|
||||
return val.([]string), true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func setVirtualRefKeywords(keywords []string) {
|
||||
if disabled {
|
||||
return
|
||||
}
|
||||
|
||||
memCache.Set("virtual_ref", keywords, 1)
|
||||
}
|
||||
|
||||
func ClearVirtualRefKeywords() {
|
||||
memCache.Del("virtual_ref")
|
||||
}
|
||||
|
||||
var defIDRefsCache = gcache.New(30*time.Minute, 5*time.Minute) // [defBlockID]map[refBlockID]*Ref
|
||||
|
||||
func GetRefsCacheByDefID(defID string) (ret []*Ref) {
|
||||
|
|
Loading…
Add table
Reference in a new issue