♻️ 移除旧版中的行级元素实现代码 https://github.com/siyuan-note/siyuan/issues/6819

This commit is contained in:
Liang Ding 2022-12-08 20:32:42 +08:00
parent c69983c56c
commit 637f1427e4
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
11 changed files with 24 additions and 322 deletions

View File

@ -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}

View File

@ -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 {

View File

@ -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 {

View File

@ -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)

View File

@ -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 {

View File

@ -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
})

View File

@ -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]
}
}

View File

@ -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
})

View File

@ -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

View File

@ -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 {

View File

@ -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