mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-05 23:21:47 +01:00
🎨 新增获取块 kramdown 源代码的 API https://github.com/siyuan-note/siyuan/issues/5289
This commit is contained in:
parent
e1b45b40e5
commit
6723b3a20d
6 changed files with 121 additions and 30 deletions
|
|
@ -95,37 +95,8 @@ func DocSaveAsTemplate(id string, overwrite bool) (code int, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
var blocks []*ast.Node
|
||||
// 添加 block ial,后面格式化渲染需要
|
||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering || !n.IsBlock() {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if ast.NodeBlockQueryEmbed == n.Type {
|
||||
if script := n.ChildByType(ast.NodeBlockQueryEmbedScript); nil != script {
|
||||
script.Tokens = bytes.ReplaceAll(script.Tokens, []byte("\n"), []byte(" "))
|
||||
}
|
||||
} else if ast.NodeHTMLBlock == n.Type {
|
||||
n.Tokens = bytes.TrimSpace(n.Tokens)
|
||||
// 使用 <div> 包裹,否则后续解析模板时会识别为行级 HTML https://github.com/siyuan-note/siyuan/issues/4244
|
||||
if !bytes.HasPrefix(n.Tokens, []byte("<div>")) {
|
||||
n.Tokens = append([]byte("<div>\n"), n.Tokens...)
|
||||
}
|
||||
if !bytes.HasSuffix(n.Tokens, []byte("</div>")) {
|
||||
n.Tokens = append(n.Tokens, []byte("\n</div>")...)
|
||||
}
|
||||
}
|
||||
|
||||
n.RemoveIALAttr("updated")
|
||||
if 0 < len(n.KramdownIAL) {
|
||||
blocks = append(blocks, n)
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
for _, block := range blocks {
|
||||
block.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: parse.IAL2Tokens(block.KramdownIAL)})
|
||||
}
|
||||
addBlockIALNodes(tree)
|
||||
|
||||
luteEngine := NewLute()
|
||||
formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
|
||||
|
|
@ -287,3 +258,36 @@ func appendRefTextRenderResultForBlockRef(blockRef *ast.Node) {
|
|||
}
|
||||
blockRef.AppendChild(&ast.Node{Type: ast.NodeBlockRefDynamicText, Tokens: gulu.Str.ToBytes(text)})
|
||||
}
|
||||
|
||||
func addBlockIALNodes(tree *parse.Tree) {
|
||||
var blocks []*ast.Node
|
||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering || !n.IsBlock() {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if ast.NodeBlockQueryEmbed == n.Type {
|
||||
if script := n.ChildByType(ast.NodeBlockQueryEmbedScript); nil != script {
|
||||
script.Tokens = bytes.ReplaceAll(script.Tokens, []byte("\n"), []byte(" "))
|
||||
}
|
||||
} else if ast.NodeHTMLBlock == n.Type {
|
||||
n.Tokens = bytes.TrimSpace(n.Tokens)
|
||||
// 使用 <div> 包裹,否则后续解析模板时会识别为行级 HTML https://github.com/siyuan-note/siyuan/issues/4244
|
||||
if !bytes.HasPrefix(n.Tokens, []byte("<div>")) {
|
||||
n.Tokens = append([]byte("<div>\n"), n.Tokens...)
|
||||
}
|
||||
if !bytes.HasSuffix(n.Tokens, []byte("</div>")) {
|
||||
n.Tokens = append(n.Tokens, []byte("\n</div>")...)
|
||||
}
|
||||
}
|
||||
|
||||
n.RemoveIALAttr("updated")
|
||||
if 0 < len(n.KramdownIAL) {
|
||||
blocks = append(blocks, n)
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
for _, block := range blocks {
|
||||
block.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: parse.IAL2Tokens(block.KramdownIAL)})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue