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 7fb7ebc commit b994942
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
6 changes: 3 additions & 3 deletions javascript/lute.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion javascript/lute.min.js.map

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions lex/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,24 @@ func AddBackslashBeforePipe(content string) string {
if ItemPipe == b && ItemBackslash != last {
buf.WriteByte(ItemBackslash)
}
last = b
buf.WriteByte(b)
}
return buf.String()
}

func RepeatBackslashBeforePipe(content string) string {
buf := bytes.Buffer{}
var last byte
for i := 0; i < len(content); i++ {
b := content[i]
if ItemPipe == b {
if ItemBackslash != last {
buf.WriteByte(ItemBackslash)
}
buf.WriteByte(ItemBackslash)
}
last = b
buf.WriteByte(b)
}
return buf.String()
Expand Down
8 changes: 0 additions & 8 deletions parse/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ func NestedInlines2FlattedSpansHybrid(tree *Tree) {
if ast.NodeInlineMathContent == n.Type {
span.TextMarkTextContent = ""
span.TextMarkInlineMathContent = string(html.EscapeHTML(n.Tokens))
if n.ParentIs(ast.NodeTableCell) {
// 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 @@ -268,10 +264,6 @@ func NestedInlines2FlattedSpans(tree *Tree) {
if ast.NodeInlineMathContent == n.Type {
span.TextMarkTextContent = ""
span.TextMarkInlineMathContent = string(html.EscapeHTML(n.Tokens))
if n.ParentIs(ast.NodeTableCell) {
// 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
8 changes: 2 additions & 6 deletions render/protyle_export_md_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,7 @@ func (r *ProtyleExportMdRenderer) renderMdMarker(node *ast.Node, entering bool)
inlineMathContent := node.TextMarkInlineMathContent
if node.ParentIs(ast.NodeTableCell) {
// Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\\|", "|")
inlineMathContent = strings.ReplaceAll(inlineMathContent, "|", "\\|")
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\\", "\\\\")
inlineMathContent = lex.RepeatBackslashBeforePipe(inlineMathContent)
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\n", "<br/>")
}
ret += "$" + inlineMathContent + "$"
Expand Down Expand Up @@ -378,9 +376,7 @@ func (r *ProtyleExportMdRenderer) renderMdMarker0(node *ast.Node, currentTextmar
inlineMathContent := node.TextMarkInlineMathContent
if node.ParentIs(ast.NodeTableCell) {
// Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\\|", "|")
inlineMathContent = strings.ReplaceAll(inlineMathContent, "|", "\\|")
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\\", "\\\\")
inlineMathContent = lex.RepeatBackslashBeforePipe(inlineMathContent)
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\n", "<br/>")
}
ret += "$" + inlineMathContent
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
2 changes: 1 addition & 1 deletion test/protyle_export_md_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

var protyleExportMdTests = []parseTest{

{"9", "| $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"},
{"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"},
Expand Down

0 comments on commit b994942

Please sign in to comment.