Skip to content

Commit

Permalink
🎨 Improve HTML clipping siyuan-note/siyuan#13353
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Dec 3, 2024
1 parent ac23909 commit 54b4e66
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
24 changes: 17 additions & 7 deletions h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,27 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
return
}

if atom.Div == firstc.DataAtom && nil != firstc.NextSibling && atom.Code == firstc.NextSibling.DataAtom {
firstc = firstc.NextSibling
n.FirstChild.Unlink()
codes := util.DomChildrenByType(n, atom.Code)
if 0 < len(codes) {
// 删除第一个 code 之前的标签
unlinks := []*html.Node{}
for prev := codes[0].PrevSibling; nil != prev; prev = prev.PrevSibling {
unlinks = append(unlinks, prev)
}
for _, unlink := range unlinks {
unlink.Unlink()
}
firstc = n.FirstChild
if nil == firstc {
return
}
}

if atom.Em == firstc.DataAtom && nil != firstc.NextSibling && atom.Em == firstc.NextSibling.DataAtom {
// pre.em,em,code 的情况,这两个 em 是“复制代码”和“隐藏代码” https://github.com/siyuan-note/siyuan/issues/13026
code := util.DomChildrenByType(n, atom.Code)
if 0 < len(code) {
firstc = code[0]

if 0 < len(codes) {
firstc = codes[0]
if nil != firstc {
unlinks := []*html.Node{}
for prev := firstc.PrevSibling; nil != prev; prev = prev.PrevSibling {
Expand All @@ -381,7 +392,6 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
}

if atom.Div == firstc.DataAtom && nil == firstc.NextSibling {
codes := util.DomChildrenByType(n, atom.Code)
if 1 == len(codes) {
code := codes[0]
// pre 下只有一个 div,且 div 下只有一个 code,那么将 pre.div 替换为 pre.code https://github.com/siyuan-note/siyuan/issues/11131
Expand Down
2 changes: 1 addition & 1 deletion 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: 2 additions & 0 deletions test/h2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

var html2MdTests = []parseTest{

{"211", "<pre><textarea></textarea><span class=\"copyTooltip\" style=\"display: inline;\"><img src=\"https://learnblockchain.cn/css/default/copy.svg\"></span><code class=\"language-go hljs\"><span class=\"hljs-keyword\">type</span> PurchaseOrder <span class=\"hljs-keyword\">struct</span> { \n Id <span class=\"hljs-type\">uint</span> <span class=\"hljs-string\">`json:\"id\" gorm:\"primaryKey\"`</span> \n UserId <span class=\"hljs-type\">string</span> <span class=\"hljs-string\">`json:\"userId\"`</span> \n Product []Product <span class=\"hljs-string\">`json:\"product\" gorm:\"foreignKey:Id\"`</span> \n Date time.Time <span class=\"hljs-string\">`json:\"date\"`</span> \n}\n</code></pre>", "```go\ntype PurchaseOrder struct { \n Id uint `json:\"id\" gorm:\"primaryKey\"` \n UserId string `json:\"userId\"` \n Product []Product `json:\"product\" gorm:\"foreignKey:Id\"` \n Date time.Time `json:\"date\"` \n}\n```\n"},
{"210", "<pre><textarea></textarea><code class=\"language-go hljs\">this is code</code></pre>", "```go\nthis is code\n```\n"},
{"209", "<figure> <img alt=\"c03f001.eps\" class=\"center\" src=\"https://www.w3schools.com/tags/pic_trulli.jpg\"> <figcaption><p><a id=\"figure3-1\" href=\"#figureanchor3-1\"><b>Figure 3-1:</b></a> If you try to design an automobile that pleases every possible driver, you end up with a car with every possible feature that pleases nobody. Software today is too often designed to please too many users, resulting in low user satisfaction. <a id=\"figureanchor3-2\" href=\"#figure3-2\">Figure 3-2</a> provides an alternative approach.</p></figcaption> </figure>", "![c03f001.eps](https://www.w3schools.com/tags/pic_trulli.jpg \"Figure 3-1: If you try to design an automobile that pleases every possible driver, you end up with a car with every possible feature that pleases nobody. Software today is too often designed to please too many users, resulting in low user satisfaction. Figure 3-2 provides an alternative approach.\")\n"},
{"208", "foo<h1>bar</h1>", "foo\n# bar\n"},
{"207", "foo<ul><li>bar<ul><li>baz</li></ul></li></ul>", "foo\n* bar\n * baz\n"},
Expand Down

0 comments on commit 54b4e66

Please sign in to comment.