Skip to content

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

:zap: Index graph tag nodes and block links https://github.com/siyuan-note/siyuan/issues/20248

Daniel4b69b07f2972
diff --git a/kernel/model/graph.go b/kernel/model/graph.go
index 747feed985..62ac77b0a2 100644
--- a/kernel/model/graph.go
+++ b/kernel/model/graph.go
@@ -229,11 +229,16 @@ func linkTagBlocks(blocks *[]*Block, nodes *[]*GraphNode, links *[]*GraphLink, p
if !isGlobal {
nodeSize = Conf.Graph.Local.NodeSize
}
+ linkTagSpans(*blocks, nodes, links, tagSpans, isGlobal, nodeSize)
+}
+func linkTagSpans(blocks []*Block, nodes *[]*GraphNode, links *[]*GraphLink, tagSpans []*sql.Span, isGlobal bool, nodeSize float64) {
// 构造标签节点
var tagNodes []*GraphNode
+ tagNodesByContent := make(map[string]*GraphNode, len(tagSpans))
+ tagSpansByBlock := make(map[string][]*sql.Span)
for _, tagSpan := range tagSpans {
- if nil == tagNodeIn(tagNodes, tagSpan.Content) {
+ if nil == tagNodesByContent[tagSpan.Content] {
node := &GraphNode{
ID: tagSpan.Content,
Label: tagSpan.Content,
@@ -242,27 +247,26 @@ func linkTagBlocks(blocks *[]*Block, nodes *[]*GraphNode, links *[]*GraphLink, p
}
*nodes = append(*nodes, node)
tagNodes = append(tagNodes, node)
+ tagNodesByContent[tagSpan.Content] = node
+ }
+ blockID := tagSpan.BlockID
+ if isGlobal {
+ blockID = tagSpan.RootID
}
+ tagSpansByBlock[blockID] = append(tagSpansByBlock[blockID], tagSpan)
}
- // 连接标签和块
- for _, block := range *blocks {
- for _, tagSpan := range tagSpans {
- if isGlobal { // 全局关系图将标签链接到文档块上
- if block.RootID == tagSpan.RootID { // 局部关系图将标签链接到子块上
- *links = append(*links, &GraphLink{
- From: tagSpan.Content,
- To: block.RootID,
- })
- }
- } else {
- if block.ID == tagSpan.BlockID { // 局部关系图将标签链接到子块上
- *links = append(*links, &GraphLink{
- From: tagSpan.Content,
- To: block.ID,
- })
- }
- }
+ // 按块查找标签,保持块顺序、标签顺序和重复连线。
+ for _, block := range blocks {
+ blockID := block.ID
+ if isGlobal { // 全局关系图将标签链接到文档块上。
+ blockID = block.RootID
+ }
+ for _, tagSpan := range tagSpansByBlock[blockID] {
+ *links = append(*links, &GraphLink{
+ From: tagSpan.Content,
+ To: blockID,
+ })
}
}
@@ -274,7 +278,7 @@ func linkTagBlocks(blocks *[]*Block, nodes *[]*GraphNode, links *[]*GraphLink, p
}
for _, targetID := range ids[:len(ids)-1] {
- if targetTag := tagNodeIn(tagNodes, targetID); nil != targetTag {
+ if targetTag := tagNodesByContent[targetID]; nil != targetTag {
*links = append(*links, &GraphLink{
From: tagNode.ID,
@@ -285,15 +289,6 @@ func linkTagBlocks(blocks *[]*Block, nodes *[]*GraphNode, links *[]*GraphLink, p
}
}
-func tagNodeIn(tagNodes []*GraphNode, content string) *GraphNode {
- for _, tagNode := range tagNodes {
- if tagNode.Label == content {
- return tagNode
- }
- }
- return nil
-}
-
func growTreeGraph(forwardlinks, backlinks *[]*Block, nodes *[]*GraphNode) {
forwardDepth, backDepth := 0, 0
visitedIDs := graphNodeIDs(*nodes)
diff --git a/kernel/model/graph_test.go b/kernel/model/graph_test.go
index dd2305bc59..1d43d84106 100644
--- a/kernel/model/graph_test.go
+++ b/kernel/model/graph_test.go
@@ -10,12 +10,53 @@ package model
import (
gosql "database/sql"
+ "reflect"
"strings"
"testing"
"github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/sql"
)
+func TestLinkTagSpansPreservesGraph(t *testing.T) {
+ blocks := []*Block{{ID: "b1", RootID: "r1"}, {ID: "b2", RootID: "r1"}, {ID: "b3", RootID: "r2"}}
+ spans := []*sql.Span{
+ {Content: "a", Type: "tag strong", BlockID: "b1", RootID: "r1"},
+ {Content: "a", Type: "tag", BlockID: "b1", RootID: "r1"},
+ {Content: "a/b/c", Type: "tag", BlockID: "b2", RootID: "r1"},
+ {Content: "b", Type: "tag", BlockID: "b3", RootID: "r2"},
+ {Content: "orphan", Type: "tag", BlockID: "missing", RootID: "missing"},
+ }
+ for _, global := range []bool{false, true} {
+ nodes := []*GraphNode{{ID: "existing"}}
+ links := []*GraphLink{{From: "existing", To: "existing"}}
+ linkTagSpans(blocks, &nodes, &links, spans, global, 12)
+ wantNodes := []*GraphNode{{ID: "existing"},
+ {ID: "a", Label: "a", Type: "tag strong", Size: 12},
+ {ID: "a/b/c", Label: "a/b/c", Type: "tag", Size: 12},
+ {ID: "b", Label: "b", Type: "tag", Size: 12},
+ {ID: "orphan", Label: "orphan", Type: "tag", Size: 12}}
+ wantLinks := []*GraphLink{{From: "existing", To: "existing"},
+ {From: "a", To: "b1"}, {From: "a", To: "b1"}, {From: "a/b/c", To: "b2"}, {From: "b", To: "b3"},
+ {From: "a/b/c", To: "a"}, {From: "a/b/c", To: "b"}}
+ if global {
+ wantLinks = []*GraphLink{{From: "existing", To: "existing"},
+ {From: "a", To: "r1"}, {From: "a", To: "r1"}, {From: "a/b/c", To: "r1"},
+ {From: "a", To: "r1"}, {From: "a", To: "r1"}, {From: "a/b/c", To: "r1"}, {From: "b", To: "r2"},
+ {From: "a/b/c", To: "a"}, {From: "a/b/c", To: "b"}}
+ }
+ if !reflect.DeepEqual(nodes, wantNodes) || !reflect.DeepEqual(links, wantLinks) {
+ t.Fatalf("global=%v: nodes=%+v links=%+v", global, nodes, links)
+ }
+ }
+ var nodes []*GraphNode
+ var links []*GraphLink
+ linkTagSpans(nil, &nodes, &links, nil, false, 12)
+ if len(nodes) != 0 || len(links) != 0 {
+ t.Fatal("empty graph gained nodes or links")
+ }
+}
+
func TestGraphSearchLiteralWildcards(t *testing.T) {
previous := Conf
Conf = &AppConf{Search: conf.NewSearch()}