🐛 中西文间插入空格影响行级标记符 Fix https://github.com/siyuan-note/siyuan/issues/5308

This commit is contained in:
Liang Ding 2022-06-28 18:56:16 +08:00
parent 38a7d48eac
commit 1d10439bbb
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
6 changed files with 37 additions and 33 deletions

View file

@ -17,7 +17,6 @@
package model
import (
"bytes"
"os"
"path/filepath"
@ -41,35 +40,39 @@ func AutoSpace(rootID string) (err error) {
generateFormatHistory(tree)
var blocks []*ast.Node
var rootIAL [][]string
// 添加 block ial后面格式化渲染需要
luteEngine := NewLute()
// 合并相邻的同类行级节点
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || !n.IsBlock() {
return ast.WalkContinue
}
if ast.NodeDocument == n.Type {
rootIAL = n.KramdownIAL
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(" "))
if entering {
switch n.Type {
case ast.NodeStrong, ast.NodeEmphasis, ast.NodeStrikethrough, ast.NodeUnderline:
luteEngine.MergeSameSpan(n, n.Type)
}
}
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)})
// 合并相邻的文本节点
for {
var unlinks []*ast.Node
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if entering && ast.NodeText == n.Type && nil != n.Next && ast.NodeText == n.Next.Type {
n.Tokens = append(n.Tokens, n.Next.Tokens...)
unlinks = append(unlinks, n.Next)
}
return ast.WalkContinue
})
for _, n := range unlinks {
n.Unlink()
}
if 1 > len(unlinks) {
break
}
}
luteEngine := NewLute()
rootIAL := tree.Root.KramdownIAL
addBlockIALNodes(tree, false)
luteEngine.SetAutoSpace(true)
formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
md := formatRenderer.Render()