An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve search match context without duplicating text https://github.com/siyuan-note/siyuan/issues/20241
Daniel315f26c1a253
diff --git a/kernel/model/search.go b/kernel/model/search.goindex f61f621041..69139722de 100644--- a/kernel/model/search.go+++ b/kernel/model/search.go@@ -3094,17 +3094,14 @@ func fromHPathSearchSQLBlockWithContentTerms(sqlBlock *sql.Block, hPathTerms, cofunc maxContent(content string, maxLen int) string {idx := strings.Index(content, "<mark>")if 128 < maxLen && maxLen <= idx {- head := bytes.Buffer{}- for range 512 {- r, size := utf8.DecodeLastRuneInString(content[:idx])- head.WriteRune(r)+ const matchContextBytes = 64+ matchIdx := idx+ for idx > 0 && matchIdx-idx <= matchContextBytes {+ _, size := utf8.DecodeLastRuneInString(content[:idx])idx -= size- if 64 < head.Len() {- break- }}- content = util.Reverse(head.String()) + content[idx:]+ content = content[idx:]}if maxLen < utf8.RuneCountInString(content) {diff --git a/kernel/model/search_snippet_test.go b/kernel/model/search_snippet_test.gonew file mode 100644index 0000000000..0d10383fda--- /dev/null+++ b/kernel/model/search_snippet_test.go@@ -0,0 +1,45 @@+// SiYuan - From thought to insight, with agents+// Copyright (c) 2020-present, b3log.org+// SPDX-License-Identifier: AGPL-3.0-or-later++package model++import (+ "strings"+ "testing"+ "unicode/utf8"+)++func TestMaxContentKeepsMatchContextWithoutDuplication(t *testing.T) {+ for _, prefix := range []string{strings.Repeat("abcdefghij", 600), strings.Repeat("中文片段", 600)} {+ content := prefix + "<mark>HIT</mark>TAIL"+ got := maxContent(content, 5120)+ if !strings.HasSuffix(content, got) {+ t.Fatalf("snippet is not an original suffix: %q", got)+ }+ if !strings.Contains(got, "<mark>HIT</mark>TAIL") || !utf8.ValidString(got) {+ t.Fatalf("snippet lost the match or split a UTF-8 character: %q", got)+ }+ contextLen := strings.Index(got, "<mark>")+ if contextLen <= 64 || contextLen > 64+utf8.UTFMax {+ t.Fatalf("unexpected context length: %d", contextLen)+ }+ }+}++func TestMaxContentTruncation(t *testing.T) {+ for _, tc := range []struct {+ content string+ limit int+ want string+ }{+ {"short <mark>HIT</mark>", 128, "short <mark>HIT</mark>"},+ {"中文内容", 3, "中文内..."},+ {"<mark>HIT</mark>TAIL", 16, "<mark>HIT</mark>..."},+ {strings.Repeat("x", 6000), 5120, strings.Repeat("x", 5120) + "..."},+ } {+ if got := maxContent(tc.content, tc.limit); got != tc.want {+ t.Fatalf("maxContent(%q, %d) = %q, want %q", tc.content, tc.limit, got, tc.want)+ }+ }+}diff --git a/kernel/util/misc.go b/kernel/util/misc.goindex 4f42d477bd..9737caba4d 100644--- a/kernel/util/misc.go+++ b/kernel/util/misc.go@@ -178,14 +178,6 @@ func HasUnclosedHtmlTag(htmlStr string) bool {return len(stack) != 0}-func Reverse(s string) string {- runes := []rune(s)- for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {- runes[i], runes[j] = runes[j], runes[i]- }- return string(runes)-}-func RemoveRedundantSpace(str string) string {buf := bytes.Buffer{}lastIsChinese := false