Skip to content

Commit

Permalink
🎨 Protyle Improve Paste and Paste As Plain Text siyuan-note/siyua…
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed May 17, 2023
1 parent e7cf9ad commit ed6fca0
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 6 deletions.
8 changes: 8 additions & 0 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ func (n *Node) Content() (ret string) {
case NodeTextMark:
if "" != n.TextMarkTextContent {
if n.IsTextMarkType("code") {
// 搜索代码内容转义问题 https://github.com/siyuan-note/siyuan/issues/5927
buf.WriteString(html.UnescapeString(n.TextMarkTextContent))
} else {
buf.WriteString(n.TextMarkTextContent)
Expand All @@ -447,6 +448,13 @@ func (n *Node) Content() (ret string) {
return buf.String()
}

// EscapeMarkerContent 返回 n 及其所有内容子节点的文本值(其中的标记符会被转义),块级节点间通过换行符分隔。
func (n *Node) EscapeMarkerContent() (ret string) {
ret = n.Content()
ret = string(lex.EscapeProtyleMarkers([]byte(ret)))
return
}

func (n *Node) Stat() (runeCnt, wordCnt, linkCnt, imgCnt, refCnt int) {
buf := make([]byte, 0, 8192)
Walk(n, func(n *Node, entering bool) WalkStatus {
Expand Down
6 changes: 5 additions & 1 deletion h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
}
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte{194, 160}, []byte{' '}) // 将   转换为空格
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("\n"), []byte{' '}) // 将 \n 转换为空格 https://github.com/siyuan-note/siyuan/issues/6052
node.Tokens = lex.EscapeMarkers(node.Tokens)
if lute.ParseOptions.ProtyleWYSIWYG {
node.Tokens = lex.EscapeProtyleMarkers(node.Tokens)
} else {
node.Tokens = lex.EscapeMarkers(node.Tokens)
}
if nil != tree.Context.Tip && tree.Context.Tip.IsBlock() && nil != n.Parent && atom.Span != n.Parent.DataAtom && 1 > len(lex.TrimWhitespace(node.Tokens)) {
// 块级节点下非 span 包裹需要忽略空白
// 剪藏时列表下方块缩进不正确 https://github.com/siyuan-note/siyuan/issues/6650
Expand Down
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.

2 changes: 1 addition & 1 deletion lex/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func IsCommonInlineMarker(token byte) bool {

func IsProtyleInlineMarker(token byte) bool {
switch token {
case ItemAsterisk, ItemUnderscore, ItemBackslash, ItemBacktick, ItemTilde, ItemDollar, ItemEqual, ItemCaret:
case ItemAsterisk, ItemUnderscore, ItemBackslash, ItemBacktick, ItemTilde, ItemDollar, ItemEqual, ItemCaret, ItemLess, ItemGreater:
return true
default:
return false
Expand Down
5 changes: 5 additions & 0 deletions protyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ func (lute *Lute) BlockDOM2Content(htmlStr string) (text string) {
return tree.Root.Content()
}

func (lute *Lute) BlockDOM2EscapeMarkerContent(htmlStr string) (text string) {
tree := lute.BlockDOM2Tree(htmlStr)
return tree.Root.EscapeMarkerContent()
}

func (lute *Lute) Tree2BlockDOM(tree *parse.Tree, options *render.Options) (vHTML string) {
renderer := render.NewProtyleRenderer(tree, options)
output := renderer.Render()
Expand Down
1 change: 1 addition & 0 deletions test/h2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

var html2MdTests = []parseTest{

{"78", "foo $bar^2$ baz^3", "foo \\$bar^2\\$ baz^3\n"},
{"77", "<p>在做拉丁字母间距设计的时候,比较好的方法是从 <code class=\"hljs language-undefined\">n</code> 和 <code class=\"hljs language-undefined\">o</code> 起步。一旦这两个字母的边距设置合理,剩下的就相对简单了。所以,我建议你键入一些类似 <code class=\"hljs language-undefined\">noononno</code> 的组合,然后开始摆弄它们的边距设置,直到你满意为止。你可以在灰色的信息面板(显示 &gt; 显示信息,Cmd-Shift-I)中检查任意字符形的边距和字宽设置:</p>\n<figure><div class=\"RatioContainer\">\n<picture class=\"js-only\"><source data-srcset=\"https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-320x-q80.webp 320w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-640x-q80.webp 640w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-1280x-q80.webp 1280w\" type=\"image/webp\" sizes=\"360px\" srcset=\"https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-320x-q80.webp 320w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-640x-q80.webp 640w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-1280x-q80.webp 1280w\"><source data-srcset=\"https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-320x-q80.png 320w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-640x-q80.png 640w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-1280x-q80.png 1280w\" type=\"image/png\" sizes=\"360px\" srcset=\"https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-320x-q80.png 320w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-640x-q80.png 640w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-1280x-q80.png 1280w\"><img alt=\"\" class=\"lazyautosizes lazyloaded\" data-sizes=\"auto\" data-srcset=\"https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-320x-q80.png 320w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-640x-q80.png 640w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-1280x-q80.png 1280w\" height=\"64\" width=\"360\" sizes=\"360px\" srcset=\"https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-320x-q80.png 320w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-640x-q80.png 640w, https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-1280x-q80.png 1280w\"></picture>\n</div></figure>", "在做拉丁字母间距设计的时候,比较好的方法是从 `n` 和 `o` 起步。一旦这两个字母的边距设置合理,剩下的就相对简单了。所以,我建议你键入一些类似 `noononno` 的组合,然后开始摆弄它们的边距设置,直到你满意为止。你可以在灰色的信息面板(显示 > 显示信息,Cmd-Shift-I)中检查任意字符形的边距和字宽设置:\n\n![](https://cdn2.glyphsapp.com/media/pages/learn/spacing/6e6a8e08ef-1624987024/spacing-1-1280x-q80.png)\n"},
{"76", "<div>\n <ul>\n <li>foo</li>\n </ul>\n <h1>bar</h1>\n</div>", "* foo\n\n# bar\n"},
{"75", "foo <mark>bar</mark> baz", "foo ==bar== baz\n"},
Expand Down
32 changes: 32 additions & 0 deletions test/p2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,35 @@ func TestBlockDOM2Content(t *testing.T) {
}
}
}

var blockDOM2EscapeMarkerContent = []parseTest{

{"1", "<div data-node-id=\"20230517211428-a5q3t4e\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20230517211428\"><div contenteditable=\"true\" spellcheck=\"false\">foo <span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"bar^2\" contenteditable=\"false\" class=\"render-node\"></span> baz^3</div><div class=\"protyle-attr\" contenteditable=\"false\">​</div></div>", "foo bar\\^2 baz\\^3"},
{"0", "<div data-node-id=\"20230517201616-1i9a98t\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20230517201616\"><div contenteditable=\"true\" spellcheck=\"false\">Since <span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"0 \\leqq s-r&lt;d\" contenteditable=\"false\" class=\"render-node\"></span> we must have <span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"s-r=0\" contenteditable=\"false\" class=\"render-node\"></span>. The cyclic subgroup generated by <span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"a\" contenteditable=\"false\" class=\"render-node\"></span> has order <span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"d\" contenteditable=\"false\" class=\"render-node\"></span>.</div><div class=\"protyle-attr\" contenteditable=\"false\">​</div></div>", "Since 0 \\\\leqq s-r\\<d we must have s-r\\=0. The cyclic subgroup generated by a has order d."},
}

func TestBlockDOM2EscapeMarkerContent(t *testing.T) {
luteEngine := lute.New()
luteEngine.SetProtyleWYSIWYG(true)
luteEngine.ParseOptions.Mark = true
luteEngine.ParseOptions.BlockRef = true
luteEngine.SetKramdownIAL(true)
luteEngine.ParseOptions.SuperBlock = true
luteEngine.SetLinkBase("/siyuan/0/测试笔记/")
luteEngine.SetTag(true)
luteEngine.SetSub(true)
luteEngine.SetSup(true)
luteEngine.SetGitConflict(true)
luteEngine.SetIndentCodeBlock(false)
luteEngine.SetEmojiSite("http://127.0.0.1:6806/stage/protyle/images/emoji")
luteEngine.SetAutoSpace(true)
luteEngine.SetParagraphBeginningSpace(true)
luteEngine.SetFileAnnotationRef(true)

for _, test := range blockDOM2EscapeMarkerContent {
result := luteEngine.BlockDOM2EscapeMarkerContent(test.from)
if test.to != result {
t.Fatalf("test case [%s] failed\nexpected\n\t%q\ngot\n\t%q\noriginal html\n\t%q", test.name, test.to, result, test.from)
}
}
}

0 comments on commit ed6fca0

Please sign in to comment.