Skip to content

Commit

Permalink
🎨 Improve web page clip code block content conversion siyuan-note/siy…
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Dec 15, 2023
1 parent 5ee5630 commit 899eaf6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
19 changes: 13 additions & 6 deletions h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,17 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
}
}

if atom.Code == n.FirstChild.DataAtom {
if nil != n.FirstChild.NextSibling && atom.Code == n.FirstChild.NextSibling.DataAtom {
if atom.Code == firstc.DataAtom {
if nil != firstc.NextSibling && atom.Code == firstc.NextSibling.DataAtom {
// pre.code code 每个 code 为一行的结构,需要在 code 中间插入换行
for c := n.FirstChild.NextSibling; nil != c; c = c.NextSibling {
for c := firstc.NextSibling; nil != c; c = c.NextSibling {
c.InsertBefore(&html.Node{DataAtom: atom.Br})
}
}
if nil != n.FirstChild.FirstChild && atom.Ol == n.FirstChild.FirstChild.DataAtom {
if nil != firstc.FirstChild && atom.Ol == firstc.FirstChild.DataAtom {
// CSDN 代码块:pre.code.ol.li
for li := n.FirstChild.FirstChild.FirstChild; nil != li; li = li.NextSibling {
if li != n.FirstChild.FirstChild.FirstChild {
for li := firstc.FirstChild.FirstChild; nil != li; li = li.NextSibling {
if li != firstc.FirstChild.FirstChild {
li.InsertBefore(&html.Node{DataAtom: atom.Br})
}
}
Expand All @@ -269,6 +269,13 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
}
}

if atom.Pre == firstc.DataAtom && nil != firstc.FirstChild {
// pre.code code 每个 code 为一行的结构,需要在 code 中间插入换行
for c := firstc.FirstChild.NextSibling; nil != c; c = c.NextSibling {
c.InsertBefore(&html.Node{DataAtom: atom.Br})
}
}

buf := &bytes.Buffer{}
buf.WriteString(util.DomText(n))
content := &ast.Node{Type: ast.NodeCodeBlockCode, Tokens: buf.Bytes()}
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: 1 addition & 1 deletion test/h2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

var html2MdTests = []parseTest{

{"82", "<pre class=\"mb-4 mt-6 max-h-[650px] overflow-x-auto rounded border bg-[#1e1e1e]\"><pre style=\"color:#9CDCFE;background-color:#1E1E1E\" class=\"px-4 py-4 prism-code language-\"><div class=\"token-line\" style=\"color:#9CDCFE\"><span class=\"token plain\">&lt;|im_start|&gt;John Smith\\</span></div></pre></pre>", "```\n<|im_start|>John Smith\\\n```\n"},
{"82", "<pre class=\"mb-4 mt-6 max-h-[650px] overflow-x-auto rounded border bg-[#1e1e1e]\"><pre style=\"color:#9CDCFE;background-color:#1E1E1E\" class=\"px-4 py-4 prism-code language-\"><div class=\"token-line\" style=\"color:#9CDCFE\"><span class=\"token plain\">foo</span></div><div class=\"token-line\" style=\"color:#9CDCFE\"><span class=\"token plain\">bar</span></div></pre></pre>", "```\nfoo\nbar\n```\n"},
{"81", "<pre data-tool=\"mdnice编辑器\" style=\"margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);text-align: left;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;\"><section style=\"padding: 15px 16px 16px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;background-color: rgb(40, 44, 52);border-radius: 5px;margin-top: 0px;\"><span style=\"letter-spacing: normal;font-size: 12px;\">setbit key offset value # 设置或者清空 key 的 value(字符串)在 offset 处的 bit 值<br>getbit key offset # 返回 key 对应的 string 在 offset 处的 bit 值 <br>bitcount key [start end] # start end 范围内被设置为1的数量,不传递 start end 默认全范围<br></span></section></pre>", "```\nsetbit key offset value # 设置或者清空 key 的 value(字符串)在 offset 处的 bit 值\ngetbit key offset # 返回 key 对应的 string 在 offset 处的 bit 值 \nbitcount key [start end] # start end 范围内被设置为1的数量,不传递 start end 默认全范围\n```\n"},
{"80", "<pre data-tool=\"mdnice编辑器\" style=\"margin-top: 10px;margin-bottom: 10px;color: rgb(0, 0, 0);text-align: left;border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;\"><section style=\"padding: 15px 16px 16px;overflow-x: auto;color: rgb(171, 178, 191);display: -webkit-box;font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace;background-color: rgb(40, 44, 52);border-radius: 5px;margin-top: 0px;\"><span style=\"letter-spacing: normal;font-size: 12px;\">object encoding xxx # xxx 为键名<br></span></section></pre>", "```\nobject encoding xxx # xxx 为键名\n```\n"},
{"79", "<p>foo <code class=\"docutils literal notranslate\"><span class=\"pre\">bar</span></code>) baz</p>", "foo `<span class=\"pre\">bar</span>`) baz\n"},
Expand Down

0 comments on commit 899eaf6

Please sign in to comment.