An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:art: Reduce redundant search breadcrumb highlighting https://github.com/siyuan-note/siyuan/issues/19145
Daniel759c9f8fb1bd
diff --git a/app/src/layout/dock/Backlink.ts b/app/src/layout/dock/Backlink.tsindex d5fcf636ea..2e146fbaa7 100644--- a/app/src/layout/dock/Backlink.ts+++ b/app/src/layout/dock/Backlink.ts@@ -1029,7 +1029,9 @@ export class Backlink extends Model {}});});- searchMarkRender(editor.protyle, response.data.keywords);+ searchMarkRender(editor.protyle, response.data.keywords, undefined, undefined, {+ excludeSelector: ".protyle-breadcrumb__bar[data-backlink-id]"+ });} else {const editorElement = document.createElement("div");editorElement.style.minHeight = "auto";@@ -1059,7 +1061,9 @@ export class Backlink extends Model {} else {editor.protyle.element.removeAttribute("data-notebook-id");}- searchMarkRender(editor.protyle, response.data.keywords);+ searchMarkRender(editor.protyle, response.data.keywords, undefined, undefined, {+ excludeSelector: ".protyle-breadcrumb__bar[data-backlink-id]"+ });this.editors.push(editor);record.editor = editor;const service = this.viewState;diff --git a/app/src/protyle/render/searchMarkRender.ts b/app/src/protyle/render/searchMarkRender.tsindex c4a776a780..09899532b5 100644--- a/app/src/protyle/render/searchMarkRender.ts+++ b/app/src/protyle/render/searchMarkRender.ts@@ -5,6 +5,7 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], hlId?: strinoptions?: {rootElement?: HTMLElement,currentElement?: Element,+ excludeSelector?: string,}) => {if (!isSupportCSSHL() || ((!keys || keys.length === 0) && !hlId)) {return;@@ -32,7 +33,9 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], hlId?: strin// 准备一个数组来保存所有文本节点const textNodes: Node[] = [];const textNodesSize: number[] = [];+ const excludedTextNodeCounts: number[] = [];let currentSize = 0;+ let excludedTextNodeCount = 0;const rootElement = options?.rootElement || protyle.contentElement;const treeWalker = document.createTreeWalker(rootElement, NodeFilter.SHOW_TEXT);@@ -41,6 +44,10 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], hlId?: strintextNodes.push(currentNode);currentSize += currentNode.textContent.length;textNodesSize.push(currentSize);+ if (options?.excludeSelector && currentNode.parentElement?.closest(options.excludeSelector)) {+ excludedTextNodeCount++;+ }+ excludedTextNodeCounts.push(excludedTextNodeCount);currentNode = treeWalker.nextNode();}@@ -61,13 +68,20 @@ export const searchMarkRender = (protyle: IProtyle, keys: string[], hlId?: strinwhile (currentNodeIndex < textNodes.length && textNodesSize[currentNodeIndex] <= startIndex) {currentNodeIndex++;}+ const startNodeIndex = currentNodeIndex;let currentTextNode = textNodes[currentNodeIndex];- range.setStart(currentTextNode, startIndex - (currentNodeIndex ? textNodesSize[currentNodeIndex - 1] : 0));while (currentNodeIndex < textNodes.length && textNodesSize[currentNodeIndex] < endIndex) {currentNodeIndex++;}currentTextNode = textNodes[currentNodeIndex];+ const excludedCountBefore = startNodeIndex ? excludedTextNodeCounts[startNodeIndex - 1] : 0;+ if (excludedTextNodeCounts[currentNodeIndex] > excludedCountBefore) {+ startIndex = endIndex;+ continue;+ }+ range.setStart(textNodes[startNodeIndex], startIndex -+ (startNodeIndex ? textNodesSize[startNodeIndex - 1] : 0));range.setEnd(currentTextNode, endIndex - (currentNodeIndex ? textNodesSize[currentNodeIndex - 1] : 0));let isCurrent = false;diff --git a/kernel/model/backlink.go b/kernel/model/backlink.goindex ac388379d4..f116f5daad 100644--- a/kernel/model/backlink.go+++ b/kernel/model/backlink.go@@ -458,6 +458,13 @@ func buildBacklink(refID string, refTree *parse.Tree, originalRefBlockIDs map[st}renderNodes, expand := getBacklinkRenderNodes(node, originalRefBlockIDs)+ var blockPaths []*BlockPath+ if (nil != node.Parent && ast.NodeDocument != node.Parent.Type) || (ast.NodeHeading != node.Type && 0 < treenode.HeadingLevel(node)) {+ blockPaths = buildBlockBreadcrumb(node, nil, false)+ }+ if 1 > len(blockPaths) {+ blockPaths = []*BlockPath{}+ }if highlight && 0 < len(keywords) {for _, renderNode := range renderNodes {@@ -486,13 +493,6 @@ func buildBacklink(refID string, refTree *parse.Tree, originalRefBlockIDs map[stfillBlockRefCount(renderNodes, refTree.Box)dom := renderVisibleBlockDOMByNodes(renderNodes, luteEngine)- var blockPaths []*BlockPath- if (nil != node.Parent && ast.NodeDocument != node.Parent.Type) || (ast.NodeHeading != node.Type && 0 < treenode.HeadingLevel(node)) {- blockPaths = buildBlockBreadcrumb(node, nil, false)- }- if 1 > len(blockPaths) {- blockPaths = []*BlockPath{}- }ret = &Backlink{ID: refID, DOM: dom, BlockPaths: blockPaths, Expand: expand, node: node}return}diff --git a/kernel/model/search.go b/kernel/model/search.goindex a4cc3d0383..d73b1a5dde 100644--- a/kernel/model/search.go+++ b/kernel/model/search.go@@ -2573,11 +2573,18 @@ func loadFTSAndHPathMatchBlocks(matches []ftsAndHPathMatch, rawQuery, query striif nil == sqlBlock {continue}- ret = append(ret, fromHPathSearchSQLBlock(sqlBlock, rawQuery, beforeLen))+ ret = append(ret, fromFTSAndHPathMatchSQLBlock(sqlBlock, match.source, rawQuery, beforeLen))}return}+func fromFTSAndHPathMatchSQLBlock(sqlBlock *sql.Block, source int, rawQuery string, beforeLen int) *Block {+ if 0 == source {+ return fromSQLBlockWithHPathTerms(sqlBlock, "", "", beforeLen)+ }+ return fromHPathSearchSQLBlock(sqlBlock, rawQuery, beforeLen)+}+func queryFTSSnippetBlocksByRowID(query string, rowIDs []int64, boxID string) (ret map[int64]*sql.Block, err error) {ret = map[int64]*sql.Block{}if 1 > len(rowIDs) {@@ -2788,10 +2795,10 @@ func fullTextSearchByLikeWithRootInBox(query, boxFilter, pathFilter string, boxAfor i, resultBlock := range resultBlocks {if 1 == result[i]["matchSource"].(int64) {docContent, _ := result[i]["docContent"].(string)- contentTerms := matchedSearchTerms(docContent, terms)- ret = append(ret, fromHPathSearchSQLBlockWithContentTerms(resultBlock, terms, contentTerms, beforeLen))+ contentTerms, hPathTerms := partitionSearchTerms(docContent, terms)+ ret = append(ret, fromHPathSearchSQLBlockWithContentTerms(resultBlock, hPathTerms, contentTerms, beforeLen))} else {- ret = append(ret, fromSQLBlock(resultBlock, terms, beforeLen))+ ret = append(ret, fromSQLBlockWithHPathTerms(resultBlock, terms, "", beforeLen))}}if 1 > len(ret) {@@ -3054,15 +3061,17 @@ func markSearch(text string, keyword string, beforeLen int) (marked string, scorreturn}-func matchedSearchTerms(text, terms string) string {- var matched []string+func partitionSearchTerms(text, terms string) (matched, unmatched string) {+ var matchedTerms, unmatchedTerms []stringfor _, term := range search.SplitKeyword(terms) {pos, _ := search.MarkText(text, term, -1, Conf.Search.CaseSensitive)if -1 < pos {- matched = append(matched, term)+ matchedTerms = append(matchedTerms, term)+ } else {+ unmatchedTerms = append(unmatchedTerms, term)}}- return strings.Join(matched, search.TermSep)+ return strings.Join(matchedTerms, search.TermSep), strings.Join(unmatchedTerms, search.TermSep)}func fromSQLBlocks(sqlBlocks *[]*sql.Block, terms string, beforeLen int) (ret []*Block) {@@ -3073,6 +3082,10 @@ func fromSQLBlocks(sqlBlocks *[]*sql.Block, terms string, beforeLen int) (ret []}func fromSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Block) {+ return fromSQLBlockWithHPathTerms(sqlBlock, terms, terms, beforeLen)+}++func fromSQLBlockWithHPathTerms(sqlBlock *sql.Block, contentTerms, hPathTerms string, beforeLen int) (block *Block) {if nil == sqlBlock {return}@@ -3091,9 +3104,9 @@ func fromSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Bloc}}- content, _ = markSearch(content, terms, beforeLen)+ content, _ = markSearch(content, contentTerms, beforeLen)content = maxContent(content, 5120)- tag, _ := markSearch(sqlBlock.Tag, terms, beforeLen)+ tag, _ := markSearch(sqlBlock.Tag, contentTerms, beforeLen)markdown := maxContent(sqlBlock.Markdown, 5120)fContent := util.EscapeHTML(sqlBlock.FContent) // fContent 会用于和 content 对比,在反链计算时用于判断是否是列表项下第一个子块,所以也需要转义 https://github.com/siyuan-note/siyuan/issues/11001block = &Block{@@ -3126,18 +3139,18 @@ func fromSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Bloc}hPathBeforeLen := 18- if "" != terms {+ if "" != hPathTerms {hPathBeforeLen = -1}- markBlockHPath(block, sqlBlock.HPath, terms, hPathBeforeLen)+ markBlockHPath(block, sqlBlock.HPath, hPathTerms, hPathBeforeLen)if "" != block.Name {- block.Name, _ = markSearch(block.Name, terms, 256)+ block.Name, _ = markSearch(block.Name, contentTerms, 256)}if "" != block.Alias {- block.Alias, _ = markSearch(block.Alias, terms, 256)+ block.Alias, _ = markSearch(block.Alias, contentTerms, 256)}if "" != block.Memo {- block.Memo, _ = markSearch(block.Memo, terms, 256)+ block.Memo, _ = markSearch(block.Memo, contentTerms, 256)}return}@@ -3151,13 +3164,11 @@ func markBlockHPath(block *Block, hPath, terms string, beforeLen int) {}func fromHPathSearchSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Block) {- return fromHPathSearchSQLBlockWithContentTerms(sqlBlock, terms, "", beforeLen)+ return fromSQLBlockWithHPathTerms(sqlBlock, "", terms, beforeLen)}func fromHPathSearchSQLBlockWithContentTerms(sqlBlock *sql.Block, hPathTerms, contentTerms string, beforeLen int) (block *Block) {- block = fromSQLBlock(sqlBlock, contentTerms, beforeLen)- markBlockHPath(block, sqlBlock.HPath, hPathTerms, -1)- return+ return fromSQLBlockWithHPathTerms(sqlBlock, contentTerms, hPathTerms, beforeLen)}func maxContent(content string, maxLen int) string {diff --git a/kernel/model/search_test.go b/kernel/model/search_test.goindex 35c8648325..79d481e7a7 100644--- a/kernel/model/search_test.go+++ b/kernel/model/search_test.go@@ -526,10 +526,13 @@ func TestFromHPathSearchSQLBlockOnlyMarksHPath(t *testing.T) {func TestFromMixedHPathSearchSQLBlockMarksDirectContent(t *testing.T) {setSearchCaseSensitive(t, true)terms := "从这里" + search.TermSep + "会员"- contentTerms := matchedSearchTerms("会员特权", terms)+ contentTerms, hPathTerms := partitionSearchTerms("会员特权", terms)if "会员" != contentTerms {t.Fatalf("文档自身直接命中的关键词错误:%q", contentTerms)}+ if "从这里" != hPathTerms {+ t.Fatalf("路径补充命中的关键词错误:%q", hPathTerms)+ }sqlBlock := &sql.Block{ID: "20260731160000-hpath03",@@ -539,7 +542,7 @@ func TestFromMixedHPathSearchSQLBlockMarksDirectContent(t *testing.T) {Content: "会员特权",Type: "d",}- block := fromHPathSearchSQLBlockWithContentTerms(sqlBlock, terms, contentTerms, 36)+ block := fromHPathSearchSQLBlockWithContentTerms(sqlBlock, hPathTerms, contentTerms, 36)if "<mark>会员</mark>特权" != block.Content {t.Fatalf("混合命中应高亮文档自身直接命中的关键词:%q", block.Content)}@@ -547,8 +550,36 @@ func TestFromMixedHPathSearchSQLBlockMarksDirectContent(t *testing.T) {t.Fatalf("路径关键词不应扩展高亮到其他文档字段:%q", block.Name)}if !strings.Contains(block.HPath, "<mark>从这里</mark>") ||- !strings.Contains(block.HPath, "<mark>会员</mark>") {- t.Fatalf("混合命中应高亮全部路径关键词:%q", block.HPath)+ strings.Contains(block.HPath, "<mark>会员</mark>") {+ t.Fatalf("混合命中只应高亮路径补充的关键词:%q", block.HPath)+ }+}++func TestFromFTSAndHPathMatchSQLBlockMarksMatchSource(t *testing.T) {+ setSearchCaseSensitive(t, true)+ sqlBlock := &sql.Block{+ ID: "20260905120000-direct1",+ RootID: "20260905120000-direct1",+ HPath: "/项目/工作总结",+ Content: search.SearchMarkLeft + "工作总结" + search.SearchMarkRight,+ Type: "d",+ }++ directBlock := fromFTSAndHPathMatchSQLBlock(sqlBlock, 0, "工作总结", 36)+ if "<mark>工作总结</mark>" != directBlock.Content {+ t.Fatalf("FTS 直接命中应保留正文高亮:%q", directBlock.Content)+ }+ if strings.Contains(directBlock.HPath, "<mark>") {+ t.Fatalf("FTS 直接命中不应重复高亮层级路径:%q", directBlock.HPath)+ }++ sqlBlock.Content = "其他内容"+ hPathBlock := fromFTSAndHPathMatchSQLBlock(sqlBlock, 1, "工作总结", 36)+ if strings.Contains(hPathBlock.Content, "<mark>") {+ t.Fatalf("路径命中不应高亮正文:%q", hPathBlock.Content)+ }+ if !strings.Contains(hPathBlock.HPath, "<mark>工作总结</mark>") {+ t.Fatalf("路径命中应高亮层级路径:%q", hPathBlock.HPath)}}