From 455f348e913b576a2b21e8b0260a437019c87583 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Wed, 3 Apr 2024 11:12:48 +0800 Subject: [PATCH] :art: Improve block ref search ID extraction https://github.com/siyuan-note/siyuan/issues/10848 --- kernel/model/search.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/kernel/model/search.go b/kernel/model/search.go index b29ae1ab3..e1a93c31c 100644 --- a/kernel/model/search.go +++ b/kernel/model/search.go @@ -1045,8 +1045,8 @@ func removeLimitClause(stmt string) string { func fullTextSearchRefBlock(keyword string, beforeLen int, onlyDoc bool) (ret []*Block) { keyword = filterQueryInvisibleChars(keyword) - if ast.IsNodeIDPattern(keyword) { - ret, _, _ = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+keyword+"'", 36, 1, 32) + if id := extractID(keyword); "" != id { + ret, _, _ = searchBySQL("SELECT * FROM `blocks` WHERE `id` = '"+id+"'", 36, 1, 32) return } @@ -1106,6 +1106,21 @@ func fullTextSearchRefBlock(keyword string, beforeLen int, onlyDoc bool) (ret [] return } +func extractID(content string) (ret string) { + if 22 > len(content) { + return + } + + // 从第一个字符开始循环,直到找到一个合法的 ID 为止 + for i := 0; i < len(content)-21; i++ { + if ast.IsNodeIDPattern(content[i : i+22]) { + ret = content[i : i+22] + return + } + } + return +} + func fullTextSearchByQuerySyntax(query, boxFilter, pathFilter, typeFilter, orderBy string, beforeLen, page, pageSize int) (ret []*Block, matchedBlockCount, matchedRootCount int) { query = filterQueryInvisibleChars(query) if ast.IsNodeIDPattern(query) {