Skip to content

Commit

Permalink
🐛 Protyle Unable to export inline formulas with italics siyuan-note/s…
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Dec 28, 2023
1 parent ad95487 commit d718662
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 105 deletions.
4 changes: 2 additions & 2 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.

11 changes: 11 additions & 0 deletions parse/inline_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,17 @@ func SetTextMarkNode(node *ast.Node, n *html.Node, options *Options) {
node.TextMarkType = dataType
node.Tokens = nil
types := strings.Split(dataType, " ")
// 重新排序,将 a、inline-memo、block-ref、file-annotation-ref、inline-math 放在最前面
var tmp []string
for i, typ := range types {
if "a" == typ || "inline-memo" == typ || "block-ref" == typ || "file-annotation-ref" == typ || "inline-math" == typ {
tmp = append(tmp, typ)
types = append(types[:i], types[i+1:]...)
break
}
}
types = append(tmp, types...)

isInlineMath := false
for _, typ := range types {
switch typ {
Expand Down
39 changes: 30 additions & 9 deletions render/protyle_export_md_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (r *ProtyleExportMdRenderer) renderAttributeView(node *ast.Node, entering b
}

func (r *ProtyleExportMdRenderer) renderTextMark(node *ast.Node, entering bool) ast.WalkStatus {
isStrongEm := node.IsTextMarkType("strong") || node.IsTextMarkType("em") || node.IsTextMarkType("s")
isStrongEm := node.ContainTextMarkTypes("strong", "em", "s") && !node.IsTextMarkType("inline-math")

if entering {
marker := r.renderMdMarker(node, entering)
Expand Down Expand Up @@ -266,23 +266,40 @@ func (r *ProtyleExportMdRenderer) renderMdMarker(node *ast.Node, entering bool)
return r.renderMdMarker0(node, types[0], entering)
}

// 重新排序,将 a、inline-memo、block-ref、file-annotation-ref、inline-math 放在最前面
var tmp []string
for i, typ := range types {
if "a" == typ || "inline-memo" == typ || "block-ref" == typ || "file-annotation-ref" == typ || "inline-math" == typ {
tmp = append(tmp, typ)
types = append(types[:i], types[i+1:]...)
break
}
}
types = append(tmp, types...)

typ := types[0]
if "a" == typ || "inline-memo" == typ || "block-ref" == typ || "file-annotation-ref" == typ || "inline-math" == typ {
types := types[1:]

if entering {
for _, typ := range types {
if "code" != typ {
ret += r.renderMdMarker1(node, typ, entering)
}
}

switch typ {
case "a":
href := node.TextMarkAHref
href = string(r.LinkPath([]byte(href)))
href = html.UnescapeHTMLStr(href)
href = r.EncodeLinkSpace(href)
ret += "["

for _, typ := range types {
ret += r.renderMdMarker1(node, typ, entering)
if "code" == typ {
ret += r.renderMdMarker1(node, typ, entering)
}
}

return
case "block-ref":
node.TextMarkTextContent = strings.ReplaceAll(node.TextMarkTextContent, "'", "'")
Expand Down Expand Up @@ -318,10 +335,6 @@ func (r *ProtyleExportMdRenderer) renderMdMarker(node *ast.Node, entering bool)
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
ret += "$" + content + "$"
}

for _, typ := range types {
ret += r.renderMdMarker1(node, typ, entering)
}
} else {
switch typ {
case "a":
Expand All @@ -331,14 +344,22 @@ func (r *ProtyleExportMdRenderer) renderMdMarker(node *ast.Node, entering bool)
href = r.EncodeLinkSpace(href)
ret += string(lex.EscapeProtyleMarkers([]byte(node.TextMarkTextContent)))
for _, typ := range types {
ret += r.renderMdMarker1(node, typ, entering)
if "code" == typ {
ret += r.renderMdMarker1(node, typ, entering)
}
}
ret += "](" + href
if "" != node.TextMarkATitle {
ret += " \"" + html.UnescapeHTMLStr(node.TextMarkATitle) + "\""
}
ret += ")"
}

for _, typ := range types {
if "code" != typ {
ret += r.renderMdMarker1(node, typ, entering)
}
}
}
} else {
if !entering {
Expand Down
1 change: 1 addition & 0 deletions test/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func TestBlockDOM2InlineBlockDOM(t *testing.T) {

var blockDOM2StdMd = []parseTest{

{"15", "foo <span data-type=\"em inline-math\" data-subtype=\"math\" data-content=\"bar\" contenteditable=\"false\" class=\"render-node\" data-render=\"true\"><span class=\"katex\"><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6595em;\"></span><span class=\"mord mathnormal\">bar</span></span></span></span></span> baz", "foo *$bar$* baz\n"},
{"14", "foo<span data-type=\"strong\">bar </span>bar", "foo**bar** bar\n"},
{"13", "<div data-node-id=\"20231028232041-5dhtaps\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20231028234123\"><div contenteditable=\"true\" spellcheck=\"false\">foo<span data-type=\"strong\">.bar</span></div><div class=\"protyle-attr\" contenteditable=\"false\">&ZeroWidthSpace;</div></div>", "foo **.bar**\n"},
{"12", "<div data-node-id=\"20231028232041-5dhtaps\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20231028234123\"><div contenteditable=\"true\" spellcheck=\"false\"><span data-type=\"strong\">foo.</span>bar</div><div class=\"protyle-attr\" contenteditable=\"false\">&ZeroWidthSpace;</div></div>", "**foo.** bar\n"},
Expand Down
12 changes: 6 additions & 6 deletions test/protyle_export_md_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ 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"},
{"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"},
{"4", "[~~\\~foo\\~foo\\~~~](bar)", "[~~\\~foo\\~foo\\~~~](bar)\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"3", "[*\\*foo\\*foo\\**](bar)", "[*\\*foo\\*foo\\**](bar)\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"2", "[**\\*foo\\*foo\\***](bar)", "[**\\*foo\\*foo\\***](bar)\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"},
{"4", "[~~\\~foo\\~foo\\~~~](bar)", "~~[\\~foo\\~foo\\~](bar)~~\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"3", "[*\\*foo\\*foo\\**](bar)", "*[\\*foo\\*foo\\*](bar)*\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"2", "[**\\*foo\\*foo\\***](bar)", "**[\\*foo\\*foo\\*](bar)**\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"1", "[`foo`](bar \"baz\")", "[`foo`](bar \"baz\")\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
{"0", "foo**bar**{: style=\"color: red;\"}baz", "foo**bar**{: style=\"color: red;\"}baz\n\n{: id=\"20060102150405-1a2b3c4\" updated=\"20060102150405\" type=\"doc\"}\n"},
}
Expand Down
Loading

0 comments on commit d718662

Please sign in to comment.