Skip to content

Commit

Permalink
🎨 Improve processing when copying and pasting plain text contains esc…
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 23, 2023
1 parent 7b296d1 commit 2357639
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 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.

23 changes: 22 additions & 1 deletion parse/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ func NestedInlines2FlattedSpansHybrid(tree *Tree, isExportMd bool) {
}

if ast.NodeLink == n.Type {
var unlinkBackslashes []*ast.Node
ast.Walk(n, func(c *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
}

if ast.NodeBackslash == c.Type {
cont := c.ChildByType(ast.NodeBackslashContent)
if nil != cont {
linkText := &ast.Node{Type: ast.NodeLinkText, Tokens: cont.Tokens}
c.InsertBefore(linkText)
}

unlinkBackslashes = append(unlinkBackslashes, c)
}
return ast.WalkContinue
})
for _, backslash := range unlinkBackslashes {
backslash.Unlink()
}

// 超链接嵌套图片情况下,图片子节点移到超链接节点前面
img := n.ChildByType(ast.NodeImage)
if nil == img {
Expand Down Expand Up @@ -79,7 +100,7 @@ func NestedInlines2FlattedSpansHybrid(tree *Tree, isExportMd bool) {
processNestedNode(n, "a", &tags, &unlinks, entering)
case ast.NodeBlockRef:
processNestedNode(n, "block-ref", &tags, &unlinks, entering)
case ast.NodeText, ast.NodeCodeSpanContent, ast.NodeInlineMathContent, ast.NodeLinkText, ast.NodeBlockRefID, ast.NodeHTMLEntity:
case ast.NodeText, ast.NodeCodeSpanContent, ast.NodeInlineMathContent, ast.NodeLinkText, ast.NodeBlockRefID, ast.NodeHTMLEntity, ast.NodeBackslash:
if 1 > len(tags) {
return ast.WalkContinue
}
Expand Down
4 changes: 2 additions & 2 deletions protyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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, false)
parse.NestedInlines2FlattedSpansHybrid(tree, false)
if reserveEmptyParagraph {
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
Expand Down Expand Up @@ -156,7 +156,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, false)
parse.NestedInlines2FlattedSpansHybrid(tree, false)
renderer := render.NewProtyleRenderer(tree, lute.RenderOptions)
for nodeType, rendererFunc := range lute.Md2BlockDOMRendererFuncs {
renderer.ExtRendererFuncs[nodeType] = rendererFunc
Expand Down
3 changes: 2 additions & 1 deletion test/m2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

var md2BlockDOMTests = []parseTest{

{"77", "\\*foo\\*", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\"><span data-type=\"backslash\"><span>\\</span>*</span>foo<span data-type=\"backslash\"><span>\\</span>*</span></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"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>"},
Expand All @@ -42,7 +43,7 @@ var md2BlockDOMTests = []parseTest{
{"56", "foo&lt;bar&gt;baz", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">foo&lt;bar&gt;baz</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"55", "foo\n{: style=\"color: green\"}\n\n{: style=\"color: green\"}\n\nbar", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" style=\"color: green\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">foo</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"2\" data-type=\"NodeParagraph\" class=\"p\" style=\"color: green\"><div contenteditable=\"true\" spellcheck=\"false\"></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div><div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"3\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">bar</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"54", "foo==bar==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=\"mark\">bar</span>baz</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"53", "foo\\\\\\~bar\\\\\\~baz", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">foo\\~bar\\~baz</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"53", "foo\\\\\\~bar\\\\\\~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=\"backslash\"><span>\\</span>\\</span><span data-type=\"backslash\"><span>\\</span>~</span>bar<span data-type=\"backslash\"><span>\\</span>\\</span><span data-type=\"backslash\"><span>\\</span>~</span>baz</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"52", "<div>\n<details>\n<summary>foo</summary>\n</div>\n{: id=\"20221022222200-2b6wj74\" updated=\"20221022222200\"}\n", "<div data-node-id=\"20221022222200-2b6wj74\" data-node-index=\"1\" data-type=\"NodeHTMLBlock\" class=\"render-node\" updated=\"20221022222200\" data-subtype=\"block\"><div class=\"protyle-icons\"><span class=\"b3-tooltips__nw b3-tooltips protyle-icon protyle-icon--first protyle-action__edit\"><svg><use xlink:href=\"#iconEdit\"></use></svg></span><span class=\"b3-tooltips__nw b3-tooltips protyle-icon protyle-action__menu protyle-icon--last\"><svg><use xlink:href=\"#iconMore\"></use></svg></span></div><div><protyle-html data-content=\"&lt;div&gt;\n&lt;details&gt;\n&lt;summary&gt;foo&lt;/summary&gt;\n&lt;/div&gt;\"></protyle-html><span style=\"position: absolute\">\u200b</span></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"51", "~~~foo~~~", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\"><span data-type=\"sub s\">foo</span></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"50", "#foo'bar#", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">\u200b<span data-type=\"tag\">\u200bfoo'bar</span>\u200b</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
Expand Down

0 comments on commit 2357639

Please sign in to comment.