Skip to content

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

:bug: Read bookmark labels from their indexed attributes https://github.com/siyuan-note/siyuan/issues/20268

Daniel358db4826612
diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go
index 575c6c304e..b355e4552c 100644
--- a/kernel/sql/block_query.go
+++ b/kernel/sql/block_query.go
@@ -27,6 +27,7 @@ import (
"strings"
"github.com/88250/lute/ast"
+ "github.com/88250/lute/html"
"github.com/88250/vitess-sqlparser/sqlparser"
"github.com/emirpasic/gods/sets/hashset"
sqlparser2 "github.com/rqlite/sql"
@@ -315,6 +316,7 @@ func queryDocTitles(searchIgnoreLines []string, boxIDs ...string) (ret []string)
}
const bookmarkBlockPredicate = "id IN (SELECT block_id FROM attributes WHERE name = 'bookmark' AND type = 'b')"
+const bookmarkLabelBlocksQuery = "SELECT (SELECT value FROM attributes WHERE block_id = blocks.id AND name = 'bookmark' AND type = 'b' LIMIT 1), box, path FROM blocks WHERE " + bookmarkBlockPredicate
func QueryBookmarkBlocks() (ret []*Block) {
sqlStmt := "SELECT * FROM blocks WHERE " + bookmarkBlockPredicate
@@ -340,7 +342,7 @@ type BookmarkLabelBlock struct {
func QueryBookmarkLabelBlocks() (ret []*BookmarkLabelBlock) {
ret = []*BookmarkLabelBlock{}
- sqlStmt := "SELECT ial, box, path FROM blocks WHERE " + bookmarkBlockPredicate
+ sqlStmt := bookmarkLabelBlocksQuery
rows, err := query(sqlStmt)
if err != nil {
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
@@ -348,13 +350,14 @@ func QueryBookmarkLabelBlocks() (ret []*BookmarkLabelBlock) {
}
defer rows.Close()
for rows.Next() {
- var ial, box, blockPath string
- if err = rows.Scan(&ial, &box, &blockPath); err != nil {
+ var label, box, blockPath string
+ if err = rows.Scan(&label, &box, &blockPath); err != nil {
logging.LogErrorf("scan query rows failed: %s", err)
continue
}
- if label := ialAttr(ial, "bookmark"); label != "" {
- ret = append(ret, &BookmarkLabelBlock{Label: label, Box: box, Path: blockPath})
+ if label != "" {
+ // 标签按属性表取值,返回时保留既有 IAL 转义形式。
+ ret = append(ret, &BookmarkLabelBlock{Label: html.EscapeAttrVal(label), Box: box, Path: blockPath})
}
}
return
diff --git a/kernel/sql/bookmark_index_test.go b/kernel/sql/bookmark_index_test.go
index c6f4ad6fe7..0a7dd9ed0e 100644
--- a/kernel/sql/bookmark_index_test.go
+++ b/kernel/sql/bookmark_index_test.go
@@ -53,11 +53,11 @@ func TestBookmarkIndexPreservesBlockFieldsAndLabels(t *testing.T) {
labels := []string{"R&D", "<b>", ""quoted"", ""}
for i, label := range labels {
id := string(rune('a' + i))
- ial := `{: bookmark="` + label + `"}`
+ ial := `{: custom-bookmark="wrong" bookmark="` + label + `"}`
if _, err := database.Exec("INSERT INTO blocks VALUES (?, '', ?, '', 'box', ?, '/doc', 'name', '', '', '', 'content', '', 'markdown', 8, 'p', '', ?, 0, '2026', '2027')", id, "root-"+id, "/"+id+".sy", ial); err != nil {
t.Fatal(err)
}
- if _, err := database.Exec("INSERT INTO attributes VALUES (?, 'bookmark', ?, 'b', ?, ?, 'box', ?)", id, label, id, "root-"+id, "/"+id+".sy"); err != nil {
+ if _, err := database.Exec("INSERT INTO attributes VALUES (?, 'bookmark', ?, 'b', ?, ?, 'box', ?)", id, html.UnescapeString(label), id, "root-"+id, "/"+id+".sy"); err != nil {
t.Fatal(err)
}
}
@@ -102,8 +102,8 @@ func TestBookmarkIndexPreservesBlockFieldsAndLabels(t *testing.T) {
if actual := QueryBookmarkLabels(); !reflect.DeepEqual(actual, wantLabels) {
t.Fatalf("labels=%v want=%v", actual, wantLabels)
}
- for _, columns := range []string{"*", "ial, box, path"} {
- plan := queryPlanDetails(t, database, "SELECT "+columns+" FROM blocks WHERE "+bookmarkBlockPredicate)
+ for _, stmt := range []string{"SELECT * FROM blocks WHERE " + bookmarkBlockPredicate, bookmarkLabelBlocksQuery} {
+ plan := queryPlanDetails(t, database, stmt)
if !strings.Contains(plan, "idx_attributes_bookmark_block_id") || !strings.Contains(plan, "SEARCH blocks USING INDEX idx_blocks_id") {
t.Fatalf("bookmark query does not use indexed attribute lookup: %s", plan)
}