Skip to content

Commit

Permalink
🎨 Improve StackExchange formula clipping siyuan-note/siyuan#11646
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 5, 2024
1 parent 3a5841e commit 898cd11
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
6 changes: 5 additions & 1 deletion h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,11 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
return
case atom.Script:
if tex := util.DomText(n.FirstChild); "" != tex {
appendInlineMath(tree, tex)
if tree.Context.Tip.IsContainerBlock() {
appendMathBlock(tree, tex)
} else {
appendInlineMath(tree, tex)
}
return
}
case atom.Figcaption:
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: 2 additions & 0 deletions test/h2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

var html2MdTests = []parseTest{

{"134", "<p>In proving that (a) implies (b), I used Tao's suggestion to define the numbers <span class=\"math-container\"><span class=\"MathJax_Preview\" style=\"color: inherit;\"></span><span class=\"MathJax\" id=\"MathJax-Element-10-Frame\" tabindex=\"0\" data-mathml=\"<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msub><mi>n</mi><mi>j</mi></msub></math>\" role=\"presentation\" style=\"position: relative;\"><nobr aria-hidden=\"true\"><span class=\"math\" id=\"MathJax-Span-94\" style=\"width: 1.084em; display: inline-block;\"><span style=\"display: inline-block; position: relative; width: 0.964em; height: 0px; font-size: 111%;\"><span style=\"position: absolute; clip: rect(1.565em, 1000.96em, 2.646em, -999.997em); top: -2.159em; left: 0em;\"><span class=\"mrow\" id=\"MathJax-Span-95\"><span class=\"msubsup\" id=\"MathJax-Span-96\"><span style=\"display: inline-block; position: relative; width: 0.964em; height: 0px;\"><span style=\"position: absolute; clip: rect(3.426em, 1000.6em, 4.207em, -999.997em); top: -4.021em; left: 0em;\"><span class=\"mi\" id=\"MathJax-Span-97\" style=\"font-family: MathJax_Math-italic;\">n</span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span><span style=\"position: absolute; top: -3.901em; left: 0.604em;\"><span class=\"mi\" id=\"MathJax-Span-98\" style=\"font-size: 70.7%; font-family: MathJax_Math-italic;\">j</span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span></span></span></span><span style=\"display: inline-block; width: 0px; height: 2.165em;\"></span></span></span><span style=\"display: inline-block; overflow: hidden; vertical-align: -0.397em; border-left: 0px solid; width: 0px; height: 0.937em;\"></span></span></nobr><span class=\"MJX_Assistive_MathML\" role=\"presentation\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><msub><mi>n</mi><mi>j</mi></msub></math></span></span><script type=\"math/tex\" id=\"MathJax-Element-10\">n_j</script></span> for each natural number <span class=\"math-container\"><span class=\"MathJax_Preview\" style=\"color: inherit;\"></span><span class=\"MathJax\" id=\"MathJax-Element-11-Frame\" tabindex=\"0\" data-mathml=\"<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>j</mi></math>\" role=\"presentation\" style=\"position: relative;\"><nobr aria-hidden=\"true\"><span class=\"math\" id=\"MathJax-Span-99\" style=\"width: 0.483em; display: inline-block;\"><span style=\"display: inline-block; position: relative; width: 0.423em; height: 0px; font-size: 111%;\"><span style=\"position: absolute; clip: rect(1.324em, 1000.42em, 2.526em, -999.997em); top: -2.159em; left: 0em;\"><span class=\"mrow\" id=\"MathJax-Span-100\"><span class=\"mi\" id=\"MathJax-Span-101\" style=\"font-family: MathJax_Math-italic;\">j</span></span><span style=\"display: inline-block; width: 0px; height: 2.165em;\"></span></span></span><span style=\"display: inline-block; overflow: hidden; vertical-align: -0.263em; border-left: 0px solid; width: 0px; height: 1.07em;\"></span></span></nobr><span class=\"MJX_Assistive_MathML\" role=\"presentation\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>j</mi></math></span></span><script type=\"math/tex\" id=\"MathJax-Element-11\">j</script></span> by the formula:</p>", "In proving that (a) implies (b), I used Tao's suggestion to define the numbers $n_j$ for each natural number $j$ by the formula:\n"},
{"133", "<p><span class=\"math-container\"><span class=\"MathJax_Preview\" style=\"color: inherit;\"></span><div class=\"MathJax_Display\" style=\"text-align: center;\"><span class=\"MathJax\" id=\"MathJax-Element-12-Frame\" tabindex=\"0\" style=\"text-align: center; position: relative;\" data-mathml=\"<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;><msub><mi>n</mi><mi>j</mi></msub><mo>:=</mo><mo movablelimits=&quot;true&quot; form=&quot;prefix&quot;>min</mo><mrow><mo>{</mo><mi>n</mi><mo>&amp;gt;</mo><msub><mi>n</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>j</mi><mo>&amp;#x2212;</mo><mn>1</mn></mrow></msub><mo>:</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo stretchy=&quot;false&quot;>|</mo></mrow><msub><mi>a</mi><mi>n</mi></msub><mo>&amp;#x2212;</mo><mi>L</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo stretchy=&quot;false&quot;>|</mo></mrow><mo>&amp;#x2264;</mo><mfrac><mn>1</mn><mi>j</mi></mfrac><mo>}</mo></mrow></math>\" role=\"presentation\"><nobr aria-hidden=\"true\"><span class=\"math\" id=\"MathJax-Span-102\" style=\"width: 18.261em; display: inline-block;\"><span style=\"display: inline-block; position: relative; width: 16.459em; height: 0px; font-size: 111%;\"><span style=\"position: absolute; clip: rect(1.805em, 1016.34em, 4.568em, -999.997em); top: -3.42em; left: 0em;\"><span class=\"mrow\" id=\"MathJax-Span-103\"><span class=\"msubsup\" id=\"MathJax-Span-104\"><span style=\"display: inline-block; position: relative; width: 0.964em; height: 0px;\"><span style=\"position: absolute; clip: rect(3.426em, 1000.6em, 4.207em, -999.997em); top: -4.021em; left: 0em;\"><span class=\"mi\" id=\"MathJax-Span-105\" style=\"font-family: MathJax_Math-italic;\">n</span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span><span style=\"position: absolute; top: -3.901em; left: 0.604em;\"><span class=\"mi\" id=\"MathJax-Span-106\" style=\"font-size: 70.7%; font-family: MathJax_Math-italic;\">j</span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span></span></span><span class=\"mo\" id=\"MathJax-Span-107\" style=\"font-family: MathJax_Main; padding-left: 0.303em;\">:<span style=\"font-family: MathJax_Main;\">=</span></span><span class=\"mo\" id=\"MathJax-Span-108\" style=\"font-family: MathJax_Main; padding-left: 0.303em;\">min</span><span class=\"mrow\" id=\"MathJax-Span-109\" style=\"padding-left: 0.183em;\"><span class=\"mo\" id=\"MathJax-Span-110\" style=\"vertical-align: 0em;\"><span style=\"font-family: MathJax_Size3;\">{</span></span><span class=\"mi\" id=\"MathJax-Span-111\" style=\"font-family: MathJax_Math-italic;\">n</span><span class=\"mo\" id=\"MathJax-Span-112\" style=\"font-family: MathJax_Main; padding-left: 0.303em;\">&gt;</span><span class=\"msubsup\" id=\"MathJax-Span-113\" style=\"padding-left: 0.303em;\"><span style=\"display: inline-block; position: relative; width: 1.865em; height: 0px;\"><span style=\"position: absolute; clip: rect(3.426em, 1000.6em, 4.207em, -999.997em); top: -4.021em; left: 0em;\"><span class=\"mi\" id=\"MathJax-Span-114\" style=\"font-family: MathJax_Math-italic;\">n</span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span><span style=\"position: absolute; top: -3.901em; left: 0.604em;\"><span class=\"texatom\" id=\"MathJax-Span-115\"><span class=\"mrow\" id=\"MathJax-Span-116\"><span class=\"mi\" id=\"MathJax-Span-117\" style=\"font-size: 70.7%; font-family: MathJax_Math-italic;\">j</span><span class=\"mo\" id=\"MathJax-Span-118\" style=\"font-size: 70.7%; font-family: MathJax_Main;\">−</span><span class=\"mn\" id=\"MathJax-Span-119\" style=\"font-size: 70.7%; font-family: MathJax_Main;\">1</span></span></span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span></span></span><span class=\"mo\" id=\"MathJax-Span-120\" style=\"font-family: MathJax_Main; padding-left: 0.303em;\">:</span><span class=\"texatom\" id=\"MathJax-Span-121\" style=\"padding-left: 0.303em;\"><span class=\"mrow\" id=\"MathJax-Span-122\"><span class=\"mo\" id=\"MathJax-Span-123\" style=\"font-family: MathJax_Main;\">|</span></span></span><span class=\"msubsup\" id=\"MathJax-Span-124\"><span style=\"display: inline-block; position: relative; width: 1.024em; height: 0px;\"><span style=\"position: absolute; clip: rect(3.426em, 1000.48em, 4.207em, -999.997em); top: -4.021em; left: 0em;\"><span class=\"mi\" id=\"MathJax-Span-125\" style=\"font-family: MathJax_Math-italic;\">a</span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span><span style=\"position: absolute; top: -3.901em; left: 0.544em;\"><span class=\"mi\" id=\"MathJax-Span-126\" style=\"font-size: 70.7%; font-family: MathJax_Math-italic;\">n</span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span></span></span><span class=\"mo\" id=\"MathJax-Span-127\" style=\"font-family: MathJax_Main; padding-left: 0.243em;\">−</span><span class=\"mi\" id=\"MathJax-Span-128\" style=\"font-family: MathJax_Math-italic; padding-left: 0.243em;\">L</span><span class=\"texatom\" id=\"MathJax-Span-129\"><span class=\"mrow\" id=\"MathJax-Span-130\"><span class=\"mo\" id=\"MathJax-Span-131\" style=\"font-family: MathJax_Main;\">|</span></span></span><span class=\"mo\" id=\"MathJax-Span-132\" style=\"font-family: MathJax_Main; padding-left: 0.303em;\">≤</span><span class=\"mfrac\" id=\"MathJax-Span-133\" style=\"padding-left: 0.303em;\"><span style=\"display: inline-block; position: relative; width: 0.604em; height: 0px; margin-right: 0.123em; margin-left: 0.123em;\"><span style=\"position: absolute; clip: rect(3.186em, 1000.42em, 4.207em, -999.997em); top: -4.682em; left: 50%; margin-left: -0.237em;\"><span class=\"mn\" id=\"MathJax-Span-134\" style=\"font-family: MathJax_Main;\">1</span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span><span style=\"position: absolute; clip: rect(3.186em, 1000.42em, 4.387em, -999.997em); top: -3.36em; left: 50%; margin-left: -0.177em;\"><span class=\"mi\" id=\"MathJax-Span-135\" style=\"font-family: MathJax_Math-italic;\">j</span><span style=\"display: inline-block; width: 0px; height: 4.027em;\"></span></span><span style=\"position: absolute; clip: rect(0.844em, 1000.6em, 1.264em, -999.997em); top: -1.318em; left: 0em;\"><span style=\"display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 0.604em; height: 0px;\"></span><span style=\"display: inline-block; width: 0px; height: 1.084em;\"></span></span></span></span><span class=\"mo\" id=\"MathJax-Span-136\" style=\"vertical-align: 0em;\"><span style=\"font-family: MathJax_Size3;\">}</span></span></span></span><span style=\"display: inline-block; width: 0px; height: 3.426em;\"></span></span></span><span style=\"display: inline-block; overflow: hidden; vertical-align: -1.13em; border-left: 0px solid; width: 0px; height: 2.803em;\"></span></span></nobr><span class=\"MJX_Assistive_MathML MJX_Assistive_MathML_Block\" role=\"presentation\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><msub><mi>n</mi><mi>j</mi></msub><mo>:=</mo><mo movablelimits=\"true\" form=\"prefix\">min</mo><mrow><mo>{</mo><mi>n</mi><mo>&gt;</mo><msub><mi>n</mi><mrow class=\"MJX-TeXAtom-ORD\"><mi>j</mi><mo>−</mo><mn>1</mn></mrow></msub><mo>:</mo><mrow class=\"MJX-TeXAtom-ORD\"><mo stretchy=\"false\">|</mo></mrow><msub><mi>a</mi><mi>n</mi></msub><mo>−</mo><mi>L</mi><mrow class=\"MJX-TeXAtom-ORD\"><mo stretchy=\"false\">|</mo></mrow><mo>≤</mo><mfrac><mn>1</mn><mi>j</mi></mfrac><mo>}</mo></mrow></math></span></span></div><script type=\"math/tex; mode=display\" id=\"MathJax-Element-12\">\nn_j := \\min \\left\\{n > n_{j-1} : |a_n - L| \\leq \\frac{1}{j} \\right\\}\n</script></span></p>", "$$\nn_j := \\min \\left\\{n > n_{j-1} : |a_n - L| \\leq \\frac{1}{j} \\right\\}\n$$\n"},
{"132", "<figure class=\"mw-halign-right\" typeof=\"mw:File/Thumb\"><a href=\"/wiki/File:Sphere_wrapped_round_itself.png\" class=\"mw-file-description\"><img src=\"//upload.wikimedia.org/wikipedia/commons/thumb/5/50/Sphere_wrapped_round_itself.png/200px-Sphere_wrapped_round_itself.png\" decoding=\"async\" width=\"200\" height=\"188\" class=\"mw-file-element\" srcset=\"//upload.wikimedia.org/wikipedia/commons/thumb/5/50/Sphere_wrapped_round_itself.png/300px-Sphere_wrapped_round_itself.png 1.5x, //upload.wikimedia.org/wikipedia/commons/5/50/Sphere_wrapped_round_itself.png 2x\" data-file-width=\"378\" data-file-height=\"356\"></a><figcaption>A degree two map of a <a href=\"/wiki/Sphere\" title=\"Sphere\">sphere</a> onto itself.</figcaption></figure>", "![](//upload.wikimedia.org/wikipedia/commons/5/50/Sphere_wrapped_round_itself.png)\n\nA degree two map of a [sphere](/wiki/Sphere \"Sphere\") onto itself.\n"},
{"131", "<p>The <a href=\"/wiki/Elliptic_integral#Incomplete_elliptic_integral_of_the_first_kind\" title=\"Elliptic integral\">incomplete elliptic integral of the first kind</a> <span class=\"texhtml\">F</span> is \n</p>", "The [incomplete elliptic integral of the first kind](/wiki/Elliptic_integral#Incomplete_elliptic_integral_of_the_first_kind \"Elliptic integral\") $F$ is\n"},
{"130", "<p>foo</p><dl><dd>bar</dd></dl><p>baz</p>", "foo\n\nbar\n\nbaz\n"},
Expand Down
18 changes: 15 additions & 3 deletions vditor_wysiwyg.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ func (lute *Lute) adjustVditorDOM(root *html.Node) {

// The browser extension supports Wikipedia formula clipping https://github.com/siyuan-note/siyuan/issues/11583
for c := root.FirstChild; nil != c; c = c.NextSibling {
lute.adjustWikipediaMath(c)
lute.adjustMath(c)
}
}

func (lute *Lute) adjustWikipediaMath(n *html.Node) {
func (lute *Lute) adjustMath(n *html.Node) {
class := util.DomAttrValue(n, "class")
if ((atom.Span == n.DataAtom || atom.Div == n.DataAtom) && strings.Contains(class, "mwe-math-element")) || strings.Contains(class, "tex") {
if annos := util.DomChildrenByType(n, atom.Annotation); 0 < len(annos) {
Expand All @@ -256,8 +256,20 @@ func (lute *Lute) adjustWikipediaMath(n *html.Node) {
}
}

if strings.Contains(class, "math") {
scripts := util.DomChildrenByType(n, atom.Script)
if 0 < len(scripts) {
script := scripts[0]
if "math/tex" == util.DomAttrValue(script, "type") {
if mathContent := util.DomText(script); "" != mathContent {
util.SetDomAttrValue(n, "data-tex", mathContent)
}
}
}
}

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

Expand Down

0 comments on commit 898cd11

Please sign in to comment.