Skip to content

Commit

Permalink
🎨 Improve HTML clipping siyuan-note/siyuan#13307
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 30, 2024
1 parent 47c361d commit 638ecbd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
20 changes: 17 additions & 3 deletions h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,13 +818,27 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {

node.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: util.StrToBytes(src)})
var linkTitle string
if nil != n.NextSibling && atom.Figcaption == n.NextSibling.DataAtom {
linkTitle = util.DomText(n.NextSibling)
n.NextSibling.Unlink()

var figcaption *html.Node
for next := n.NextSibling; nil != next; next = next.NextSibling {
if html.TextNode == next.Type && "" == strings.TrimSpace(next.Data) {
continue
}

if atom.Figcaption == next.DataAtom {
figcaption = next
break
}
}

if nil != figcaption {
linkTitle = util.DomText(figcaption)
figcaption.Unlink()
}
if "" == linkTitle {
linkTitle = util.DomAttrValue(n, "title")
}
linkTitle = strings.TrimSpace(linkTitle)
if "" != linkTitle {
node.AppendChild(&ast.Node{Type: ast.NodeLinkSpace})
node.AppendChild(&ast.Node{Type: ast.NodeLinkTitle, Tokens: []byte(linkTitle)})
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{

{"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"},
{"206", "<p class=\"el-h4\"><h4 data-heading=\"foo\" dir=\"auto\" class=\"publish-article-heading\"><span class=\"heading-collapse-indicator collapse-indicator collapse-icon\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" class=\"svg-icon right-triangle\"><path d=\"M3 8L12 17L21 8\"></path></svg></span><br data-added-by-siyuan=\"true\">foo <span class=\"clickable-icon\" aria-label=\"Copy link\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" class=\"svg-icon lucide-link\"><path d=\"M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71\"></path><path d=\"M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71\"></path></svg></span><br data-added-by-siyuan=\"true\"></h4></p>", "#### foo\n"},
Expand Down

0 comments on commit 638ecbd

Please sign in to comment.