Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Prevent search path highlights from overflowing https://github.com/siyuan-note/siyuan/issues/18305

Daniel1b9e3810bebf
diff --git a/kernel/model/search.go b/kernel/model/search.go
index c8e2115c61..bbe49dc64e 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -566,11 +566,32 @@ func filterSelfHPath(blocks []*Block) {
for _, b := range blocks {
if b.IsDoc() {
- b.HPath = strings.TrimSuffix(b.HPath, path.Base(b.HPath))
+ b.HPath = trimSelfHPath(b.HPath)
}
}
}
+func trimSelfHPath(hPath string) string {
+ inTag := false
+ lastSlash := -1
+ for i, r := range hPath {
+ switch r {
+ case '<':
+ inTag = true
+ case '>':
+ inTag = false
+ case '/':
+ if !inTag {
+ lastSlash = i
+ }
+ }
+ }
+ if 0 > lastSlash {
+ return hPath
+ }
+ return hPath[:lastSlash+1]
+}
+
func prependNotebookNameInHPath(blocks []*Block) {
// 在 hPath 中加入笔记本名 Show notebooks in hpath of block ref search list results https://github.com/siyuan-note/siyuan/issues/9378
diff --git a/kernel/model/search_test.go b/kernel/model/search_test.go
index 154b9a9d65..d246c1afc2 100644
--- a/kernel/model/search_test.go
+++ b/kernel/model/search_test.go
@@ -405,6 +405,44 @@ func TestFromHPathSearchSQLBlockOnlyMarksHPath(t *testing.T) {
}
}
+func TestFilterSelfHPathPreservesHighlight(t *testing.T) {
+ setSearchCaseSensitive(t, true)
+ multipleKeywords := fromHPathSearchSQLBlock(&sql.Block{
+ ID: "20260731120000-hpath01",
+ RootID: "20260731120000-hpath01",
+ HPath: "/思源笔记用户指南/请/从这里开始/会员特权",
+ Content: "会员特权",
+ Type: "d",
+ }, "从这里"+search.TermSep+"会员", 36)
+ singleKeyword := fromHPathSearchSQLBlock(&sql.Block{
+ ID: "20260731120001-hpath02",
+ RootID: "20260731120001-hpath02",
+ HPath: "/会员特权",
+ Content: "会员特权",
+ Type: "d",
+ }, "会员", 36)
+ blocks := []*Block{
+ multipleKeywords,
+ singleKeyword,
+ {
+ Type: "NodeParagraph",
+ HPath: "/思源笔记用户指南/<mark>会员</mark>特权",
+ },
+ }
+
+ filterSelfHPath(blocks)
+
+ if expected := "/思源笔记用户指南/请/<mark>从这里</mark>开始/"; expected != blocks[0].HPath {
+ t.Fatalf("多关键字高亮路径移除文档自身后错误:got %q, want %q", blocks[0].HPath, expected)
+ }
+ if expected := "/"; expected != blocks[1].HPath {
+ t.Fatalf("单关键字高亮路径移除文档自身后错误:got %q, want %q", blocks[1].HPath, expected)
+ }
+ if expected := "/思源笔记用户指南/<mark>会员</mark>特权"; expected != blocks[2].HPath {
+ t.Fatalf("非文档块路径不应变化:got %q, want %q", blocks[2].HPath, expected)
+ }
+}
+
func TestBuildHPathSearchOrderBy(t *testing.T) {
setSearchCaseSensitive(t, true)
assertOrderBySequence(t, buildHPathSearchOrderBy("Parent", 0),