This commit is contained in:
Liang Ding 2022-09-16 18:02:04 +08:00
parent dcee38bcfa
commit c5f0b96929
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
13 changed files with 133 additions and 85 deletions

View file

@ -1117,27 +1117,22 @@ func exportTree(tree *parse.Tree, wysiwyg, expandKaTexMacros bool) (ret *parse.T
return ast.WalkSkipChildren
}
if ast.NodeBlockRef != n.Type {
if !treenode.IsBlockRef(n) {
return ast.WalkContinue
}
// 处理引用节点
var linkText string
id := n.ChildByType(ast.NodeBlockRefID).TokensStr()
if anchor := n.ChildByType(ast.NodeBlockRefText); nil != anchor {
linkText = anchor.Text()
} else if anchor = n.ChildByType(ast.NodeBlockRefDynamicText); nil != anchor {
linkText = anchor.Text()
} else {
linkText = sql.GetRefText(id)
defID, linkText, _ := treenode.GetBlockRef(n)
if "" == linkText {
linkText = sql.GetRefText(defID)
}
if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(linkText) {
linkText = gulu.Str.SubStr(linkText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
}
linkText = Conf.Export.BlockRefTextLeft + linkText + Conf.Export.BlockRefTextRight
defTree, _ := loadTreeByBlockID(id)
defTree, _ := loadTreeByBlockID(defID)
if nil == defTree {
return ast.WalkContinue
}
@ -1150,13 +1145,12 @@ func exportTree(tree *parse.Tree, wysiwyg, expandKaTexMacros bool) (ret *parse.T
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte(linkText)})
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte("siyuan://blocks/" + id)})
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte("siyuan://blocks/" + defID)})
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
n.InsertBefore(blockRefLink)
case 3: // 仅锚文本
n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(linkText)})
case 4: // 脚注
defID := n.ChildByType(ast.NodeBlockRefID).TokensStr()
refFoot := getRefAsFootnotes(defID, &refFootnotes)
n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(linkText)})
n.InsertBefore(&ast.Node{Type: ast.NodeFootnotesRef, Tokens: []byte("^" + refFoot.refNum), FootnotesRefId: refFoot.refNum, FootnotesRefLabel: []byte("^" + refFoot.refNum)})
@ -1295,8 +1289,8 @@ func resolveFootnotesDefs(refFootnotes *[]*refAsFootnotes, rootID string) (footn
return ast.WalkContinue
}
if ast.NodeBlockRef == n.Type {
defID := n.ChildByType(ast.NodeBlockRefID).TokensStr()
if treenode.IsBlockRef(n) {
defID, _, _ := treenode.GetBlockRef(n)
if f := getRefAsFootnotes(defID, refFootnotes); nil != f {
n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(Conf.Export.BlockRefTextLeft + f.refAnchorText + Conf.Export.BlockRefTextRight)})
n.InsertBefore(&ast.Node{Type: ast.NodeFootnotesRef, Tokens: []byte("^" + f.refNum), FootnotesRefId: f.refNum, FootnotesRefLabel: []byte("^" + f.refNum)})
@ -1412,8 +1406,8 @@ func collectFootnotesDefs0(node *ast.Node, refFootnotes *[]*refAsFootnotes, tree
return ast.WalkContinue
}
if ast.NodeBlockRef == n.Type {
defID := n.ChildByType(ast.NodeBlockRefID).TokensStr()
if treenode.IsBlockRef(n) {
defID, _, _ := treenode.GetBlockRef(n)
if nil == getRefAsFootnotes(defID, refFootnotes) {
anchorText := sql.GetRefText(defID)
if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(anchorText) {
@ -1463,12 +1457,11 @@ func exportRefTrees0(tree *parse.Tree, retTrees *map[string]*parse.Tree) {
return ast.WalkContinue
}
if ast.NodeBlockRef == n.Type {
defIDNode := n.ChildByType(ast.NodeBlockRefID)
if nil == defIDNode {
return ast.WalkSkipChildren
if treenode.IsBlockRef(n) {
defID, _, _ := treenode.GetBlockRef(n)
if "" == defID {
return ast.WalkContinue
}
defID := defIDNode.TokensStr()
defBlock := treenode.GetBlockTree(defID)
if nil == defBlock {
return ast.WalkSkipChildren

View file

@ -637,7 +637,7 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size
}
}
if ast.NodeBlockRef == n.Type {
if treenode.IsBlockRef(n) {
appendRefTextRenderResultForBlockRef(n)
return ast.WalkSkipChildren
}

View file

@ -178,7 +178,7 @@ func GetDocHistoryContent(historyPath, keyword string) (id, rootID, content stri
n.RemoveIALAttr("heading-fold")
n.RemoveIALAttr("fold")
if ast.NodeBlockRef == n.Type {
if treenode.IsBlockRef(n) {
appendRefTextRenderResultForBlockRef(n)
return ast.WalkSkipChildren
}

View file

@ -183,7 +183,7 @@ func toSubTree(blocks []*Block, keyword string) (ret []*Path) {
unfold := true
for liFirstBlockSpan := li.FirstChild.FirstChild; nil != liFirstBlockSpan; liFirstBlockSpan = liFirstBlockSpan.Next {
if ast.NodeBlockRef == liFirstBlockSpan.Type {
if treenode.IsBlockRef(liFirstBlockSpan) {
continue
}
if "" != strings.TrimSpace(liFirstBlockSpan.Text()) {
@ -278,7 +278,7 @@ func toSubTree(blocks []*Block, keyword string) (ret []*Path) {
unfold := true
for headingFirstSpan := h.FirstChild; nil != headingFirstSpan; headingFirstSpan = headingFirstSpan.Next {
if ast.NodeBlockRef == headingFirstSpan.Type {
if treenode.IsBlockRef(headingFirstSpan) {
continue
}
if "" != strings.TrimSpace(headingFirstSpan.Text()) {

View file

@ -237,23 +237,18 @@ func renderTemplate(p, id string) (string, error) {
}
func appendRefTextRenderResultForBlockRef(blockRef *ast.Node) {
if ast.NodeBlockRef != blockRef.Type {
if !treenode.IsBlockRef(blockRef) {
return
}
refText := blockRef.ChildByType(ast.NodeBlockRefText)
if nil != refText {
return
}
refText = blockRef.ChildByType(ast.NodeBlockRefDynamicText)
if nil != refText {
refID, text, _ := treenode.GetBlockRef(blockRef)
if "" != text {
return
}
// 动态解析渲染 ((id)) 的锚文本
// 现行版本已经不存在该语法情况,这里保留是为了迁移历史数据
refID := blockRef.ChildByType(ast.NodeBlockRefID)
text := sql.GetRefText(refID.TokensStr())
text = sql.GetRefText(refID)
if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(text) {
text = gulu.Str.SubStr(text, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
}

View file

@ -883,7 +883,9 @@ func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) {
// 剔除空白的行级公式
unlinks = append(unlinks, n)
}
} else if ast.NodeBlockRefID == n.Type {
} else if ast.NodeBlockRef == n.Type {
sql.CacheRef(subTree, n)
} else if ast.NodeTextMark == n.Type && n.IsTextMarkType("block-ref") {
sql.CacheRef(subTree, n)
}
return ast.WalkContinue
@ -1098,27 +1100,28 @@ func updateRefText(refNode *ast.Node, changedDefNodes map[string]*ast.Node) (cha
if !entering {
return ast.WalkContinue
}
if ast.NodeBlockRef == n.Type && nil != n.ChildByType(ast.NodeBlockRefDynamicText) {
defIDNode := n.ChildByType(ast.NodeBlockRefID)
if nil == defIDNode {
return ast.WalkSkipChildren
}
defID := defIDNode.TokensStr()
defNode := changedDefNodes[defID]
if nil == defNode {
return ast.WalkSkipChildren
}
if ast.NodeDocument != defNode.Type && defNode.IsContainerBlock() {
defNode = treenode.FirstLeafBlock(defNode)
}
defContent := renderBlockText(defNode)
if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(defContent) {
defContent = gulu.Str.SubStr(defContent, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
}
treenode.SetDynamicBlockRefText(n, defContent)
changed = true
if !treenode.IsBlockRef(n) {
return ast.WalkContinue
}
defID, _, subtype := treenode.GetBlockRef(n)
if "s" == subtype || "" == defID {
return ast.WalkContinue
}
defNode := changedDefNodes[defID]
if nil == defNode {
return ast.WalkSkipChildren
}
if ast.NodeDocument != defNode.Type && defNode.IsContainerBlock() {
defNode = treenode.FirstLeafBlock(defNode)
}
defContent := renderBlockText(defNode)
if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(defContent) {
defContent = gulu.Str.SubStr(defContent, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
}
treenode.SetDynamicBlockRefText(n, defContent)
changed = true
return ast.WalkContinue
})
return

View file

@ -52,10 +52,14 @@ func resetTree(tree *parse.Tree, titleSuffix string) {
// 收集所有引用
refIDs := map[string]string{}
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || ast.NodeBlockRefID != n.Type {
if !entering || !treenode.IsBlockRef(n) {
return ast.WalkContinue
}
refIDs[n.TokensStr()] = "1"
defID, _, _ := treenode.GetBlockRef(n)
if "" == defID {
return ast.WalkContinue
}
refIDs[defID] = "1"
return ast.WalkContinue
})
@ -78,11 +82,15 @@ func resetTree(tree *parse.Tree, titleSuffix string) {
// 重置内部引用
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || ast.NodeBlockRefID != n.Type {
if !entering || !treenode.IsBlockRef(n) {
return ast.WalkContinue
}
if "1" != refIDs[n.TokensStr()] {
n.Tokens = []byte(refIDs[n.TokensStr()])
defID, _, _ := treenode.GetBlockRef(n)
if "" == defID {
return ast.WalkContinue
}
if "1" != refIDs[defID] {
n.Tokens = []byte(refIDs[defID])
}
return ast.WalkContinue
})