Skip to content

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

:bug: 表格块过大时搜索无法高亮并定位匹配的关键字 Fix https://github.com/siyuan-note/siyuan/issues/6103

Liang Ding3a1d8de0a3d7
diff --git a/kernel/model/search.go b/kernel/model/search.go
index 7cf67eeb03..14e65d11bc 100644
--- a/kernel/model/search.go
+++ b/kernel/model/search.go
@@ -452,9 +452,6 @@ func query2Stmt(queryStr string) (ret string) {
func markSearch(text string, keyword string, beforeLen int) (marked string, score float64) {
if 0 == len(keyword) {
marked = text
- if maxLen := 5120; maxLen < utf8.RuneCountInString(marked) {
- marked = gulu.Str.SubStr(marked, maxLen) + "..."
- }
if strings.Contains(marked, search.SearchMarkLeft) { // 使用 FTS snippet() 处理过高亮片段,这里简单替换后就返回
marked = html.EscapeString(text)
@@ -550,6 +547,21 @@ func fromSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Bloc
}
func maxContent(content string, maxLen int) string {
+ idx := strings.Index(content, "<mark>")
+ if 128 < maxLen && maxLen <= idx {
+ head := bytes.Buffer{}
+ for i := 0; i < 512; i++ {
+ r, size := utf8.DecodeLastRuneInString(content[:idx])
+ head.WriteRune(r)
+ idx -= size
+ if 64 < head.Len() {
+ break
+ }
+ }
+
+ content = util.Reverse(head.String()) + content[idx:]
+ }
+
if maxLen < utf8.RuneCountInString(content) {
return gulu.Str.SubStr(content, maxLen) + "..."
}
diff --git a/kernel/util/string.go b/kernel/util/string.go
index 739e0a6bcd..6f25cb2552 100644
--- a/kernel/util/string.go
+++ b/kernel/util/string.go
@@ -15,3 +15,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package util
+
+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)
+}