Skip to content

Commit

Permalink
🎨 Improve HTML clipping siyuan-note/siyuan#13056
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 8, 2024
1 parent 1c970cf commit b4c30cb
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 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.

70 changes: 70 additions & 0 deletions parse/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,73 @@ func processNestedNode(n *ast.Node, tag string, tags *[]string, unlinks *[]*ast.
}
}
}

func TextMarks2Inlines(tree *Tree) {
inlines := func(content string) (ret []*ast.Node) {
subTree := Inline("", []byte(content), tree.Context.ParseOption)
for c := subTree.Root.FirstChild.FirstChild; nil != c; c = c.Next {
ret = append(ret, c)
}
return
}

ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
}

if ast.NodeTextMark == n.Type {
switch n.TextMarkType {
case "sup":
n.Type = ast.NodeSup
n.PrependChild(&ast.Node{Type: ast.NodeSupOpenMarker})
nodes := inlines(n.TextMarkTextContent)
for _, node := range nodes {
n.AppendChild(node)
}
n.AppendChild(&ast.Node{Type: ast.NodeSupCloseMarker})
case "sub":
n.Type = ast.NodeSub
n.PrependChild(&ast.Node{Type: ast.NodeSubOpenMarker})
nodes := inlines(n.TextMarkTextContent)
for _, node := range nodes {
n.AppendChild(node)
}
n.AppendChild(&ast.Node{Type: ast.NodeSubCloseMarker})
case "em":
n.Type = ast.NodeEmphasis
n.PrependChild(&ast.Node{Type: ast.NodeEmA6kOpenMarker})
nodes := inlines(n.TextMarkTextContent)
for _, node := range nodes {
n.AppendChild(node)
}
n.AppendChild(&ast.Node{Type: ast.NodeEmA6kCloseMarker})
case "strong":
n.Type = ast.NodeStrong
n.PrependChild(&ast.Node{Type: ast.NodeStrongA6kOpenMarker})
nodes := inlines(n.TextMarkTextContent)
for _, node := range nodes {
n.AppendChild(node)
}
n.AppendChild(&ast.Node{Type: ast.NodeStrongA6kCloseMarker})
case "mark":
n.Type = ast.NodeMark
n.PrependChild(&ast.Node{Type: ast.NodeMark2OpenMarker})
nodes := inlines(n.TextMarkTextContent)
for _, node := range nodes {
n.AppendChild(node)
}
n.AppendChild(&ast.Node{Type: ast.NodeMark2CloseMarker})
case "s":
n.Type = ast.NodeStrikethrough
n.PrependChild(&ast.Node{Type: ast.NodeStrikethrough2OpenMarker})
nodes := inlines(n.TextMarkTextContent)
for _, node := range nodes {
n.AppendChild(node)
}
n.AppendChild(&ast.Node{Type: ast.NodeStrikethrough2CloseMarker})
}
}
return ast.WalkContinue
})
}
4 changes: 4 additions & 0 deletions protyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (lute *Lute) Md2BlockDOM(markdown string, reserveEmptyParagraph bool) (vHTM

func (lute *Lute) Md2BlockDOMTree(markdown string, reserveEmptyParagraph bool) (vHTML string, tree *parse.Tree) {
tree = parse.Parse("", []byte(markdown), lute.ParseOptions)

// 先将 TextMark 转换为 Inlines https://github.com/siyuan-note/siyuan/issues/13056
parse.TextMarks2Inlines(tree)

parse.NestedInlines2FlattedSpansHybrid(tree, false)
if reserveEmptyParagraph {
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
Expand Down
1 change: 1 addition & 0 deletions test/h2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func TestHTML2Md(t *testing.T) {

var html2MdDisableSyntaxTests = []parseTest{

{"8", "<span>这两个语素最开始</span><sup data-text=\"大概是春秋战国音吧。\" data-url=\"\" data-numero=\"1\" data-draft-node=\"inline\" data-draft-type=\"reference\" data-tooltip=\"大概是春秋战国音吧。\" data-tooltip-richtext=\"1\" data-tooltip-preset=\"white\" data-tooltip-classname=\"ztext-reference-tooltip\"><a id=\"ref_1_0\" href=\"https://www.zhihu.com/question/2127166482#ref_1\" data-reference-link=\"true\" aria-labelledby=\"ref_1\">[1]</a></sup><span>都很拟声</span>", "这两个语素最开始<sup>[[1]](https://www.zhihu.com/question/2127166482#ref_1)</sup>都很拟声\n"},
{"7", "<strong><em>foo</em></strong> <em><strong>bar</strong></em>", "<strong><em>foo</em></strong> <em><strong>bar</strong></em>\n"},
{"6", "<kbd>foo</kbd>", "<kbd>foo</kbd>\n"},
{"5", "<mark>foo</mark>", "<mark>foo</mark>\n"},
Expand Down
1 change: 1 addition & 0 deletions test/m2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func TestMd2BlockDOM(t *testing.T) {

var md2BlockDOMDisableSyntaxTests = []parseTest{

{"9", "这两个语素最开始<sup>[[1]](https://www.zhihu.com/question/2127166482#ref_1)</sup>都很拟声\\n", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\"><div contenteditable=\"true\" spellcheck=\"false\">这两个语素最开始<span data-type=\"sup a\" data-href=\"https://www.zhihu.com/question/2127166482#ref_1\">[1]</span>都很拟声\\n</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"8", "<kbd>foo</kbd>", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\"><div contenteditable=\"true\" spellcheck=\"false\">\u200b<span data-type=\"kbd\">\u200bfoo</span>\u200b</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"7", "<mark>foo</mark>", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\"><div contenteditable=\"true\" spellcheck=\"false\"><span data-type=\"mark\">foo</span></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"6", "<u>foo</u>", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\"><div contenteditable=\"true\" spellcheck=\"false\"><span data-type=\"u\">foo</span></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
Expand Down

0 comments on commit b4c30cb

Please sign in to comment.