Files
tougao_web/public/js/global-math.js
2025-03-21 09:06:56 +08:00

111 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 确保 MathJax 配置正确
window.MathJax = window.MathJax || {
loader: {
load: ['[tex]/ams', '[tex]/newcommand']
},
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']]
},
a11y: {
// 启用 MathJax 可访问性功能,确保使用 aria-label
texHints: true,
screenReader: true,
mathml: {
enable: true,
},
label: {
// 公式的 aria-label 配置
enable: true,
texClass: 'MathJax-Label',
form: 'LaTeX'
}
},
svg: { fontCache: 'global' }
};
// **定义全局渲染方法**
window.renderMathJax = function (tinymceId) {
// if (window.MathJax && typeof window.MathJax.typesetPromise === "function") {
// console.log("正在渲染 MathJax 公式...");
// // 如果提供了 TinyMCE 编辑器 ID
// if (tinymceId) {
// const editorInstance = window.tinymce.get(tinymceId); // 根据 ID 获取编辑器实例
// if (!editorInstance) {
// return;
// }
// // 获取指定的 TinyMCE 编辑器中的内容
// const editorBody = editorInstance.getBody();
// // 渲染 editorBody 中所有 <wmath> 标签,并使用 data-latex 的值
// const wmathElements = editorBody.querySelectorAll('wmath');
// wmathElements.forEach((element) => {
// // 检查 <wmath> 标签是否包含有效的 data-latex 属性值
// const latexContent = element.getAttribute('data-latex');
// if (latexContent) {
// // 将元素的内部内容替换为 data-latex 的值
// element.innerHTML = latexContent;
// // 渲染 MathJax 公式
// window.MathJax.typesetPromise([element]).catch((err) => {
// console.warn("TinyMCE MathJax 渲染失败:", err);
// });
// }
// });
// // 渲染 editorBody 中所有 <math> 标签MathML 内容)
// const mathElements = editorBody.querySelectorAll('math');
// mathElements.forEach((element) => {
// // 渲染 MathJax 公式
// window.MathJax.typesetPromise([element]).catch((err) => {
// console.warn("MathJax 渲染失败:", err);
// });
// });
// } else {
// // 处理全局文档中的 <wmath> 标签
// const wmathElements = document.querySelectorAll('wmath');
// wmathElements.forEach((element) => {
// // 检查 <wmath> 标签是否包含有效的 data-latex 属性值
// const latexContent = element.getAttribute('data-latex');
// if (latexContent) {
// // 将元素的内部内容替换为 data-latex 的值
// element.innerHTML = latexContent;
// // 渲染 MathJax 公式
// window.MathJax.typesetPromise([element]).catch((err) => {
// console.warn("MathJax 渲染失败:", err);
// });
// }
// });
// // 处理全局文档中的 <math> 标签MathML 内容)
// const mathElements = document.querySelectorAll('math');
// mathElements.forEach((element) => {
// // 渲染 MathJax 公式
// window.MathJax.typesetPromise([element]).catch((err) => {
// console.warn("MathJax 渲染失败:", err);
// });
// });
// }
// } else {
// console.warn("MathJax 未正确加载!");
// }
}