🐛 中西文间插入空格影响行级标记符 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

File diff suppressed because one or more lines are too long

View file

@ -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.20220623112232-c502d9016360
github.com/88250/lute v1.7.4-0.20220624073109-a79c7a5c9c9d
github.com/88250/lute v1.7.4-0.20220628102902-e32443650b1c
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1
github.com/88250/pdfcpu v0.3.13
github.com/88250/protyle v0.0.0-20220519012506-0a2c8dc24397

View file

@ -51,8 +51,8 @@ github.com/88250/gulu v1.2.0/go.mod h1:ZhEJ98UjR2y7j2toGj4/b+1rRELcZFQAPq/Yjyin2
github.com/88250/gulu v1.2.3-0.20220623112232-c502d9016360 h1:afQ0cjIA/tzwvIDFy9Jf0jFCb1FvWwKuG1QidEMMi4M=
github.com/88250/gulu v1.2.3-0.20220623112232-c502d9016360/go.mod h1:I1qBzsksFL2ciGSuqDE7R3XW4BUMrfDgOvSXEk7FsAI=
github.com/88250/lute v1.7.4-0.20220426011157-34c9bfa2e148/go.mod h1:Bdu9LRNjQhtL3TftbtpjIWTwDVAXoS7AD8QsZQPk7zo=
github.com/88250/lute v1.7.4-0.20220624073109-a79c7a5c9c9d h1:nGN2jBHT5fMnzCBrUsXqTDA+hAsCudvxvXdNbf1sZDc=
github.com/88250/lute v1.7.4-0.20220624073109-a79c7a5c9c9d/go.mod h1:Bdu9LRNjQhtL3TftbtpjIWTwDVAXoS7AD8QsZQPk7zo=
github.com/88250/lute v1.7.4-0.20220628102902-e32443650b1c h1:lM3ChAFl8eNrW08JNwKOWnsFjqiX9sl+y7B2rfdDynE=
github.com/88250/lute v1.7.4-0.20220628102902-e32443650b1c/go.mod h1:Bdu9LRNjQhtL3TftbtpjIWTwDVAXoS7AD8QsZQPk7zo=
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1 h1:9Cb+iN639vUI2OcIBc+4oGwml9/0J6bL6dWNb8Al+1s=
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1/go.mod h1:jH6MMPr8G7AMzaVmWHXZQiB1DKO3giWbcWZ7UoJ1teI=
github.com/88250/pdfcpu v0.3.13 h1:touMWMZkCGalMIbEg9bxYp7rETM+zwb9hXjwhqi4I7Q=

View file

@ -110,7 +110,7 @@ func GetBlockKramdown(id string) (ret string) {
return
}
addBlockIALNodes(tree)
addBlockIALNodes(tree, false)
node := treenode.GetNodeInTree(tree, id)
luteEngine := NewLute()
ret, _ = lute.FormatNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)

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 entering {
switch n.Type {
case ast.NodeStrong, ast.NodeEmphasis, ast.NodeStrikethrough, ast.NodeUnderline:
luteEngine.MergeSameSpan(n, n.Type)
}
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 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()

View file

@ -95,8 +95,7 @@ func DocSaveAsTemplate(id string, overwrite bool) (code int, err error) {
return
}
// 添加 block ial后面格式化渲染需要
addBlockIALNodes(tree)
addBlockIALNodes(tree, true)
luteEngine := NewLute()
formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
@ -259,7 +258,7 @@ func appendRefTextRenderResultForBlockRef(blockRef *ast.Node) {
blockRef.AppendChild(&ast.Node{Type: ast.NodeBlockRefDynamicText, Tokens: gulu.Str.ToBytes(text)})
}
func addBlockIALNodes(tree *parse.Tree) {
func addBlockIALNodes(tree *parse.Tree, removeUpdated bool) {
var blocks []*ast.Node
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering || !n.IsBlock() {
@ -272,7 +271,7 @@ func addBlockIALNodes(tree *parse.Tree) {
}
} else if ast.NodeHTMLBlock == n.Type {
n.Tokens = bytes.TrimSpace(n.Tokens)
// 使用 <div> 包裹,否则后续解析模板时会识别为行级 HTML https://github.com/siyuan-note/siyuan/issues/4244
// 使用 <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...)
}
@ -281,7 +280,9 @@ func addBlockIALNodes(tree *parse.Tree) {
}
}
if removeUpdated {
n.RemoveIALAttr("updated")
}
if 0 < len(n.KramdownIAL) {
blocks = append(blocks, n)
}