Skip to content

Commit

Permalink
🎨 Improve HTML clipping siyuan-note/siyuan#13242
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 23, 2024
1 parent d4196fd commit 7cfd2f5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,9 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
if strings.Contains(class, "language-") {
language := class[strings.Index(class, "language-")+len("language-"):]
language = strings.Split(language, " ")[0]
node.LastChild.CodeBlockInfo = []byte(language)
if "fallback" != language && "chroma" != language {
node.LastChild.CodeBlockInfo = []byte(language)
}
} else {
if atom.Code == firstc.DataAtom && !span2Code {
class := util.DomAttrValue(firstc, "class")
Expand All @@ -474,7 +476,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {

if 1 > len(node.LastChild.CodeBlockInfo) {
class := util.DomAttrValue(n, "class")
if !strings.Contains(class, " ") {
if !strings.Contains(class, " ") && "fallback" != class && "chroma" != class {
node.LastChild.CodeBlockInfo = []byte(class)
}
}
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.

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{

{"203", "<div class=\"highlight\"><div class=\"chroma open\"><div class=\"code-header language-fallback\"><span class=\"code-title\"><i class=\"arrow fas fa-chevron-right fa-fw\" aria-hidden=\"true\"></i></span><span class=\"ellipses\"><i class=\"fas fa-ellipsis-h fa-fw\" aria-hidden=\"true\"></i></span><span class=\"copy\" data-clipboard-text=\"2020-08-29 支出 100 CNY ; 购买日用品\n\" title=\"Copy to clipboard\"><i class=\"far fa-copy fa-fw\" aria-hidden=\"true\"></i></span></div><div class=\"table-wrapper\"><table class=\"lntable\"><tbody><tr><td class=\"lntd\"><pre tabindex=\"0\" class=\"chroma\"><code><span class=\"lnt\">1\n</span></code></pre></td><td class=\"lntd\"><pre tabindex=\"0\" class=\"chroma\"><code class=\"language-fallback\" data-lang=\"fallback\"><span class=\"line\"><span class=\"cl\">2020-08-29 支出 100 CNY ; 购买日用品\n</span></span></code></pre></td></tr></tbody></table></div></div></div>", "```\n2020-08-29 支出 100 CNY ; 购买日用品\n```\n"},
{"202", "<p>foo<u>bar</u><b><u>baz</u></b>bazz</p>", "foo<u>bar</u>**<u>baz</u>**bazz\n"},
{"201", "<figure data-size=\"normal\"><div><div class=\"GifPlayer css-1isopsn\" data-size=\"normal\" data-za-detail-view-path-module=\"GifItem\"><img class=\"ztext-gif\" width=\"480\" role=\"presentation\" src=\"https://pica.zhimg.com/v2-454df793026c3018d1f047d9bbf2aefe_b.jpg\" data-thumbnail=\"https://pica.zhimg.com/v2-454df793026c3018d1f047d9bbf2aefe_b.jpg\" data-size=\"normal\" alt=\"动图封面\" style=\"display: block;\"><div class=\"GifPlayer-icon css-d39tw7\"><svg width=\"50\" height=\"50\" viewBox=\"0 0 60 60\" xmlns=\"http://www.w3.org/2000/svg\"><g stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\"><ellipse fill=\"#000\" opacity=\"0.45\" cx=\"30\" cy=\"30\" rx=\"30\" ry=\"30\"></ellipse><ellipse stroke=\"#FFF\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" opacity=\"0.5\" cx=\"30\" cy=\"30\" rx=\"26\" ry=\"26\"></ellipse></g><svg x=\"16\" y=\"18.5\"><g fill=\"#fff\"><path x=\"100\" y=\"100\" d=\"M12.8422852,12.9814453 L12.8422852,11.3994141 L7.63916016,11.3994141 L7.63916016,13.0517578 L10.9086914,13.0517578 L10.9086914,13.3242188 C10.8911133,15.2050781 9.46728516,16.4707031 7.39306641,16.4707031 C5.01123047,16.4707031 3.51708984,14.625 3.51708984,11.6367188 C3.51708984,8.70117188 5.00244141,6.84667969 7.34912109,6.84667969 C9.08056641,6.84667969 10.284668,7.68164062 10.7768555,9.2109375 L12.7543945,9.2109375 C12.3237305,6.64453125 10.2319336,5.00976562 7.34912109,5.00976562 C3.79833984,5.00976562 1.50439453,7.61132812 1.50439453,11.6542969 C1.50439453,15.75 3.77197266,18.3076172 7.36669922,18.3076172 C10.6889648,18.3076172 12.8422852,16.2246094 12.8422852,12.9814453 Z M17.5180664,18 L17.5180664,5.31738281 L15.5493164,5.31738281 L15.5493164,18 L17.5180664,18 Z M22.659668,18 L22.659668,12.7441406 L28.1088867,12.7441406 L28.1088867,11.0039062 L22.659668,11.0039062 L22.659668,7.11035156 L28.6098633,7.11035156 L28.6098633,5.31738281 L20.690918,5.31738281 L20.690918,18 L22.659668,18 Z\"></path></g></svg></svg></div></div></div></figure>", "![动图](https://pica.zhimg.com/v2-454df793026c3018d1f047d9bbf2aefe_b.webp)\n"},
{"200", "<section><span><strong><span>foo</span></strong></span></section>\n<section><span>bar</span></section>\n<section><span>baz</span></section>", "**foo**\n\nbar\n\nbaz\n"},
Expand Down
14 changes: 14 additions & 0 deletions vditor_wysiwyg.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,20 @@ func (lute *Lute) adjustTableCode(n *html.Node) {
continue
}

// 移除 <span class="lnt"> 格式的行号 https://github.com/siyuan-note/siyuan/issues/13242
spans := util.DomChildrenByType(td, atom.Span)
removed := false
for _, span := range spans {
if "lnt" == util.DomAttrValue(span, "class") {
removed = true
break
}
}
if removed {
unlinks = append(unlinks, td)
continue
}

if strings.Contains(tdClass, "code") {
if c := td.FirstChild; nil != c && atom.Div == c.DataAtom {
c.DataAtom = atom.Pre
Expand Down

0 comments on commit 7cfd2f5

Please sign in to comment.