🎨 Search preview supports HTML tags in inline code content https://github.com/siyuan-note/siyuan/issues/11418

This commit is contained in:
Daniel 2024-05-17 11:20:24 +08:00
parent 67a4613d31
commit 4624b62e4d
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 14 additions and 2 deletions

View file

@ -35,6 +35,7 @@ import (
"github.com/88250/gulu"
"github.com/88250/lute"
"github.com/88250/lute/ast"
"github.com/88250/lute/html"
"github.com/88250/lute/lex"
"github.com/88250/lute/parse"
"github.com/88250/vitess-sqlparser/sqlparser"
@ -1538,8 +1539,8 @@ func stringQuery(query string) string {
// markReplaceSpan 用于处理搜索高亮。
func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, keywords []string, markSpanDataType string, luteEngine *lute.Lute) bool {
text := n.Content()
if ast.NodeText == n.Type {
text := n.Content()
escapedText := util.EscapeHTML(text)
escapedKeywords := make([]string, len(keywords))
for i, keyword := range keywords {
@ -1569,6 +1570,17 @@ func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, keywords []string, markS
return false
}
var text string
if n.IsTextMarkType("code") {
// code 在前面的 n.
for i, k := range keywords {
keywords[i] = html.EscapeString(k)
}
text = n.TextMarkTextContent
} else {
text = n.Content()
}
startTag := search.GetMarkSpanStart(markSpanDataType)
text = search.EncloseHighlighting(text, keywords, startTag, search.GetMarkSpanEnd(), Conf.Search.CaseSensitive, false)
if strings.Contains(text, search.MarkDataType) {

View file

@ -114,7 +114,7 @@ func EncloseHighlighting(text string, keywords []string, openMark, closeMark str
ret = text
if reg, err := regexp.Compile(re); nil == err {
ret = reg.ReplaceAllStringFunc(text, func(s string) string { return openMark + util.EscapeHTML(s) + closeMark })
ret = reg.ReplaceAllStringFunc(text, func(s string) string { return openMark + s + closeMark })
}
// 搜索结果预览包含转义符问题 Search results preview contains escape character issue https://github.com/siyuan-note/siyuan/issues/9790