An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:recycle: 搜索结果高亮支持大部分行级元素 https://github.com/siyuan-note/siyuan/issues/6745
Liang Ding9a0f2e7bc3a7
diff --git a/kernel/model/backlink.go b/kernel/model/backlink.goindex f2de389fc2..9cf0a2e95d 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 stfunc 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, excludeBacklinkcontinue}- 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.goindex 1b233061ff..520dea408d 100644--- a/kernel/model/file.go+++ b/kernel/model/file.go@@ -440,12 +440,18 @@ func StatTree(id string) (ret *util.BlockStatResult) {}const (- searchMarkSpanStart = "<span data-type=\"search-mark\">"- searchMarkSpanEnd = "</span>"- virtualBlockRefSpanStart = "<span data-type=\"virtual-block-ref\">"- virtualBlockRefSpanEnd = "</span>"+ searchMarkDataType = "search-mark"+ virtualBlockRefDataType = "virtual-block-ref")+func getMarkSpanStart(dataType string) string {+ return fmt.Sprintf("<span data-type=\"%s\">", dataType)+}++func getMarkSpanEnd() string {+ return "</span>"+}+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.goindex 15c1bd0608..19070ae9cc 100644--- a/kernel/model/history.go+++ b/kernel/model/history.go@@ -179,7 +179,7 @@ func GetDocHistoryContent(historyPath, keyword string) (id, rootID, content striif 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.goindex 3eec57acbf..0d5293bbb7 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)- }- for _, c := range children {- n.InsertBefore(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}- *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.goindex 155a31c981..a897bdd8e5 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/3185var blockKeys []string@@ -52,7 +52,7 @@ func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeywordblockKeys = 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