Skip to content

Commit

Permalink
🎨 Improve HTML inline code clipping siyuan-note/siyuan#11370
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed May 13, 2024
1 parent 859ae9c commit 417af55
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
12 changes: 11 additions & 1 deletion h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,17 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {

if nil != n.Parent && atom.Span == n.Parent.DataAtom && ("" != util.DomAttrValue(n.Parent, "class")) {
if lastc := tree.Context.Tip.LastChild; nil == lastc || (ast.NodeText == lastc.Type && !bytes.HasSuffix(lastc.Tokens, []byte("**"))) {
node.Tokens = []byte("**" + util.BytesToStr(node.Tokens) + "**")
content := string(node.Tokens)
prefixSpaces := lute.prefixSpaces(content)
suffixSpaces := lute.suffixSpaces(content)
content = strings.TrimSpace(content)
if "" != prefixSpaces {
node.Tokens = []byte(prefixSpaces)
}
node.Tokens = []byte("**" + content + "**")
if "" != suffixSpaces {
node.Tokens = append(node.Tokens, []byte(suffixSpaces)...)
}
}
}
tree.Context.Tip.AppendChild(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{

{"100", "<h3 class=\"heading settled\" data-level=\"2.2\" id=\"interface-event\"><span class=\"secno\">2.2. </span><span class=\"content\">Interface <code class=\"idl\"><a data-link-type=\"idl\" href=\"#event\" id=\"ref-for-event①\">Event</a></code></span><a class=\"self-link\" href=\"#interface-event\"></a></h3>", "### 2.2. **Interface** [`Event`](#event)\n"},
{"99", "<div class=\"markdown-heading\"><h3 class=\"heading-element\"><code>subject:matches-attr(arg)</code></h3><a id=\"user-content-subjectmatches-attrarg\" class=\"anchor\" aria-label=\"Permalink: subject:matches-attr(arg)\" href=\"#subjectmatches-attrarg\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"></path></svg></a></div>", "### `subject:matches-attr(arg)`\n"},
{"98", "<code class=\"notranslate\"><span>capture_stdout=True,</span><span>&nbsp;</span><span>stderr=subprocess.STDOUT</span></code>", "`capture_stdout=True, stderr=subprocess.STDOUT`\n"},
{"97", "<pre class=\"prettyprint linenums prettyprinted\" style=\"\"><ol class=\"linenums\"><li class=\"L0\"><span class=\"pln\">sudo xattr </span><span class=\"pun\">-</span><span class=\"pln\">r </span><span class=\"pun\">-</span><span class=\"pln\">d com</span><span class=\"pun\">.</span><span class=\"pln\">apple</span><span class=\"pun\">.</span><span class=\"pln\">quarantine </span><span class=\"pun\">/</span><span class=\"typ\">Applications</span><span class=\"pun\">/</span><span class=\"pln\">host</span><span class=\"pun\">防验证工具.</span><span class=\"pln\">app</span></li></ol></pre>", "```\nsudo xattr -r -d com.apple.quarantine /Applications/host防验证工具.app\n```\n"},
Expand Down
37 changes: 37 additions & 0 deletions vditor_wysiwyg.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ func (lute *Lute) adjustVditorDOM(root *html.Node) {
for c := root.FirstChild; nil != c; c = c.NextSibling {
lute.adjustVditorDOMListItemInP(c)
}

for c := root.FirstChild; nil != c; c = c.NextSibling {
lute.adjustVditorDOMCodeA(c)
}
}

// adjustVditorDOMListList 用于将 ul.ul 调整为 ul.li.ul。
Expand Down Expand Up @@ -463,6 +467,39 @@ func (lute *Lute) adjustVditorDOMListItemInP(n *html.Node) {
}
}

func (lute *Lute) adjustVditorDOMCodeA(n *html.Node) {
// https://github.com/siyuan-note/siyuan/issues/11370
if atom.Code == n.DataAtom && nil != n.FirstChild && atom.A == n.FirstChild.DataAtom && n.FirstChild == n.LastChild {
// code.a 的情况将 a 移到 code 外层,即 a.code
prev := n.PrevSibling
parent := n.Parent
a := n.FirstChild
a.Unlink()
n.Unlink()

var anchorTexts []*html.Node
for c := a.FirstChild; nil != c; c = c.NextSibling {
anchorTexts = append(anchorTexts, c)
c.Unlink()
}
for _, anchorText := range anchorTexts {
n.AppendChild(anchorText)
}
a.AppendChild(n)
n = a
if nil != prev {
prev.InsertAfter(n)
} else if nil != parent {
parent.AppendChild(n)
}
return
}

for c := n.FirstChild; c != nil; c = c.NextSibling {
lute.adjustVditorDOMCodeA(c)
}
}

// forwardNextBlock 向前移动至下一个块级节点,即跳过行级节点。
func (lute *Lute) forwardNextBlock(spanNode *html.Node) (spans []*html.Node, nextBlock *html.Node) {
for next := spanNode; nil != next; next = next.NextSibling {
Expand Down

0 comments on commit 417af55

Please sign in to comment.