Skip to content

Commit

Permalink
🎨 Improve HTML table clipping siyuan-note/siyuan#11783
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 22, 2024
1 parent d71a0f3 commit 13f09e5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
45 changes: 45 additions & 0 deletions h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,51 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
if nil == n.FirstChild {
break
}

tbodys := util.DomChildrenByType(n.Parent, atom.Tbody)
if 0 < len(tbodys) {
tbody := tbodys[0]
// 找到最多的 td 数
var tdCount int
for tr := tbody.FirstChild; nil != tr; tr = tr.NextSibling {
if atom.Tr != tr.DataAtom {
continue
}

var count int
for td := tr.FirstChild; nil != td; td = td.NextSibling {
if atom.Td == td.DataAtom {
count++
}
}

if count > tdCount {
tdCount = count
}
}

// 补全 thead 中 tr 的 th
for tr := n.FirstChild; nil != tr; tr = tr.NextSibling {
if atom.Tr != tr.DataAtom {
continue
}

var count int
for td := tr.FirstChild; nil != td; td = td.NextSibling {
if atom.Th == td.DataAtom {
count++
}
}

if count < tdCount {
for i := count; i < tdCount; i++ {
th := &html.Node{Data: "th", DataAtom: atom.Th, Type: html.ElementNode}
tr.AppendChild(th)
}
}
}
}

node.Type = ast.NodeTableHead
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
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{

{"159", "<table id=\"tablepress-74\" class=\"tablepress tablepress-id-74\">\n<thead>\n<tr class=\"row-1 odd\">\n<th colspan=\"2\" class=\"column-1\">\n<p style=\"text-align: center;\">&nbsp;<strong>以图搜图</strong> - <a href=\"https://www.runningcheese.cn/s13\" rel=\"noopener\" target=\"_blank\"><strong>详细</strong> <img decoding=\"async\" class=\"ico\" src=\"https://www.runningcheese.com/icons/open.svg\"></a></p>\n</th>\n</tr>\n</thead>\n<tbody class=\"row-hover\">\n<tr class=\"row-2 even\">\n<td class=\"column-1\"><img decoding=\"async\" class=\"ico\" src=\"https://www.runningcheese.com/icons/menu.svg\">&nbsp;<strong>通用搜索</strong></td>\n<td class=\"column-2\"><strong>简介</strong></td>\n</tr>\n<tr class=\"row-3 odd\">\n<td class=\"column-1\"><img decoding=\"async\" class=\"ico\" src=\"https://www.runningcheese.com/icons/web.svg\">&nbsp;<a href=\"https://images.google.com/\">Google Images</a></td>\n<td class=\"column-2\">国外第一,可能是最好用的。👍</td>\n</tr>\n</tbody>\n</table>", "| **以图搜图**-[**详细** ![](https://www.runningcheese.com/icons/open.svg)](https://www.runningcheese.cn/s13) | |\n| --------------------------------------------------------------------------------------------------------------------- | ------------------------------ |\n| ![](https://www.runningcheese.com/icons/menu.svg) **通用搜索** | **简介** |\n| ![](https://www.runningcheese.com/icons/web.svg) [Google Images](https://images.google.com/) | 国外第一,可能是最好用的。👍 |\n"},
{"158", "<span style=\"outline: 0px;font-size: 17px;\"><strong style=\"outline: 0px;\"><span style=\"outline: 0px;font-family: Optima-Regular, PingFangTC-light;\">&nbsp;&nbsp;</span></strong><strong style=\"outline: 0px;\"><span style=\"font-size: 17px;outline: 0px;font-family: Optima-Regular, PingFangTC-light;\">梦想的起点</span></strong><strong style=\"outline: 0px;\"></strong></span>", "**\u200b 梦想的起点**\n"},
{"157", "<figure data-size=\"normal\"><noscript><img src=\"https://picx.zhimg.com/50/v2-fa19ad5cf46134b3d568236bd95f8ee8_720w.jpg?source=2c26e567\" data-size=\"normal\" data-rawwidth=\"1113\" data-rawheight=\"731\" data-original-token=\"v2-fa19ad5cf46134b3d568236bd95f8ee8\" data-default-watermark-src=\"https://pic1.zhimg.com/50/v2-ce5892149198b4890c626cb5fce3fe74_720w.jpg?source=2c26e567\" class=\"origin_image zh-lightbox-thumb\" width=\"1113\" data-original=\"https://pica.zhimg.com/v2-fa19ad5cf46134b3d568236bd95f8ee8_r.jpg?source=2c26e567\"/></noscript><div><img src=\"https://picx.zhimg.com/80/v2-fa19ad5cf46134b3d568236bd95f8ee8_720w.webp?source=2c26e567\" data-size=\"normal\" data-rawwidth=\"1113\" data-rawheight=\"731\" data-original-token=\"v2-fa19ad5cf46134b3d568236bd95f8ee8\" data-default-watermark-src=\"https://pic1.zhimg.com/50/v2-ce5892149198b4890c626cb5fce3fe74_720w.jpg?source=2c26e567\" class=\"origin_image zh-lightbox-thumb lazy\" width=\"1113\" data-original=\"https://pica.zhimg.com/v2-fa19ad5cf46134b3d568236bd95f8ee8_r.jpg?source=2c26e567\" data-actualsrc=\"https://picx.zhimg.com/50/v2-fa19ad5cf46134b3d568236bd95f8ee8_720w.jpg?source=2c26e567\" height=\"731\" data-lazy-status=\"ok\"></div><figcaption>工具选型</figcaption></figure><p data-pid=\"yJnbdUed\">最后我选择是 Colube <i>( 20230907作者说即将改名为 Mefo ) </i>,这是一款类似 <span class=\"nolink\">Heptabase</span> 的<span><a class=\"RichContent-EntityWord css-b7erz1\" data-za-not-track-link=\"true\" data-paste-text=\"true\" href=\"https://www.zhihu.com/search?q=%E7%99%BD%E6%9D%BF%E7%AC%94%E8%AE%B0%E8%BD%AF%E4%BB%B6&amp;search_source=Entity&amp;hybrid_search_source=Entity&amp;hybrid_search_extra=%7B%22sourceType%22%3A%22answer%22%2C%22sourceId%22%3A3211660195%7D\" target=\"_blank\">白板笔记软件<svg width=\"10px\" height=\"10px\" viewBox=\"0 0 15 15\" class=\"css-1dvsrp\"><path d=\"M10.89 9.477l3.06 3.059a1 1 0 0 1-1.414 1.414l-3.06-3.06a6 6 0 1 1 1.414-1.414zM6 10a4 4 0 1 0 0-8 4 4 0 0 0 0 8z\" fill=\"currentColor\"></path></svg></a></span>,但相对而言有极大的价格优势,因此对我很有吸引力。</p>", "![](https://picx.zhimg.com/80/v2-fa19ad5cf46134b3d568236bd95f8ee8_720w.webp?source=2c26e567)\n\n工具选型\n\n最后我选择是 Colube \u200b*( 20230907 作者说即将改名为 Mefo ) \u200b*\u200b,这是一款类似 Heptabase 的[白板笔记软件](https://www.zhihu.com/search?q=%E7%99%BD%E6%9D%BF%E7%AC%94%E8%AE%B0%E8%BD%AF%E4%BB%B6&search_source=Entity&hybrid_search_source=Entity&hybrid_search_extra=%7B%22sourceType%22%3A%22answer%22%2C%22sourceId%22%3A3211660195%7D),但相对而言有极大的价格优势,因此对我很有吸引力。\n"},
{"156", "<p data-tool=\"mdnice编辑器\"><span><span role=\"presentation\" data-formula=\"w=[0, 1]\" data-formula-type=\"inline-equation\"></span></span>&nbsp;就是以向量的形式表示<span><span role=\"presentation\" data-formula=\"w_1=0, w_2=1\" data-formula-type=\"inline-equation\"></span></span>。现在,我们给这个神经元一个输入<span><span role=\"presentation\" data-formula=\"x=[2, 3]\" data-formula-type=\"inline-equation\"></span></span>。我们用点积来表示:</p>", "$w=[0, 1]$ 就是以向量的形式表示 $w_1=0, w_2=1$。现在,我们给这个神经元一个输入 $x=[2, 3]$。我们用点积来表示:\n"},
Expand Down

0 comments on commit 13f09e5

Please sign in to comment.