From c4f269de7172b15b1d44f6d73264d0e24e34d48b Mon Sep 17 00:00:00 2001
From: Daniel <845765@qq.com>
Date: Mon, 25 Sep 2023 21:44:24 +0800
Subject: [PATCH] :art: Protyle Improve the handling of inline-math containing
`|` in the table https://github.com/siyuan-note/siyuan/issues/9227
---
lex/token.go | 2 +-
parse/transform.go | 12 ++++++++++--
protyle.go | 6 +++---
test/m2p_test.go | 4 ++--
test/protyle_export_md_test.go | 4 ++--
5 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/lex/token.go b/lex/token.go
index a902af8cb9..dced97294b 100644
--- a/lex/token.go
+++ b/lex/token.go
@@ -413,7 +413,7 @@ func RepeatBackslashBeforePipe(content string) string {
if ItemBackslash != last {
buf.WriteByte(ItemBackslash)
}
- if 1 < backslashCnt {
+ if 1 <= backslashCnt {
buf.WriteByte(ItemBackslash)
}
}
diff --git a/parse/transform.go b/parse/transform.go
index 5587dd7423..756ba844db 100644
--- a/parse/transform.go
+++ b/parse/transform.go
@@ -19,7 +19,7 @@ import (
// NestedInlines2FlattedSpansHybrid 将嵌套的行级节点转换为平铺的文本标记节点。
// 该函数不会移除转义节点。
-func NestedInlines2FlattedSpansHybrid(tree *Tree) {
+func NestedInlines2FlattedSpansHybrid(tree *Tree, isExportMd bool) {
var unlinks []*ast.Node
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
@@ -89,6 +89,10 @@ func NestedInlines2FlattedSpansHybrid(tree *Tree) {
if ast.NodeInlineMathContent == n.Type {
span.TextMarkTextContent = ""
span.TextMarkInlineMathContent = string(html.EscapeHTML(n.Tokens))
+ if n.ParentIs(ast.NodeTableCell) && !isExportMd {
+ // Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
+ span.TextMarkInlineMathContent = strings.ReplaceAll(span.TextMarkInlineMathContent, "\\|", "|")
+ }
} else if ast.NodeBackslash == n.Type {
if c := n.ChildByType(ast.NodeBackslashContent); nil != c {
span.TextMarkTextContent = string(html.EscapeHTML(c.Tokens))
@@ -175,7 +179,7 @@ func NestedInlines2FlattedSpansHybrid(tree *Tree) {
// NestedInlines2FlattedSpans 将嵌套的行级节点转换为平铺的文本标记节点。
// 该函数会移除转义节点。
-func NestedInlines2FlattedSpans(tree *Tree) {
+func NestedInlines2FlattedSpans(tree *Tree, isExportMd bool) {
var unlinks []*ast.Node
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
@@ -264,6 +268,10 @@ func NestedInlines2FlattedSpans(tree *Tree) {
if ast.NodeInlineMathContent == n.Type {
span.TextMarkTextContent = ""
span.TextMarkInlineMathContent = string(html.EscapeHTML(n.Tokens))
+ if n.ParentIs(ast.NodeTableCell) && !isExportMd {
+ // Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
+ span.TextMarkInlineMathContent = strings.ReplaceAll(span.TextMarkInlineMathContent, "\\|", "|")
+ }
} else if ast.NodeBackslash == n.Type {
if c := n.ChildByType(ast.NodeBackslashContent); nil != c {
span.TextMarkTextContent = string(html.EscapeHTML(c.Tokens))
diff --git a/protyle.go b/protyle.go
index 1b400c6349..dc3981ca09 100644
--- a/protyle.go
+++ b/protyle.go
@@ -51,7 +51,7 @@ func (lute *Lute) SpinBlockDOM(ivHTML string) (ovHTML string) {
// 使用 Markdown 标记符嵌套行级元素后被还原为纯文本 https://github.com/siyuan-note/siyuan/issues/7637
// 这里需要将混合嵌套(比如 )的行级元素拆分为多个平铺的行级元素( 和 )
- parse.NestedInlines2FlattedSpansHybrid(tree)
+ parse.NestedInlines2FlattedSpansHybrid(tree, false)
ovHTML = lute.Tree2BlockDOM(tree, lute.RenderOptions)
return
@@ -120,7 +120,7 @@ func (lute *Lute) BlockDOM2InlineBlockDOM(vHTML string) (vIHTML string) {
func (lute *Lute) Md2BlockDOM(markdown string, reserveEmptyParagraph bool) (vHTML string) {
tree := parse.Parse("", []byte(markdown), lute.ParseOptions)
- parse.NestedInlines2FlattedSpans(tree)
+ parse.NestedInlines2FlattedSpans(tree, false)
if reserveEmptyParagraph {
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
@@ -149,7 +149,7 @@ func (lute *Lute) Md2BlockDOM(markdown string, reserveEmptyParagraph bool) (vHTM
func (lute *Lute) InlineMd2BlockDOM(markdown string) (vHTML string) {
tree := parse.Inline("", []byte(markdown), lute.ParseOptions)
- parse.NestedInlines2FlattedSpans(tree)
+ parse.NestedInlines2FlattedSpans(tree, false)
renderer := render.NewProtyleRenderer(tree, lute.RenderOptions)
for nodeType, rendererFunc := range lute.Md2BlockDOMRendererFuncs {
renderer.ExtRendererFuncs[nodeType] = rendererFunc
diff --git a/test/m2p_test.go b/test/m2p_test.go
index a642cdf11f..1c7669776c 100644
--- a/test/m2p_test.go
+++ b/test/m2p_test.go
@@ -22,8 +22,8 @@ var md2BlockDOMTests = []parseTest{
{"76", "| `foo\\\\\\|bar` |\n| - |", "\u200b\u200bfoo\\\\|bar\u200b |
---|
\u200b
"},
{"75", "| `foo\\\\|bar` |\n| - |", "\u200b\u200bfoo\\|bar\u200b |
---|
\u200b
"},
{"74", "| `foo\\|bar` |\n| - |", "\u200b\u200bfoo|bar\u200b |
---|
\u200b
"},
- {"73", "| $foo\\\\|bar$ |\n| -- |", ""},
- {"72", "| $foo\\|bar$ |\n| -- |", ""},
+ {"73", "| $foo\\\\|bar$ |\n| -- |", ""},
+ {"72", "| $foo\\|bar$ |\n| -- |", ""},
{"71", "1. {: id=\"20230920164903-bkt3zj4\" updated=\"20230920164903\"}bar\n{: id=\"20230920164903-tff56f8\" updated=\"20230920164903\"}\n2. {: id=\"20230920164903-apsgahk\" updated=\"20230920164903\"}baz\n{: id=\"20230920164903-t5xw86z\" updated=\"20230920164903\"}", ""},
{"70", "* [ ] foo\n\n * [ ] 1. bar\n 2. baz", ""},
{"69", "foo bar baz", ""},
diff --git a/test/protyle_export_md_test.go b/test/protyle_export_md_test.go
index 8d54db33d1..48ec7c8073 100644
--- a/test/protyle_export_md_test.go
+++ b/test/protyle_export_md_test.go
@@ -24,7 +24,7 @@ var protyleExportMdTests = []parseTest{
{"10", "| $foo\\\\\\|bar$ |\n| -- |", "|$foo\\\\\\\\|bar$|\n| -|\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"9", "| $foo\\\\|bar$ |\n| -- |", "|$foo\\\\\\|bar$|\n| -|\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
- {"8", "| $foo\\|bar$ |\n| -- |", "|$foo\\|bar$|\n| -|\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
+ {"8", "| $foo\\|bar$ |\n| -- |", "|$foo\\\\|bar$|\n| -|\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"7", "[~\\~foo\\~foo\\~~](bar)", "[~\\~foo\\~foo\\~~](bar)\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"6", "[^\\^foo\\^foo\\^^](bar)", "[^\\^foo\\^foo\\^^](bar)\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"5", "[==\\=foo\\=foo\\===](bar)", "[==\\=foo\\=foo\\===](bar)\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
@@ -57,7 +57,7 @@ func TestProtyleExportMd(t *testing.T) {
ast.Testing = true
for _, test := range protyleExportMdTests {
tree := parse.Parse("", []byte(test.from), luteEngine.ParseOptions)
- parse.NestedInlines2FlattedSpans(tree)
+ parse.NestedInlines2FlattedSpans(tree, true)
renderer := render.NewProtyleExportMdRenderer(tree, luteEngine.RenderOptions)
output := renderer.Render()
kmd := util.BytesToStr(output)