|
|
@@ -42,6 +42,10 @@ export const mathRender = (element: HTMLElement, options?: { cdn?: string, math?
|
|
|
addStyle(`${options.cdn}/dist/js/katex/katex.min.css`, "vditorKatexStyle");
|
|
|
addScript(`${options.cdn}/dist/js/katex/katex.min.js`, "vditorKatexScript").then(() => {
|
|
|
mathElements.forEach((mathElement) => {
|
|
|
+ if (mathElement.parentElement.classList.contains("vditor-wysiwyg__pre") ||
|
|
|
+ mathElement.parentElement.classList.contains("vditor-ir__marker--pre")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (mathElement.getAttribute("data-math")) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -68,6 +72,18 @@ export const mathRender = (element: HTMLElement, options?: { cdn?: string, math?
|
|
|
});
|
|
|
});
|
|
|
} else if (options.math.engine === "MathJax") {
|
|
|
+ const chainAsync = (fns: any) => {
|
|
|
+ if (fns.length === 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ let curr = 0;
|
|
|
+ const last = fns[fns.length - 1];
|
|
|
+ const next = () => {
|
|
|
+ const fn = fns[curr++];
|
|
|
+ fn === last ? fn() : fn(next);
|
|
|
+ };
|
|
|
+ next();
|
|
|
+ };
|
|
|
if (!window.MathJax) {
|
|
|
window.MathJax = {
|
|
|
loader: {
|
|
|
@@ -80,40 +96,43 @@ export const mathRender = (element: HTMLElement, options?: { cdn?: string, math?
|
|
|
}
|
|
|
// 循环加载会抛异常
|
|
|
addScriptSync(`${options.cdn}/dist/js/mathjax/tex-svg.js`, "vditorMathJaxScript");
|
|
|
- const renderMath = () => {
|
|
|
- mathElements.forEach((mathElement) => {
|
|
|
- if (mathElement.getAttribute("data-math")) {
|
|
|
- return;
|
|
|
- }
|
|
|
- const math = code160to32(mathElement.textContent);
|
|
|
+ const renderMath = (mathElement: Element, next?: () => void) => {
|
|
|
+ const math = code160to32(mathElement.textContent).trim();
|
|
|
+ const mathOptions = window.MathJax.getMetricsFor(mathElement);
|
|
|
+ mathOptions.display = mathElement.tagName === "DIV";
|
|
|
+ window.MathJax.tex2svgPromise(math, mathOptions).then((node: Element) => {
|
|
|
+ mathElement.innerHTML = "";
|
|
|
mathElement.setAttribute("data-math", math);
|
|
|
- if (!math) {
|
|
|
- return;
|
|
|
+ mathElement.append(node);
|
|
|
+ window.MathJax.startup.document.clear();
|
|
|
+ window.MathJax.startup.document.updateDocument();
|
|
|
+ const errorTextElement = node.querySelector('[data-mml-node="merror"]');
|
|
|
+ if (errorTextElement && errorTextElement.textContent.trim() !== "") {
|
|
|
+ mathElement.innerHTML = errorTextElement.textContent.trim();
|
|
|
+ mathElement.className = "vditor-reset--error";
|
|
|
+ }
|
|
|
+ if (next) {
|
|
|
+ next();
|
|
|
}
|
|
|
- window.MathJax.texReset();
|
|
|
- const mathOptions = window.MathJax.getMetricsFor(mathElement);
|
|
|
- mathOptions.display = mathElement.tagName === "DIV";
|
|
|
- window.MathJax.tex2svgPromise(math, mathOptions).then((node: HTMLElement) => {
|
|
|
- mathElement.innerHTML = "";
|
|
|
- mathElement.append(node);
|
|
|
- window.MathJax.startup.document.clear();
|
|
|
- window.MathJax.startup.document.updateDocument();
|
|
|
-
|
|
|
- const errorTextElement = mathElement.querySelector('[data-mml-node="merror"]');
|
|
|
- if (errorTextElement && errorTextElement.textContent.trim() !== "") {
|
|
|
- mathElement.innerHTML = errorTextElement.textContent.trim();
|
|
|
- mathElement.className = "vditor-reset--error";
|
|
|
- }
|
|
|
- });
|
|
|
});
|
|
|
};
|
|
|
- // 初次加载需等到 js 文件执行完成
|
|
|
- if (!window.MathJax.texReset) {
|
|
|
- setTimeout(() => {
|
|
|
- renderMath();
|
|
|
- });
|
|
|
- } else {
|
|
|
- renderMath();
|
|
|
- }
|
|
|
+ window.MathJax.startup.promise.then(() => {
|
|
|
+ const chains: any [] = [];
|
|
|
+ for (let i = 0; i < mathElements.length; i++) {
|
|
|
+ const mathElement = mathElements[i];
|
|
|
+ if (!mathElement.parentElement.classList.contains("vditor-wysiwyg__pre") &&
|
|
|
+ !mathElement.parentElement.classList.contains("vditor-ir__marker--pre") &&
|
|
|
+ !mathElement.getAttribute("data-math") && code160to32(mathElement.textContent).trim()) {
|
|
|
+ chains.push((next: () => void) => {
|
|
|
+ if (i === mathElements.length - 1) {
|
|
|
+ renderMath(mathElement);
|
|
|
+ } else {
|
|
|
+ renderMath(mathElement, next);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ chainAsync(chains);
|
|
|
+ });
|
|
|
}
|
|
|
};
|