Skip to content

Commit e321c63

Browse files
committed
🎨 Update the parent_id of blocks below a heading block when updating the heading block #14871
1 parent 35ca2be commit e321c63

File tree

2 files changed

+8
-23
lines changed

2 files changed

+8
-23
lines changed

kernel/sql/database.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ func buildBlockFromNode(n *ast.Node, tree *parse.Tree) (block *Block, attributes
849849
fcontent = NodeStaticContent(fc, nil, true, false, true)
850850

851851
parentID = n.Parent.ID
852-
if h := heading(n); nil != h { // 如果在标题块下方,则将标题块作为父节点
852+
if h := treenode.HeadingParent(n); nil != h { // 如果在标题块下方,则将标题块作为父节点
853853
parentID = h.ID
854854
}
855855
length = utf8.RuneCountInString(fcontent)
@@ -861,7 +861,7 @@ func buildBlockFromNode(n *ast.Node, tree *parse.Tree) (block *Block, attributes
861861
content = NodeStaticContent(n, nil, true, indexAssetPath, true)
862862

863863
parentID = n.Parent.ID
864-
if h := heading(n); nil != h {
864+
if h := treenode.HeadingParent(n); nil != h {
865865
parentID = h.ID
866866
}
867867
length = utf8.RuneCountInString(content)
@@ -945,26 +945,6 @@ func tagFromNode(node *ast.Node) (ret string) {
945945
return strings.TrimSpace(tagBuilder.String())
946946
}
947947

948-
func heading(node *ast.Node) *ast.Node {
949-
if nil == node {
950-
return nil
951-
}
952-
953-
currentLevel := 16
954-
if ast.NodeHeading == node.Type {
955-
currentLevel = node.HeadingLevel
956-
}
957-
958-
for prev := node.Previous; nil != prev; prev = prev.Previous {
959-
if ast.NodeHeading == prev.Type {
960-
if prev.HeadingLevel < currentLevel {
961-
return prev
962-
}
963-
}
964-
}
965-
return nil
966-
}
967-
968948
func deleteByBoxTx(tx *sql.Tx, box string) (err error) {
969949
if err = deleteBlocksByBoxTx(tx, box); err != nil {
970950
return

kernel/treenode/tree.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ func NodeHash(node *ast.Node, tree *parse.Tree, luteEngine *lute.Lute) string {
4444
}
4545
hpath := tree.HPath
4646
data := tree.Box + tree.Path + hpath + string(ial) + md
47+
var parentID string
4748
if nil != node.Parent {
48-
data += node.Parent.ID
49+
parentID = node.Parent.ID
4950
}
51+
if h := HeadingParent(node); nil != h {
52+
parentID = h.ID
53+
}
54+
data += parentID
5055
return fmt.Sprintf("%x", sha256.Sum256(gulu.Str.ToBytes(data)))[:7]
5156
}
5257

0 commit comments

Comments
 (0)