From 637f1427e450e416c92b7de11d8154f1bf6e8f70 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Thu, 8 Dec 2022 20:32:42 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20=E7=A7=BB=E9=99=A4=E6=97=A7?= =?UTF-8?q?=E7=89=88=E4=B8=AD=E7=9A=84=E8=A1=8C=E7=BA=A7=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BB=A3=E7=A0=81=20https://github.com/siyua?= =?UTF-8?q?n-note/siyuan/issues/6819?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/export.go | 20 +----- kernel/model/import.go | 9 +-- kernel/model/index.go | 5 -- kernel/model/render.go | 5 +- kernel/model/template.go | 44 +++---------- kernel/model/transaction.go | 2 - kernel/model/tree.go | 4 +- kernel/sql/block_query.go | 13 +--- kernel/sql/database.go | 121 ------------------------------------ kernel/sql/upsert.go | 25 -------- kernel/treenode/node.go | 98 ++++------------------------- 11 files changed, 24 insertions(+), 322 deletions(-) diff --git a/kernel/model/export.go b/kernel/model/export.go index 9645c51ec..353b894a6 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -1100,15 +1100,6 @@ func exportTree(tree *parse.Tree, wysiwyg, expandKaTexMacros, keepFold bool) (re return ast.WalkContinue } } - case ast.NodeFileAnnotationRef: - refIDNode := n.ChildByType(ast.NodeFileAnnotationRefID) - if nil == refIDNode { - return ast.WalkSkipChildren - } - refID := refIDNode.TokensStr() - status := processFileAnnotationRef(refID, n) - unlinks = append(unlinks, n) - return status } if !treenode.IsBlockRef(n) { @@ -1534,16 +1525,7 @@ func processFileAnnotationRef(refID string, n *ast.Node) ast.WalkStatus { page := int(pages[0].(map[string]interface{})["index"].(float64)) + 1 pageStr := strconv.Itoa(page) - var refText string - if ast.NodeTextMark == n.Type { - refText = n.TextMarkTextContent - } else { - refTextNode := n.ChildByType(ast.NodeFileAnnotationRefText) - if nil == refTextNode { - return ast.WalkSkipChildren - } - refText = refTextNode.TokensStr() - } + refText := n.TextMarkTextContent ext := filepath.Ext(p) file := p[7:len(p)-23-len(ext)] + ext fileAnnotationRefLink := &ast.Node{Type: ast.NodeLink} diff --git a/kernel/model/import.go b/kernel/model/import.go index 5abe655f9..6d767fd30 100644 --- a/kernel/model/import.go +++ b/kernel/model/import.go @@ -128,17 +128,12 @@ func ImportSY(zipPath, boxID, toPath string) (err error) { if !entering { return ast.WalkContinue } + if treenode.IsBlockRef(n) { defID, _, _ := treenode.GetBlockRef(n) newDefID := blockIDs[defID] if "" != newDefID { - if ast.NodeBlockRef == n.Type { - if id := n.ChildByType(ast.NodeBlockRefID); nil != id { - id.Tokens = []byte(newDefID) - } - } else { - n.TextMarkBlockRefID = newDefID - } + n.TextMarkBlockRefID = newDefID } } else if ast.NodeBlockQueryEmbedScript == n.Type { for oldID, newID := range blockIDs { diff --git a/kernel/model/index.go b/kernel/model/index.go index 3c0de1a54..3f0e64b3b 100644 --- a/kernel/model/index.go +++ b/kernel/model/index.go @@ -25,7 +25,6 @@ import ( "strings" "time" - "github.com/88250/lute/ast" "github.com/88250/lute/parse" "github.com/dustin/go-humanize" "github.com/emirpasic/gods/sets/hashset" @@ -251,10 +250,6 @@ func IndexRefs() { logging.LogInfof("resolved refs [%d] in [%dms]", len(refBlocks), time.Now().Sub(start).Milliseconds()) } -func isLegacyDynamicBlockRef(blockRef *ast.Node) bool { - return nil == blockRef.ChildByType(ast.NodeBlockRefText) && nil == blockRef.ChildByType(ast.NodeBlockRefDynamicText) -} - func init() { eventbus.Subscribe(eventbus.EvtSQLInsertBlocks, func(context map[string]interface{}, blockCount int, hash string) { if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container { diff --git a/kernel/model/render.go b/kernel/model/render.go index c5976452e..f8f9a03f0 100644 --- a/kernel/model/render.go +++ b/kernel/model/render.go @@ -46,10 +46,7 @@ func renderOutline(node *ast.Node, luteEngine *lute.Lute) (ret string) { return ast.WalkContinue } switch n.Type { - case ast.NodeBlockRef: - buf.WriteString(html.EscapeString(treenode.GetDynamicBlockRefText(n))) - return ast.WalkSkipChildren - case ast.NodeText, ast.NodeLinkText, ast.NodeFileAnnotationRefText, ast.NodeFootnotesRef, ast.NodeCodeBlockCode, ast.NodeMathBlockContent: + case ast.NodeText, ast.NodeLinkText, ast.NodeFootnotesRef, ast.NodeCodeBlockCode, ast.NodeMathBlockContent: tokens := html.EscapeHTML(n.Tokens) tokens = bytes.ReplaceAll(tokens, []byte(" "), []byte(" ")) // 大纲面板条目中无法显示多个空格 https://github.com/siyuan-note/siyuan/issues/4370 buf.Write(tokens) diff --git a/kernel/model/template.go b/kernel/model/template.go index 8fa3e665b..111916218 100644 --- a/kernel/model/template.go +++ b/kernel/model/template.go @@ -20,15 +20,6 @@ import ( "bytes" "errors" "fmt" - "io/fs" - "os" - "path/filepath" - "sort" - "strings" - "text/template" - "time" - "unicode/utf8" - "github.com/88250/lute/ast" "github.com/88250/lute/parse" "github.com/88250/lute/render" @@ -36,6 +27,13 @@ import ( "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/treenode" "github.com/siyuan-note/siyuan/kernel/util" + "io/fs" + "os" + "path/filepath" + "sort" + "strings" + "text/template" + "time" "github.com/88250/gulu" sprig "github.com/Masterminds/sprig/v3" @@ -224,15 +222,6 @@ func renderTemplate(p, id string) (string, error) { unlinks = append(unlinks, n) } } - } else if ast.NodeBlockRef == n.Type { - if idNode := n.ChildByType(ast.NodeBlockRefID); nil != idNode { - refText := sql.GetRefText(idNode.TokensStr()) - if "" != refText { - treenode.SetDynamicBlockRefText(n, refText) - } else { - unlinks = append(unlinks, n) - } - } } return ast.WalkContinue }) @@ -260,25 +249,6 @@ func renderTemplate(p, id string) (string, error) { return dom, nil } -func appendRefTextRenderResultForBlockRef(blockRef *ast.Node) { - if !treenode.IsBlockRef(blockRef) { - return - } - - refID, text, _ := treenode.GetBlockRef(blockRef) - if "" != text { - return - } - - // 动态解析渲染 ((id)) 的锚文本 - // 现行版本已经不存在该语法情况,这里保留是为了迁移历史数据 - text = sql.GetRefText(refID) - if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(text) { - text = gulu.Str.SubStr(text, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..." - } - blockRef.AppendChild(&ast.Node{Type: ast.NodeBlockRefDynamicText, Tokens: gulu.Str.ToBytes(text)}) -} - func addBlockIALNodes(tree *parse.Tree, removeUpdated bool) { var blocks []*ast.Node ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go index fd2c69ac9..b2104ad1e 100644 --- a/kernel/model/transaction.go +++ b/kernel/model/transaction.go @@ -909,8 +909,6 @@ func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) { } } } - } else if ast.NodeBlockRef == n.Type { - sql.CacheRef(subTree, n) } return ast.WalkContinue }) diff --git a/kernel/model/tree.go b/kernel/model/tree.go index 16f2708b6..35b1a9f2f 100644 --- a/kernel/model/tree.go +++ b/kernel/model/tree.go @@ -91,9 +91,7 @@ func resetTree(tree *parse.Tree, titleSuffix string) { return ast.WalkContinue } if "1" != refIDs[defID] { - if ast.NodeBlockRefID == n.Type { - n.Tokens = []byte(refIDs[defID]) - } else if ast.NodeTextMark == n.Type { + if ast.NodeTextMark == n.Type { n.TextMarkBlockRefID = refIDs[defID] } } diff --git a/kernel/sql/block_query.go b/kernel/sql/block_query.go index 30730e395..134d4f956 100644 --- a/kernel/sql/block_query.go +++ b/kernel/sql/block_query.go @@ -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 }) diff --git a/kernel/sql/database.go b/kernel/sql/database.go index f5f589287..75aaa8d38 100644 --- a/kernel/sql/database.go +++ b/kernel/sql/database.go @@ -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 diff --git a/kernel/sql/upsert.go b/kernel/sql/upsert.go index 843f180bc..24d2c7049 100644 --- a/kernel/sql/upsert.go +++ b/kernel/sql/upsert.go @@ -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 { diff --git a/kernel/treenode/node.go b/kernel/treenode/node.go index b1dd3bedd..ab3d94bb7 100644 --- a/kernel/treenode/node.go +++ b/kernel/treenode/node.go @@ -36,30 +36,10 @@ func GetBlockRef(n *ast.Node) (blockRefID, blockRefText, blockRefSubtype string) if !IsBlockRef(n) { return } - if ast.NodeBlockRef == n.Type { - id := n.ChildByType(ast.NodeBlockRefID) - if nil == id { - return - } - blockRefID = id.TokensStr() - text := n.ChildByType(ast.NodeBlockRefText) - if nil != text { - blockRefText = text.Text() - blockRefSubtype = "s" - return - } - text = n.ChildByType(ast.NodeBlockRefDynamicText) - if nil != text { - blockRefText = text.Text() - blockRefSubtype = "d" - return - } - } - if ast.NodeTextMark == n.Type { - blockRefID = n.TextMarkBlockRefID - blockRefText = n.TextMarkTextContent - blockRefSubtype = n.TextMarkBlockRefSubtype - } + + blockRefID = n.TextMarkBlockRefID + blockRefText = n.TextMarkTextContent + blockRefSubtype = n.TextMarkBlockRefSubtype return } @@ -67,13 +47,7 @@ func IsBlockRef(n *ast.Node) bool { if nil == n { return false } - if ast.NodeBlockRef == n.Type { - return true - } - if ast.NodeTextMark == n.Type { - return n.IsTextMarkType("block-ref") - } - return false + return ast.NodeTextMark == n.Type && n.IsTextMarkType("block-ref") } func NodeStaticMdContent(node *ast.Node, luteEngine *lute.Lute) (md, content string) { @@ -126,10 +100,6 @@ func NodeStaticContent(node *ast.Node) string { } switch n.Type { - case ast.NodeBlockRef: - buf.WriteString(GetDynamicBlockRefText(n)) - lastSpace = false - return ast.WalkSkipChildren case ast.NodeLinkText: buf.Write(n.Tokens) buf.WriteByte(' ') @@ -138,7 +108,7 @@ func NodeStaticContent(node *ast.Node) string { buf.WriteByte(' ') case ast.NodeLinkTitle: buf.Write(n.Tokens) - case ast.NodeText, ast.NodeFileAnnotationRefText, ast.NodeFootnotesRef, ast.NodeCodeBlockCode, ast.NodeMathBlockContent, ast.NodeHTMLBlock: + case ast.NodeText, ast.NodeCodeBlockCode, ast.NodeMathBlockContent, ast.NodeHTMLBlock: tokens := n.Tokens if IsChartCodeBlockCode(n) { // 图表块的内容在数据库 `blocks` 表 `content` 字段中被转义 https://github.com/siyuan-note/siyuan/issues/6326 @@ -284,12 +254,11 @@ var typeAbbrMap = map[string]string{ "NodeVideo": "video", "NodeAudio": "audio", // 行级元素 TODO: 移除旧版中的行级元素实现代码 https://github.com/siyuan-note/siyuan/issues/6819 - "NodeText": "text", - "NodeImage": "img", - "NodeLinkText": "link_text", - "NodeLinkDest": "link_dest", - "NodeBlockRefID": "ref_id", - "NodeTextMark": "textmark", + "NodeText": "text", + "NodeImage": "img", + "NodeLinkText": "link_text", + "NodeLinkDest": "link_dest", + "NodeTextMark": "textmark", } var abbrTypeMap = map[string]string{} @@ -343,20 +312,6 @@ func SubTypeAbbr(n *ast.Node) string { return "" } -func GetLegacyDynamicBlockRefDefIDs(node *ast.Node) (ret []string) { - ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus { - if !entering { - return ast.WalkContinue - } - if ast.NodeBlockRefID == n.Type && ast.NodeCloseParen == n.Next.Type { - ret = append(ret, n.TokensStr()) - return ast.WalkSkipChildren - } - return ast.WalkContinue - }) - return -} - var DynamicRefTexts = sync.Map{} func SetDynamicBlockRefText(blockRef *ast.Node, refText string) { @@ -364,25 +319,6 @@ func SetDynamicBlockRefText(blockRef *ast.Node, refText string) { return } - if ast.NodeBlockRef == blockRef.Type { - idNode := blockRef.ChildByType(ast.NodeBlockRefID) - if nil == idNode { - return - } - - var spacesRefTexts []*ast.Node // 可能会有多个空格,或者遗留错误插入的锚文本节点,这里做一次订正 - for n := idNode.Next; ast.NodeCloseParen != n.Type; n = n.Next { - spacesRefTexts = append(spacesRefTexts, n) - } - for _, toRemove := range spacesRefTexts { - toRemove.Unlink() - } - refText = strings.TrimSpace(refText) - idNode.InsertAfter(&ast.Node{Type: ast.NodeBlockRefDynamicText, Tokens: []byte(refText)}) - idNode.InsertAfter(&ast.Node{Type: ast.NodeBlockRefSpace}) - return - } - blockRef.TextMarkBlockRefSubtype = "d" blockRef.TextMarkTextContent = refText @@ -390,18 +326,6 @@ func SetDynamicBlockRefText(blockRef *ast.Node, refText string) { DynamicRefTexts.Store(blockRef.TextMarkBlockRefID, refText) } -func GetDynamicBlockRefText(blockRef *ast.Node) string { - refText := blockRef.ChildByType(ast.NodeBlockRefText) - if nil != refText { - return refText.Text() - } - refText = blockRef.ChildByType(ast.NodeBlockRefDynamicText) - if nil != refText { - return refText.Text() - } - return "ref resolve failed" -} - func IsChartCodeBlockCode(code *ast.Node) bool { if nil == code.Previous || ast.NodeCodeBlockFenceInfoMarker != code.Previous.Type || 1 > len(code.Previous.CodeBlockInfo) { return false