Skip to content

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

:bug: Filter non-block heading children https://github.com/siyuan-note/siyuan/issues/18522

Daniel81082b17383d
diff --git a/kernel/model/block.go b/kernel/model/block.go
index eb286d7fe1..f7bd2acbed 100644
--- a/kernel/model/block.go
+++ b/kernel/model/block.go
@@ -793,10 +793,14 @@ func GetHeadingChildrenIDs(id string) (ret []string) {
if nil == heading || ast.NodeHeading != heading.Type {
return
}
+ return headingChildrenIDs(heading)
+}
- children := treenode.HeadingChildren(heading)
- nodes := append([]*ast.Node{}, children...)
- for _, n := range nodes {
+func headingChildrenIDs(heading *ast.Node) (ret []string) {
+ for _, n := range treenode.HeadingChildren(heading) {
+ if !n.IsBlock() {
+ continue
+ }
ret = append(ret, n.ID)
}
return
@@ -1337,7 +1341,11 @@ func GetChildBlocksInBox(id, boxID string) (ret []*ChildBlock) {
if err != nil {
return
}
+ return getChildBlocksFromTree(id, tree)
+}
+func getChildBlocksFromTree(id string, tree *parse.Tree) (ret []*ChildBlock) {
+ ret = []*ChildBlock{}
node := treenode.GetNodeInTree(tree, id)
if nil == node {
return
@@ -1346,14 +1354,9 @@ func GetChildBlocksInBox(id, boxID string) (ret []*ChildBlock) {
if ast.NodeHeading == node.Type {
children := treenode.HeadingChildren(node)
for _, c := range children {
- block := sql.BuildBlockFromNode(c, tree)
- ret = append(ret, &ChildBlock{
- ID: c.ID,
- Type: treenode.TypeAbbr(c.Type.String()),
- SubType: treenode.SubTypeAbbr(c),
- Content: block.Content,
- Markdown: block.Markdown,
- })
+ if childBlock := childBlockFromNode(c, tree); nil != childBlock {
+ ret = append(ret, childBlock)
+ }
}
return
}
@@ -1363,22 +1366,28 @@ func GetChildBlocksInBox(id, boxID string) (ret []*ChildBlock) {
}
for c := node.FirstChild; nil != c; c = c.Next {
- if !c.IsBlock() {
- continue
+ if childBlock := childBlockFromNode(c, tree); nil != childBlock {
+ ret = append(ret, childBlock)
}
-
- block := sql.BuildBlockFromNode(c, tree)
- ret = append(ret, &ChildBlock{
- ID: c.ID,
- Type: treenode.TypeAbbr(c.Type.String()),
- SubType: treenode.SubTypeAbbr(c),
- Content: block.Content,
- Markdown: block.Markdown,
- })
}
return
}
+func childBlockFromNode(node *ast.Node, tree *parse.Tree) *ChildBlock {
+ if !node.IsBlock() {
+ return nil
+ }
+
+ block := sql.BuildBlockFromNode(node, tree)
+ return &ChildBlock{
+ ID: node.ID,
+ Type: treenode.TypeAbbr(node.Type.String()),
+ SubType: treenode.SubTypeAbbr(node),
+ Content: block.Content,
+ Markdown: block.Markdown,
+ }
+}
+
func GetTailChildBlocks(id string, n int) (ret []*ChildBlock) {
return GetTailChildBlocksInBox(id, n, "")
}
@@ -1400,7 +1409,11 @@ func GetTailChildBlocksInBox(id string, n int, boxID string) (ret []*ChildBlock)
if err != nil {
return
}
+ return getTailChildBlocksFromTree(id, n, tree)
+}
+func getTailChildBlocksFromTree(id string, n int, tree *parse.Tree) (ret []*ChildBlock) {
+ ret = []*ChildBlock{}
node := treenode.GetNodeInTree(tree, id)
if nil == node {
return
@@ -1410,14 +1423,11 @@ func GetTailChildBlocksInBox(id string, n int, boxID string) (ret []*ChildBlock)
children := treenode.HeadingChildren(node)
for i := len(children) - 1; 0 <= i; i-- {
c := children[i]
- block := sql.BuildBlockFromNode(c, tree)
- ret = append(ret, &ChildBlock{
- ID: c.ID,
- Type: treenode.TypeAbbr(c.Type.String()),
- SubType: treenode.SubTypeAbbr(c),
- Content: block.Content,
- Markdown: block.Markdown,
- })
+ childBlock := childBlockFromNode(c, tree)
+ if nil == childBlock {
+ continue
+ }
+ ret = append(ret, childBlock)
if n == len(ret) {
return
}
@@ -1430,18 +1440,11 @@ func GetTailChildBlocksInBox(id string, n int, boxID string) (ret []*ChildBlock)
}
for c := node.LastChild; nil != c; c = c.Previous {
- if !c.IsBlock() {
+ childBlock := childBlockFromNode(c, tree)
+ if nil == childBlock {
continue
}
-
- block := sql.BuildBlockFromNode(c, tree)
- ret = append(ret, &ChildBlock{
- ID: c.ID,
- Type: treenode.TypeAbbr(c.Type.String()),
- SubType: treenode.SubTypeAbbr(c),
- Content: block.Content,
- Markdown: block.Markdown,
- })
+ ret = append(ret, childBlock)
if n == len(ret) {
return
diff --git a/kernel/model/block_child_test.go b/kernel/model/block_child_test.go
new file mode 100644
index 0000000000..d5200aad80
--- /dev/null
+++ b/kernel/model/block_child_test.go
@@ -0,0 +1,73 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "slices"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+)
+
+func TestHeadingChildBlocksSkipSuperBlockCloseMarker(t *testing.T) {
+ const (
+ rootID = "20260803160000-root001"
+ superBlockID = "20260803160001-super01"
+ headingID = "20260803160002-heading"
+ paragraphAID = "20260803160003-para001"
+ paragraphBID = "20260803160004-para002"
+ )
+
+ root := &ast.Node{Type: ast.NodeDocument, ID: rootID}
+ superBlock := &ast.Node{Type: ast.NodeSuperBlock, ID: superBlockID}
+ root.AppendChild(superBlock)
+ superBlock.AppendChild(&ast.Node{Type: ast.NodeSuperBlockOpenMarker})
+ superBlock.AppendChild(&ast.Node{Type: ast.NodeSuperBlockLayoutMarker})
+ heading := &ast.Node{Type: ast.NodeHeading, ID: headingID, HeadingLevel: 4}
+ superBlock.AppendChild(heading)
+ paragraphA := treenode.NewParagraph(paragraphAID)
+ paragraphA.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("Paragraph A")})
+ superBlock.AppendChild(paragraphA)
+ paragraphB := treenode.NewParagraph(paragraphBID)
+ paragraphB.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("Paragraph B")})
+ superBlock.AppendChild(paragraphB)
+ superBlock.AppendChild(&ast.Node{Type: ast.NodeSuperBlockCloseMarker})
+ tree := &parse.Tree{Root: root, ID: rootID}
+
+ children := getChildBlocksFromTree(headingID, tree)
+ if 2 != len(children) {
+ t.Fatalf("expected two child blocks, got [%d]", len(children))
+ }
+ if paragraphAID != children[0].ID || paragraphBID != children[1].ID {
+ t.Fatalf("unexpected child block IDs: [%s, %s]", children[0].ID, children[1].ID)
+ }
+ if "p" != children[0].Type || "p" != children[1].Type {
+ t.Fatalf("unexpected child block types: [%s, %s]", children[0].Type, children[1].Type)
+ }
+
+ tail := getTailChildBlocksFromTree(headingID, 1, tree)
+ if 1 != len(tail) || paragraphBID != tail[0].ID {
+ t.Fatalf("unexpected tail child blocks: [%v]", tail)
+ }
+
+ ids := headingChildrenIDs(heading)
+ if !slices.Equal(ids, []string{paragraphAID, paragraphBID}) {
+ t.Fatalf("unexpected heading child IDs: [%v]", ids)
+ }
+}