diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go index f2de389fc..9cf0a2e95 100644 --- a/kernel/model/backlink.go +++ b/kernel/model/backlink.go @@ -204,7 +204,7 @@ func buildBacklink(refID string, refTree *parse.Tree, mentionKeywords []string, } if ast.NodeText == n.Type { text := string(n.Tokens) - newText := markReplaceSpanWithSplit(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd) + newText := markReplaceSpanWithSplit(text, mentionKeywords, getMarkSpanStart(searchMarkDataType), getMarkSpanEnd()) if text == newText { return ast.WalkContinue } @@ -627,7 +627,6 @@ func buildTreeBackmention(defSQLBlock *sql.Block, refBlocks []*Block, keyword st func searchBackmention(mentionKeywords []string, keyword string, excludeBacklinkIDs *hashset.Set, rootID string, beforeLen int) (ret []*Block) { ret = []*Block{} - if 1 > len(mentionKeywords) { return } @@ -696,7 +695,7 @@ func searchBackmention(mentionKeywords []string, keyword string, excludeBacklink continue } - newText := markReplaceSpanWithSplit(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd) + newText := markReplaceSpanWithSplit(text, mentionKeywords, getMarkSpanStart(searchMarkDataType), getMarkSpanEnd()) if text != newText { tmp = append(tmp, b) } diff --git a/kernel/model/file.go b/kernel/model/file.go index 1b233061f..520dea408 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -440,12 +440,18 @@ func StatTree(id string) (ret *util.BlockStatResult) { } const ( - searchMarkSpanStart = "" - searchMarkSpanEnd = "" - virtualBlockRefSpanStart = "" - virtualBlockRefSpanEnd = "" + searchMarkDataType = "search-mark" + virtualBlockRefDataType = "virtual-block-ref" ) +func getMarkSpanStart(dataType string) string { + return fmt.Sprintf("", dataType) +} + +func getMarkSpanEnd() string { + return "" +} + func GetDoc(startID, endID, id string, index int, keyword string, mode int, size int) (blockCount, childBlockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, err error) { WaitForWritingFiles() // 写入数据时阻塞,避免获取到的数据不一致 @@ -631,7 +637,7 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size } } if hitBlock { - if markReplaceSpan(n, &unlinks, string(n.Tokens), keywords, searchMarkSpanStart, searchMarkSpanEnd, luteEngine) { + if markReplaceSpan(n, &unlinks, keywords, searchMarkDataType, luteEngine) { return ast.WalkContinue } } diff --git a/kernel/model/history.go b/kernel/model/history.go index 15c1bd060..19070ae9c 100644 --- a/kernel/model/history.go +++ b/kernel/model/history.go @@ -179,7 +179,7 @@ func GetDocHistoryContent(historyPath, keyword string) (id, rootID, content stri if ast.NodeText == n.Type { if 0 < len(keywords) { - if markReplaceSpan(n, &unlinks, string(n.Tokens), keywords, searchMarkSpanStart, searchMarkSpanEnd, luteEngine) { + if markReplaceSpan(n, &unlinks, keywords, searchMarkDataType, luteEngine) { return ast.WalkContinue } } diff --git a/kernel/model/search.go b/kernel/model/search.go index 3eec57acb..0d5293bbb 100644 --- a/kernel/model/search.go +++ b/kernel/model/search.go @@ -830,21 +830,26 @@ func stringQuery(query string) string { } // markReplaceSpan 用于处理搜索高亮。 -func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, text string, keywords []string, replacementStart, replacementEnd string, luteEngine *lute.Lute) bool { - text = search.EncloseHighlighting(text, keywords, searchMarkSpanStart, searchMarkSpanEnd, Conf.Search.CaseSensitive) - n.Tokens = gulu.Str.ToBytes(text) - if bytes.Contains(n.Tokens, []byte("search-mark")) { - n.Tokens = lex.EscapeMarkers(n.Tokens) - linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions) - var children []*ast.Node - for c := linkTree.Root.FirstChild.FirstChild; nil != c; c = c.Next { - children = append(children, c) +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 = search.EncloseHighlighting(text, keywords, getMarkSpanStart(markSpanDataType), getMarkSpanEnd(), Conf.Search.CaseSensitive) + n.Tokens = gulu.Str.ToBytes(text) + if bytes.Contains(n.Tokens, []byte("search-mark")) { + n.Tokens = lex.EscapeMarkers(n.Tokens) + linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions) + var children []*ast.Node + for c := linkTree.Root.FirstChild.FirstChild; nil != c; c = c.Next { + children = append(children, c) + } + for _, c := range children { + n.InsertBefore(c) + } + *unlinks = append(*unlinks, n) + return true } - for _, c := range children { - n.InsertBefore(c) - } - *unlinks = append(*unlinks, n) - return true + } else if ast.NodeTextMark == n.Type { + // 搜索结果高亮支持大部分行级元素 https://github.com/siyuan-note/siyuan/issues/6745 } return false } diff --git a/kernel/model/virutalref.go b/kernel/model/virutalref.go index 155a31c98..a897bdd8e 100644 --- a/kernel/model/virutalref.go +++ b/kernel/model/virutalref.go @@ -41,7 +41,7 @@ func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeyword } content := string(n.Tokens) - newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, virtualBlockRefSpanStart, virtualBlockRefSpanEnd) + newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, getMarkSpanStart(virtualBlockRefDataType), getMarkSpanEnd()) if content != newContent { // 虚拟引用排除命中自身块命名和别名的情况 https://github.com/siyuan-note/siyuan/issues/3185 var blockKeys []string @@ -52,7 +52,7 @@ func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeyword blockKeys = append(blockKeys, alias) } if 0 < len(blockKeys) { - keys := gulu.Str.SubstringsBetween(newContent, virtualBlockRefSpanStart, virtualBlockRefSpanEnd) + keys := gulu.Str.SubstringsBetween(newContent, getMarkSpanStart(virtualBlockRefDataType), getMarkSpanEnd()) for _, k := range keys { if gulu.Str.Contains(k, blockKeys) { return true