Skip to content

Commit 6b9d743

Browse files
committed
🎨 Improve formula clipping siyuan-note/siyuan#11640
1 parent 53a7d0c commit 6b9d743

File tree

7 files changed

+49
-7
lines changed

7 files changed

+49
-7
lines changed

h2m.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
166166
if lute.ParseOptions.ProtyleWYSIWYG {
167167
node.Tokens = lex.EscapeProtyleMarkers(node.Tokens)
168168
} else {
169-
node.Tokens = lex.EscapeMarkers(node.Tokens)
169+
node.Tokens = lex.EscapeCommonMarkers(node.Tokens)
170170
if lute.parentIs(n, atom.Table) {
171171
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("\\|"), []byte("|"))
172172
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("|"), []byte("\\|"))

javascript/lute.min.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/lute.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lex/token.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func RepeatBackslashBeforePipe(content string) string {
414414
return buf.String()
415415
}
416416

417-
func EscapeMarkers(tokens []byte) []byte {
417+
func EscapeCommonMarkers(tokens []byte) []byte {
418418
for i := 0; i < len(tokens); i++ {
419419
if IsCommonInlineMarker(tokens[i]) {
420420
remains := append([]byte{ItemBackslash}, tokens[i:]...)

test/h2m_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
var html2MdTests = []parseTest{
2020

21+
{"140", "<span class=\"texhtml\">{<i>f</i><sub><i>k</i></sub>}<sub><i>k</i> ∈ <b>N</b></sub></span>", "$\\{f_k\\}_k ∈ N$\n"},
2122
{"139", "<div id=\"readability-page-1\" class=\"page\">\n <div class=\"tablet:max-xl:w-mid-content xl:w-content flex\" id=\"post-content\">\n <div class=\" post-body text-gray-text xl:col-start-1 xl:order-4 order-4 text-lg\">\n <p><img src=\"https://test.com/foo.png\"\n width=\"640\" height=\"366\" alt=\"bar\"></p>\n <p>baz\n </p>\n </div>\n </div>\n</div>", "![bar](https://test.com/foo.png)\n\nbaz\n"},
2223
{"138", "<code class=\"samp docutils literal notranslate\"><em><span class=\"pre\">package_name</span></em><span class=\"pre\">-</span><em><span class=\"pre\">version</span></em><span class=\"pre\">-</span><em><span class=\"pre\">python_tag</span></em><span class=\"pre\">-</span><em><span class=\"pre\">abi_tag</span></em><span class=\"pre\">-</span><em><span class=\"pre\">platform_tag</span></em><span class=\"pre\">.whl</span></code>", "`package_name-version-python_tag-abi_tag-platform_tag.whl`\n"},
2324
{"137", "<div><figure>foo<figcaption>bar</figcaption></figure><figure>foo1<figcaption>bar1</figcaption></figure><p>baz</p></div>", "foo\nbar\n\nfoo1\nbar1\n\n\nbaz\n"},

util/html.go

+41
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,47 @@ func DomHTML(n *html.Node) []byte {
141141
return bytes.ReplaceAll(buf.Bytes(), []byte(editor.Zwsp), nil)
142142
}
143143

144+
func DomTexhtml(n *html.Node) string {
145+
buf := &bytes.Buffer{}
146+
if html.TextNode == n.Type {
147+
buf.WriteString(n.Data)
148+
return buf.String()
149+
}
150+
for child := n.FirstChild; nil != child; child = child.NextSibling {
151+
domTexhtml0(child, buf)
152+
}
153+
return buf.String()
154+
}
155+
156+
func domTexhtml0(n *html.Node, buffer *bytes.Buffer) {
157+
if nil == n {
158+
return
159+
}
160+
161+
switch n.DataAtom {
162+
case 0:
163+
buffer.WriteString(escapeMathSymbol(n.Data))
164+
return
165+
case atom.Sup:
166+
buffer.WriteString("^")
167+
case atom.Sub:
168+
buffer.WriteString("_")
169+
}
170+
171+
for child := n.FirstChild; nil != child; child = child.NextSibling {
172+
domTexhtml0(child, buffer)
173+
}
174+
}
175+
176+
func escapeMathSymbol(s string) string {
177+
// 转义 Tex 公式中的符号,比如 _ ^ { }
178+
s = strings.ReplaceAll(s, "_", "\\_")
179+
s = strings.ReplaceAll(s, "^", "\\^")
180+
s = strings.ReplaceAll(s, "{", "\\{")
181+
s = strings.ReplaceAll(s, "}", "\\}")
182+
return s
183+
}
184+
144185
func DomText(n *html.Node) string {
145186
buf := &bytes.Buffer{}
146187
if html.TextNode == n.Type {

vditor_wysiwyg.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (lute *Lute) adjustMath(n *html.Node) {
249249
}
250250

251251
if strings.Contains(class, "texhtml") {
252-
if mathContent := util.DomText(n); "" != mathContent {
252+
if mathContent := util.DomTexhtml(n); "" != mathContent {
253253
util.SetDomAttrValue(n, "data-tex", mathContent)
254254
return
255255
}

0 commit comments

Comments
 (0)