Skip to content

Commit

Permalink
🎨 Improve HTML code block clipping siyuan-note/siyuan#11540
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed May 27, 2024
1 parent 7777ff8 commit 011086d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
4 changes: 3 additions & 1 deletion h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {

buf := &bytes.Buffer{}
buf.WriteString(util.DomText(n))
content := &ast.Node{Type: ast.NodeCodeBlockCode, Tokens: buf.Bytes()}
tokens := buf.Bytes()
tokens = bytes.ReplaceAll(tokens, []byte("\u00A0"), []byte(" "))
content := &ast.Node{Type: ast.NodeCodeBlockCode, Tokens: tokens}
node.AppendChild(content)
node.AppendChild(&ast.Node{Type: ast.NodeCodeBlockFenceCloseMarker, Tokens: util.StrToBytes("```"), CodeBlockFenceLen: 3})

Expand Down
4 changes: 2 additions & 2 deletions 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.

5 changes: 3 additions & 2 deletions test/h2m_test.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions util/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ func IsTempMarkSpan(n *html.Node) bool {

}

func SetDomAttrValue(n *html.Node, attrName, attrVal string) {
if nil == n {
return
}

for _, attr := range n.Attr {
if attr.Key == attrName {
attr.Val = attrVal
return
}
}

n.Attr = append(n.Attr, &html.Attribute{Key: attrName, Val: attrVal})
}

func DomAttrValue(n *html.Node, attrName string) string {
if nil == n {
return ""
Expand Down
43 changes: 43 additions & 0 deletions vditor_wysiwyg.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,49 @@ func (lute *Lute) adjustVditorDOM(root *html.Node) {
for c := root.FirstChild; nil != c; c = c.NextSibling {
lute.mergeSameStrong(c)
}

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

func (lute *Lute) adjustTableCode(n *html.Node) {
if atom.Table == n.DataAtom {
// 表格类型的代码块进行预处理 https://github.com/siyuan-note/siyuan/issues/11540
// 移除 <td class="gutter">
// td class="code"> 下的 <div class="container"> 改为 <pre>

tds := util.DomChildrenByType(n, atom.Td)
var unlinks []*html.Node
for _, td := range tds {
tdClass := util.DomAttrValue(td, "class")
if strings.Contains(tdClass, "gutter") {
unlinks = append(unlinks, td)
continue
}

if strings.Contains(tdClass, "code") {
if c := td.FirstChild; nil != c && atom.Div == c.DataAtom {
c.DataAtom = atom.Pre
c.Data = "pre"
lang := util.DomAttrValue(n, "class")
lang = strings.ReplaceAll(lang, "syntaxhighlighter", "")
lang = strings.TrimSpace(lang)
if "" != lang {
util.SetDomAttrValue(c, "class", lang)
}
for div := c.FirstChild; nil != div; div = div.NextSibling {
div.DataAtom = atom.Code
div.Data = "code"
}
}
}
}
}

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

func (lute *Lute) mergeSameStrong(n *html.Node) {
Expand Down

0 comments on commit 011086d

Please sign in to comment.