瀏覽代碼

fix MathJax v3 support

1. Support MathJax inside a fenced code block;
2. Support scale factor in preview;
Le Tan 5 年之前
父節點
當前提交
7d3732c58f

+ 0 - 7
src/resources/markdown-it.js

@@ -256,13 +256,6 @@ var postProcessMathJaxWhenMathjaxReady = function() {
                 content.setLog("err: " + err);
             }
         }
-
-        if (node.tagName.toLowerCase() == 'code') {
-            var pre = node.parentNode;
-            var p = document.createElement('p');
-            p.innerHTML = node.innerHTML;
-            pre.parentNode.replaceChild(p, pre);
-        }
     }
 };
 

+ 12 - 4
src/resources/markdown_template.js

@@ -1269,6 +1269,7 @@ var renderCodeBlockLineNumber = function() {
 
 var addClassToCodeBlock = function() {
     var codes = document.getElementsByTagName('code');
+    var mathCodes = [];
     for (var i = 0; i < codes.length; ++i) {
         var code = codes[i];
         var pare = code.parentElement;
@@ -1278,13 +1279,20 @@ var addClassToCodeBlock = function() {
             if (VEnableMathjax
                 && (code.classList.contains("lang-mathjax")
                     || code.classList.contains("language-mathjax"))) {
-                // Add the class to pre.
-                pare.classList.add("lang-mathjax");
-                pare.classList.add("language-mathjax");
-                pare.classList.add("tex-to-render");
+                mathCodes.push(code);
             }
         }
     }
+
+    // Replace math codes with <x-eqn>.
+    for (var i = mathCodes.length - 1; i >= 0; --i) {
+        var code = mathCodes[i];
+        var pare = code.parentElement;
+        var xeqn = document.createElement('x-eqn');
+        xeqn.classList.add("tex-to-render");
+        xeqn.innerHTML = code.innerHTML;
+        pare.parentNode.replaceChild(xeqn, pare);
+    }
 };
 
 var addCopyButtonToCodeBlock = function() {

+ 7 - 6
src/resources/mathjax_preview_template.html

@@ -12,12 +12,13 @@
     <!-- EXTRA_PLACE_HOLDER -->
     <script>
         MathJax = {
-          tex: {
-            inlineMath: [['$', '$'], ['\\(', '\\)']]
-          },
-          chtml: {
-            scale: 1 // http://docs.mathjax.org/en/latest/options/output/index.html#options-common-to-all-output-processors
-          }
+            tex: {
+                inlineMath: [['$', '$'], ['\\(', '\\)']]
+            },
+            chtml: {
+                // http://docs.mathjax.org/en/latest/options/output/index.html#options-common-to-all-output-processors
+                scale: SCALE_FACTOR_PLACE_HOLDER
+            }
         };
     </script>
     <script src="JS_PLACE_HOLDER" defer id="MathJax-script"></script>

+ 0 - 2
src/utils/vutils.cpp

@@ -967,8 +967,6 @@ QString VUtils::generateMathJaxPreviewTemplate()
 
     QString extraFile;
 
-    QString mathjaxScale = QString::number((int)(100 * VUtils::calculateScaleFactor()));
-
     /*
     // Mermaid.
     extraFile += "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + g_config->getMermaidCssStyleUrl() + "\"/>\n" +

+ 1 - 0
src/vconstants.h

@@ -45,6 +45,7 @@ namespace HtmlHolder
     static const QString c_cssHolder = "CSS_PLACE_HOLDER";
     static const QString c_codeBlockCssHolder = "HIGHLIGHTJS_CSS_PLACE_HOLDER";
     static const QString c_commonCssHolder = "COMMON_CSS_PLACE_HOLDER";
+    static const QString c_scaleFactorHolder = "SCALE_FACTOR_PLACE_HOLDER";
     static const QString c_globalStyleHolder = "/* STYLE_GLOBAL_PLACE_HOLDER */";
     static const QString c_extraHolder = "<!-- EXTRA_PLACE_HOLDER -->";
     static const QString c_bodyHolder = "<!-- BODY_PLACE_HOLDER -->";

+ 2 - 0
src/vnote.cpp

@@ -195,6 +195,8 @@ QString VNote::generateMathJaxPreviewTemplate()
 
     templ.replace(HtmlHolder::c_cssHolder, g_config->getCssStyleUrl());
 
+    templ.replace(HtmlHolder::c_scaleFactorHolder, QString::number(VUtils::calculateScaleFactor()));
+
     return templ;
 }