Skip to content

Commit

Permalink
🔧 Update MathJax configuration and enhance LaTeX processing
Browse files Browse the repository at this point in the history
- Replaced the MathJax script source in index.html to use 'tex-chtml-full.js' for improved rendering capabilities.
- Added 'mhchem' package to MathJax configuration in mathjax-config.js for better chemical equation support.
- Removed the obsolete 'tex-mml-chtml.js' file.
- Enhanced error handling in LaTeX processing functions within latex.js, including logging for MathJax rendering success and errors.
- Improved Markdown rendering to maintain original formatting for list items and handle math expression placeholders more effectively.
  • Loading branch information
yym68686 committed Jan 14, 2025
1 parent defade5 commit d55b2a9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<script src="lib/marked.min.js"></script>
<script src="lib/highlight.min.js"></script>
<script src="lib/mathjax-config.js"></script>
<script type="text/javascript" id="MathJax-script" async src="lib/tex-mml-chtml.js"></script>
<script type="text/javascript" id="MathJax-script" async src="lib/tex-chtml-full.js"></script>
<link rel="stylesheet" href="lib/github-dark.css">
<link rel="stylesheet" href="sidebar.css">
</head>
Expand Down
2 changes: 1 addition & 1 deletion lib/mathjax-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']],
packages: ['base', 'ams', 'noerrors', 'noundefined', 'textmacros', 'newcommand', 'physics', 'cancel', 'color', 'bbox', 'boldsymbol'],
packages: ['base', 'ams', 'noerrors', 'noundefined', 'textmacros', 'newcommand', 'physics', 'cancel', 'color', 'bbox', 'boldsymbol', 'mhchem'],
},
options: {
skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre']
Expand Down
34 changes: 34 additions & 0 deletions lib/tex-chtml-full.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion lib/tex-mml-chtml.js

This file was deleted.

2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Cerebr",
"version": "1.8.0",
"version": "1.8.1",
"description": "Cerebr - 智能AI聊天助手",
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
Expand Down
33 changes: 26 additions & 7 deletions src/utils/latex.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,25 @@ export function processMathAndMarkdown(text) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(code, { language: lang }).value;
} catch (err) {}
} catch (err) {
console.error('代码高亮错误:', err);
}
}
return hljs.highlightAuto(code).value;
},
renderer: Object.assign(new marked.Renderer(), {
code(code, language) {
// 检查是否包含数学表达式占位符
if (code.includes('%%MATH_EXPRESSION_')) {
return code; // 如果包含数学表达式,直接返回原文本
}
const validLanguage = language && hljs.getLanguage(language) ? language : '';
const highlighted = this.options.highlight(code, validLanguage);
return `<pre data-language="${validLanguage || 'plaintext'}"><code>${highlighted}</code></pre>`;
},
listitem(text) {
// 保持列表项的原始格式
return `<li>${text}</li>\n`;
}
})
});
Expand All @@ -65,14 +75,16 @@ export function processMathAndMarkdown(text) {
return ' '.repeat(level) + '*' + trailing;
});

// 处理第一级列表(确保使用3个空格)
text = text.replace(/^(\s{0,2})\*(\s+)/mg, ' *$2');
// // 处理第一级列表(确保使用3个空格)
// text = text.replace(/^(\s{0,2})\*(\s+)/mg, ' *$2');

// 渲染 Markdown
let html = marked.parse(text);

// 恢复数学公式
html = html.replace(/%%MATH_EXPRESSION_(\d+)%%/g, (_, index) => mathExpressions[index]);
html = html.replace(/%%MATH_EXPRESSION_(\d+)%%/g, (_, index) => {
return mathExpressions[index];
});

// 移除数学公式容器外的 p 标签
html = html.replace(/<p>\s*(<div class="math-display-container">[\s\S]*?<\/div>)\s*<\/p>/g, '$1');
Expand All @@ -83,8 +95,15 @@ export function processMathAndMarkdown(text) {
// 渲染数学公式的函数
export function renderMathInElement(element) {
if (window.MathJax) {
MathJax.typesetPromise([element]).catch((err) => {
console.error('MathJax渲染错误:', err);
});
MathJax.typesetPromise([element])
.then(() => {
console.log('MathJax 渲染成功');
})
.catch((err) => {
console.error('MathJax 渲染错误:', err);
console.error('错误堆栈:', err.stack);
});
} else {
console.error('MathJax 未加载');
}
}

0 comments on commit d55b2a9

Please sign in to comment.