Skip to content

Commit

Permalink
✨ 行级元素支持多重样式类型 siyuan-note#2911
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 authored and leolee9086 committed Oct 4, 2022
1 parent efb2410 commit d78c470
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 85 deletions.
2 changes: 1 addition & 1 deletion app/stage/protyle/js/lute/lute.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion kernel/go.mod
Expand Up @@ -7,7 +7,7 @@ require (
github.com/88250/css v0.1.2
github.com/88250/flock v0.8.2
github.com/88250/gulu v1.2.3-0.20220916075322-eb117059d70a
github.com/88250/lute v1.7.5-0.20220916031248-441b66b72883
github.com/88250/lute v1.7.5-0.20220916090743-91ee79ea1ba7
github.com/88250/pdfcpu v0.3.13
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1
github.com/ConradIrwin/font v0.0.0-20210318200717-ce8d41cc0732
Expand Down
4 changes: 2 additions & 2 deletions kernel/go.sum
Expand Up @@ -19,8 +19,8 @@ github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5 h1:8HdZozCsXS
github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/88250/gulu v1.2.3-0.20220916075322-eb117059d70a h1:qQdnk8clbgA+MXtf5bXOTOby32iQYjqMOn6oBIMV/Tk=
github.com/88250/gulu v1.2.3-0.20220916075322-eb117059d70a/go.mod h1:I1qBzsksFL2ciGSuqDE7R3XW4BUMrfDgOvSXEk7FsAI=
github.com/88250/lute v1.7.5-0.20220916031248-441b66b72883 h1:7uCW8I6P5934DhzUpJmZcaanRl8ws+X76sw4uca4gRM=
github.com/88250/lute v1.7.5-0.20220916031248-441b66b72883/go.mod h1:cEoBGi0zArPqAsp0MdG9SKinvH/xxZZWXU7sRx8vHSA=
github.com/88250/lute v1.7.5-0.20220916090743-91ee79ea1ba7 h1:ddXXCU+Np3iJoFV34NShb/NwaSrnG+9ZGonxd5SdJow=
github.com/88250/lute v1.7.5-0.20220916090743-91ee79ea1ba7/go.mod h1:cEoBGi0zArPqAsp0MdG9SKinvH/xxZZWXU7sRx8vHSA=
github.com/88250/pdfcpu v0.3.13 h1:touMWMZkCGalMIbEg9bxYp7rETM+zwb9hXjwhqi4I7Q=
github.com/88250/pdfcpu v0.3.13/go.mod h1:S5YT38L/GCjVjmB4PB84PymA1qfopjEhfhTNQilLpv4=
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1 h1:48T899JQDwyyRu9yXHePYlPdHtpJfrJEUGBMH3SMBWY=
Expand Down
35 changes: 14 additions & 21 deletions kernel/model/export.go
Expand Up @@ -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
}
Expand All @@ -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)})
Expand Down Expand Up @@ -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)})
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kernel/model/file.go
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion kernel/model/history.go
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions kernel/model/path.go
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down
13 changes: 4 additions & 9 deletions kernel/model/template.go
Expand Up @@ -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) + "..."
}
Expand Down
43 changes: 23 additions & 20 deletions kernel/model/transaction.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions kernel/model/tree.go
Expand Up @@ -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
})

Expand All @@ -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
}
defID, _, _ := treenode.GetBlockRef(n)
if "" == defID {
return ast.WalkContinue
}
if "1" != refIDs[n.TokensStr()] {
n.Tokens = []byte(refIDs[n.TokensStr()])
if "1" != refIDs[defID] {
n.Tokens = []byte(refIDs[defID])
}
return ast.WalkContinue
})
Expand Down
4 changes: 2 additions & 2 deletions kernel/sql/cache.go
Expand Up @@ -112,8 +112,8 @@ func GetRefsCacheByDefID(defID string) (ret []*Ref) {

var defIDRefsCache = gcache.New(30*time.Minute, 5*time.Minute) // [defBlockID]map[refBlockID]*Ref

func CacheRef(tree *parse.Tree, refIDNode *ast.Node) {
ref := buildRef(tree, refIDNode)
func CacheRef(tree *parse.Tree, refNode *ast.Node) {
ref := buildRef(tree, refNode)
putRefCache(ref)
}

Expand Down
13 changes: 6 additions & 7 deletions kernel/sql/database.go
Expand Up @@ -269,7 +269,7 @@ func refsFromTree(tree *parse.Tree) (refs []*Ref, fileAnnotationRefs []*FileAnno
return ast.WalkContinue
}

if ast.NodeBlockRefID == n.Type {
if treenode.IsBlockRef(n) {
ref := buildRef(tree, n)
refs = append(refs, ref)
} else if ast.NodeFileAnnotationRefID == n.Type {
Expand Down Expand Up @@ -306,18 +306,17 @@ func refsFromTree(tree *parse.Tree) (refs []*Ref, fileAnnotationRefs []*FileAnno
return
}

func buildRef(tree *parse.Tree, refIDNode *ast.Node) *Ref {
markdown := treenode.FormatNode(refIDNode.Parent, luteEngine)
defBlockID := refIDNode.TokensStr()
func buildRef(tree *parse.Tree, refNode *ast.Node) *Ref {
markdown := treenode.FormatNode(refNode, luteEngine)
defBlockID, text, _ := treenode.GetBlockRef(refNode)
var defBlockParentID, defBlockRootID, defBlockPath string
defBlock := treenode.GetBlockTree(defBlockID)
if nil != defBlock {
defBlockParentID = defBlock.ParentID
defBlockRootID = defBlock.RootID
defBlockPath = defBlock.Path
}
text := treenode.GetDynamicBlockRefText(refIDNode.Parent)
parentBlock := treenode.ParentBlock(refIDNode)
parentBlock := treenode.ParentBlock(refNode)
return &Ref{
ID: ast.NewNodeID(),
DefBlockID: defBlockID,
Expand All @@ -330,7 +329,7 @@ func buildRef(tree *parse.Tree, refIDNode *ast.Node) *Ref {
Path: tree.Path,
Content: text,
Markdown: markdown,
Type: treenode.TypeAbbr(refIDNode.Type.String()),
Type: treenode.TypeAbbr(refNode.Type.String()),
}
}

Expand Down

0 comments on commit d78c470

Please sign in to comment.