mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
♻️ 移除旧版中的行级元素实现代码 https://github.com/siyuan-note/siyuan/issues/6819
This commit is contained in:
parent
c69983c56c
commit
637f1427e4
11 changed files with 24 additions and 322 deletions
|
|
@ -656,21 +656,10 @@ func GetContainerText(container *ast.Node) string {
|
|||
return ast.WalkContinue
|
||||
}
|
||||
switch n.Type {
|
||||
case ast.NodeText, ast.NodeLinkText, ast.NodeFileAnnotationRefText, ast.NodeFootnotesRef,
|
||||
ast.NodeCodeBlockCode, ast.NodeMathBlockContent:
|
||||
case ast.NodeText, ast.NodeLinkText, ast.NodeCodeBlockCode, ast.NodeMathBlockContent:
|
||||
buf.Write(n.Tokens)
|
||||
case ast.NodeTextMark:
|
||||
buf.WriteString(n.Content())
|
||||
case ast.NodeBlockRef:
|
||||
if anchor := n.ChildByType(ast.NodeBlockRefText); nil != anchor {
|
||||
buf.WriteString(anchor.Text())
|
||||
} else if anchor = n.ChildByType(ast.NodeBlockRefDynamicText); nil != anchor {
|
||||
buf.WriteString(anchor.Text())
|
||||
} else {
|
||||
text := GetRefText(n.TokensStr())
|
||||
buf.WriteString(text)
|
||||
}
|
||||
return ast.WalkSkipChildren
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
|
|
|
|||
|
|
@ -276,34 +276,6 @@ func refsFromTree(tree *parse.Tree) (refs []*Ref, fileAnnotationRefs []*FileAnno
|
|||
if treenode.IsBlockRef(n) {
|
||||
ref := buildRef(tree, n)
|
||||
refs = append(refs, ref)
|
||||
} else if ast.NodeFileAnnotationRefID == n.Type {
|
||||
pathID := n.TokensStr()
|
||||
idx := strings.LastIndex(pathID, "/")
|
||||
if -1 == idx {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
filePath := pathID[:idx]
|
||||
annotationID := pathID[idx+1:]
|
||||
|
||||
anchor := n.Parent.ChildByType(ast.NodeFileAnnotationRefText)
|
||||
text := filePath
|
||||
if nil != anchor {
|
||||
text = anchor.Text()
|
||||
}
|
||||
parentBlock := treenode.ParentBlock(n)
|
||||
ref := &FileAnnotationRef{
|
||||
ID: ast.NewNodeID(),
|
||||
FilePath: filePath,
|
||||
AnnotationID: annotationID,
|
||||
BlockID: parentBlock.ID,
|
||||
RootID: tree.ID,
|
||||
Box: tree.Box,
|
||||
Path: tree.Path,
|
||||
Content: text,
|
||||
Type: treenode.TypeAbbr(n.Type.String()),
|
||||
}
|
||||
fileAnnotationRefs = append(fileAnnotationRefs, ref)
|
||||
} else if ast.NodeTextMark == n.Type && n.IsTextMarkType("file-annotation-ref") {
|
||||
pathID := n.TextMarkFileAnnotationRefID
|
||||
idx := strings.LastIndex(pathID, "/")
|
||||
|
|
@ -365,99 +337,6 @@ func buildRef(tree *parse.Tree, refNode *ast.Node) *Ref {
|
|||
}
|
||||
}
|
||||
|
||||
func ResolveRefContent(block *Block, anchors *map[string]string) (ret string) {
|
||||
if "d" == block.Type {
|
||||
(*anchors)[block.ID] = block.Content
|
||||
return block.Content
|
||||
}
|
||||
|
||||
tree := parse.Parse("", []byte(block.Markdown), luteEngine.ParseOptions)
|
||||
depth := 0
|
||||
var stack []string
|
||||
c := treenode.FirstLeafBlock(tree.Root)
|
||||
ret = resolveRefContent0(c, anchors, &depth, &stack)
|
||||
return
|
||||
}
|
||||
|
||||
func resolveRefContent0(node *ast.Node, anchors *map[string]string, depth *int, stack *[]string) (ret string) {
|
||||
*depth++
|
||||
if 7 < *depth {
|
||||
return ""
|
||||
}
|
||||
if ast.NodeBlockRefID == node.Type {
|
||||
id := node.TokensStr()
|
||||
var ok bool
|
||||
if ret, ok = (*anchors)[id]; ok {
|
||||
return ret
|
||||
}
|
||||
|
||||
if gulu.Str.Contains(id, *stack) {
|
||||
return ""
|
||||
}
|
||||
|
||||
defBlock := GetBlock(id)
|
||||
if nil == defBlock {
|
||||
return "block not found"
|
||||
}
|
||||
|
||||
if "" != defBlock.Name {
|
||||
(*anchors)[id] = defBlock.Name
|
||||
return defBlock.Name
|
||||
}
|
||||
|
||||
if "d" == defBlock.Type {
|
||||
(*anchors)[id] = defBlock.Content
|
||||
return defBlock.Content
|
||||
}
|
||||
|
||||
tree := parse.Parse("", gulu.Str.ToBytes(defBlock.Markdown), luteEngine.ParseOptions)
|
||||
c := treenode.FirstLeafBlock(tree.Root)
|
||||
*stack = append(*stack, id)
|
||||
ret = resolveRefContent0(c, anchors, depth, stack)
|
||||
(*anchors)[id] = ret
|
||||
return
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
buf.Grow(4096)
|
||||
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
switch n.Type {
|
||||
case ast.NodeDocument:
|
||||
buf.WriteString(n.IALAttr("title"))
|
||||
return ast.WalkStop
|
||||
case ast.NodeText, ast.NodeLinkText, ast.NodeLinkTitle, ast.NodeFileAnnotationRefText, ast.NodeFootnotesRef,
|
||||
ast.NodeCodeBlockCode, ast.NodeMathBlockContent:
|
||||
buf.Write(n.Tokens)
|
||||
case ast.NodeTextMark:
|
||||
if n.IsTextMarkType("tag") {
|
||||
buf.WriteByte('#')
|
||||
}
|
||||
buf.WriteString(n.Content())
|
||||
if n.IsTextMarkType("tag") {
|
||||
buf.WriteByte('#')
|
||||
}
|
||||
case ast.NodeBlockRef:
|
||||
if anchor := n.ChildByType(ast.NodeBlockRefText); nil != anchor {
|
||||
buf.WriteString(anchor.Text())
|
||||
return ast.WalkSkipChildren
|
||||
} else if anchor = n.ChildByType(ast.NodeBlockRefDynamicText); nil != anchor {
|
||||
buf.WriteString(anchor.Text())
|
||||
return ast.WalkSkipChildren
|
||||
}
|
||||
|
||||
defID := n.ChildByType(ast.NodeBlockRefID)
|
||||
anchor := resolveRefContent0(defID, anchors, depth, stack)
|
||||
(*anchors)[defID.TokensStr()] = anchor
|
||||
buf.WriteString(anchor)
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func fromTree(node *ast.Node, tree *parse.Tree) (blocks []*Block, spans []*Span, assets []*Asset, attributes []*Attribute) {
|
||||
rootID := tree.Root.ID
|
||||
boxID := tree.Box
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@ import (
|
|||
"github.com/emirpasic/gods/sets/hashset"
|
||||
"github.com/siyuan-note/eventbus"
|
||||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
|
|
@ -473,30 +472,6 @@ func upsertTree(tx *sql.Tx, tree *parse.Tree, context map[string]interface{}) (e
|
|||
if err = insertBlocks(tx, blocks, context); nil != err {
|
||||
return
|
||||
}
|
||||
anchors := map[string]string{}
|
||||
var refIDs []string
|
||||
for _, block := range blocks {
|
||||
if "" != block.Content {
|
||||
// content 不为空的话说明是定值,不需要解析引用内容
|
||||
continue
|
||||
}
|
||||
subTree := parse.Parse("", []byte(block.Markdown), luteEngine.ParseOptions)
|
||||
if nil == subTree {
|
||||
logging.LogErrorf("parse temp block [%s] failed: %s", block.ID, err)
|
||||
continue
|
||||
}
|
||||
if 0 < len(treenode.GetLegacyDynamicBlockRefDefIDs(subTree.Root)) {
|
||||
refIDs = append(refIDs, block.ID)
|
||||
}
|
||||
}
|
||||
// 先删除再插入会快很多
|
||||
refBlocks := GetBlocks(refIDs)
|
||||
for _, refBlock := range refBlocks {
|
||||
blockContent := ResolveRefContent(refBlock, &anchors)
|
||||
refBlock.Content = blockContent
|
||||
}
|
||||
deleteBlocksByIDs(tx, refIDs)
|
||||
insertBlocks(tx, refBlocks, context)
|
||||
|
||||
refs, fileAnnotationRefs := refsFromTree(tree)
|
||||
if err = insertRefs(tx, refs); nil != err {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue