Skip to content

Commit

Permalink
🎨 Protyle Improve the handling of inline-math containing | in the t…
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Sep 25, 2023
1 parent 1b47323 commit c4f269d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lex/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func RepeatBackslashBeforePipe(content string) string {
if ItemBackslash != last {
buf.WriteByte(ItemBackslash)
}
if 1 < backslashCnt {
if 1 <= backslashCnt {
buf.WriteByte(ItemBackslash)
}
}
Expand Down
12 changes: 10 additions & 2 deletions parse/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions protyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (lute *Lute) SpinBlockDOM(ivHTML string) (ovHTML string) {

// 使用 Markdown 标记符嵌套行级元素后被还原为纯文本 https://github.com/siyuan-note/siyuan/issues/7637
// 这里需要将混合嵌套(比如 <strong><span a></span></strong>)的行级元素拆分为多个平铺的行级元素(<span strong> 和 <span strong a>)
parse.NestedInlines2FlattedSpansHybrid(tree)
parse.NestedInlines2FlattedSpansHybrid(tree, false)

ovHTML = lute.Tree2BlockDOM(tree, lute.RenderOptions)
return
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/m2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var md2BlockDOMTests = []parseTest{
{"76", "| `foo\\\\\\|bar` |\n| - |", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeTable\" class=\"table\" updated=\"20060102150405\"><div contenteditable=\"false\"><table contenteditable=\"true\" spellcheck=\"false\"><colgroup><col /></colgroup><thead><tr><th>\u200b<span data-type=\"code\">\u200bfoo\\\\&#124;bar</span>\u200b</th></tr></thead><tbody></tbody></table><div class=\"protyle-action__table\"><div class=\"table__resize\"></div><div class=\"table__select\"></div></div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"75", "| `foo\\\\|bar` |\n| - |", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeTable\" class=\"table\" updated=\"20060102150405\"><div contenteditable=\"false\"><table contenteditable=\"true\" spellcheck=\"false\"><colgroup><col /></colgroup><thead><tr><th>\u200b<span data-type=\"code\">\u200bfoo\\&#124;bar</span>\u200b</th></tr></thead><tbody></tbody></table><div class=\"protyle-action__table\"><div class=\"table__resize\"></div><div class=\"table__select\"></div></div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"74", "| `foo\\|bar` |\n| - |", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeTable\" class=\"table\" updated=\"20060102150405\"><div contenteditable=\"false\"><table contenteditable=\"true\" spellcheck=\"false\"><colgroup><col /></colgroup><thead><tr><th>\u200b<span data-type=\"code\">\u200bfoo&#124;bar</span>\u200b</th></tr></thead><tbody></tbody></table><div class=\"protyle-action__table\"><div class=\"table__resize\"></div><div class=\"table__select\"></div></div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"73", "| $foo\\\\|bar$ |\n| -- |", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeTable\" class=\"table\" updated=\"20060102150405\"><div contenteditable=\"false\"><table contenteditable=\"true\" spellcheck=\"false\"><colgroup><col /></colgroup><thead><tr><th><span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"foo\\\\&#124;bar\" contenteditable=\"false\" class=\"render-node\"></span></th></tr></thead><tbody></tbody></table><div class=\"protyle-action__table\"><div class=\"table__resize\"></div><div class=\"table__select\"></div></div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"72", "| $foo\\|bar$ |\n| -- |", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeTable\" class=\"table\" updated=\"20060102150405\"><div contenteditable=\"false\"><table contenteditable=\"true\" spellcheck=\"false\"><colgroup><col /></colgroup><thead><tr><th><span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"foo\\&#124;bar\" contenteditable=\"false\" class=\"render-node\"></span></th></tr></thead><tbody></tbody></table><div class=\"protyle-action__table\"><div class=\"table__resize\"></div><div class=\"table__select\"></div></div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"73", "| $foo\\\\|bar$ |\n| -- |", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeTable\" class=\"table\" updated=\"20060102150405\"><div contenteditable=\"false\"><table contenteditable=\"true\" spellcheck=\"false\"><colgroup><col /></colgroup><thead><tr><th><span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"foo\\&#124;bar\" contenteditable=\"false\" class=\"render-node\"></span></th></tr></thead><tbody></tbody></table><div class=\"protyle-action__table\"><div class=\"table__resize\"></div><div class=\"table__select\"></div></div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"72", "| $foo\\|bar$ |\n| -- |", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeTable\" class=\"table\" updated=\"20060102150405\"><div contenteditable=\"false\"><table contenteditable=\"true\" spellcheck=\"false\"><colgroup><col /></colgroup><thead><tr><th><span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"foo&#124;bar\" contenteditable=\"false\" class=\"render-node\"></span></th></tr></thead><tbody></tbody></table><div class=\"protyle-action__table\"><div class=\"table__resize\"></div><div class=\"table__select\"></div></div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"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\"}", "<div data-subtype=\"o\" data-node-id=\"20230920164903-tff56f8\" data-node-index=\"1\" data-type=\"NodeList\" class=\"list\" updated=\"20230920164903\"><div data-marker=\"1.\" data-subtype=\"o\" data-node-id=\"20230920164903-bkt3zj4\" data-type=\"NodeListItem\" class=\"li\" updated=\"20230920164903\"><div class=\"protyle-action protyle-action--order\" contenteditable=\"false\" draggable=\"true\">1.</div><div data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">bar</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div data-subtype=\"o\" data-node-id=\"20230920164903-t5xw86z\" data-node-index=\"2\" data-type=\"NodeList\" class=\"list\" updated=\"20230920164903\"><div data-marker=\"2.\" data-subtype=\"o\" data-node-id=\"20230920164903-apsgahk\" data-type=\"NodeListItem\" class=\"li\" updated=\"20230920164903\"><div class=\"protyle-action protyle-action--order\" contenteditable=\"false\" draggable=\"true\">2.</div><div data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">baz</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"70", "* [ ] foo\n\n * [ ] 1. bar\n 2. baz", "<div data-subtype=\"t\" data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeList\" class=\"list\" updated=\"20060102150405\"><div data-marker=\"*\" data-subtype=\"t\" data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeListItem\" class=\"li\" updated=\"20060102150405\"><div class=\"protyle-action protyle-action--task\" draggable=\"true\"><svg><use xlink:href=\"#iconUncheck\"></use></svg></div><div data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">foo</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div data-subtype=\"t\" data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeList\" class=\"list\" updated=\"20060102150405\"><div data-marker=\"*\" data-subtype=\"t\" data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeListItem\" class=\"li\" updated=\"20060102150405\"><div class=\"protyle-action protyle-action--task\" draggable=\"true\"><svg><use xlink:href=\"#iconUncheck\"></use></svg></div><div data-subtype=\"o\" data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeList\" class=\"list\" updated=\"20060102150405\"><div data-marker=\"1.\" data-subtype=\"o\" data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeListItem\" class=\"li\" updated=\"20060102150405\"><div class=\"protyle-action protyle-action--order\" contenteditable=\"false\" draggable=\"true\">1.</div><div data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">bar</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div data-marker=\"2.\" data-subtype=\"o\" data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeListItem\" class=\"li\" updated=\"20060102150405\"><div class=\"protyle-action protyle-action--order\" contenteditable=\"false\" draggable=\"true\">2.</div><div data-node-id=\"20060102150405-1a2b3c4\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">baz</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"69", "foo <span data-type=\"text\" style=\"background-color: var(--b3-card-error-background); color: var(--b3-card-error-color);\" custom-data=\"test\">bar</span> baz", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">foo <span data-type=\"text\" style=\"background-color: var(--b3-card-error-background); color: var(--b3-card-error-color);\" custom-data=\"test\">bar</span> baz</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
Expand Down
4 changes: 2 additions & 2 deletions test/protyle_export_md_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c4f269d

Please sign in to comment.