feat(渲染): 支持 LaTeX 数学公式,KaTeX 本地化

聊天消息与 md 文件预览都支持 $...$ 行内与 $$...$$ 块级。

实现:占位符往返,不用 marked 扩展
- 解析前把公式抠成 @@MATHn@@(跳过围栏代码与行内代码)
- marked 正常解析后把占位换回 KaTeX 输出
- 这样公式里的 * _ \ { } 不会被 marked 当成强调/转义

行内 $ 的边界判断:前后紧邻字母/数字时不识别,避免「花了 $5 和 $6」被当公式。

KaTeX 0.16.11 本地 vendor(592KB,含 20 个 woff2 字体),不走 CDN。
md 预览的新窗口用根绝对路径引入 CSS,blob 文档的相对路径找不到字体。
This commit is contained in:
2026-11-17 22:42:48 +08:00
parent 85984e1ee2
commit 488734641d
27 changed files with 141 additions and 3 deletions
+7 -1
View File
@@ -48,7 +48,13 @@
if (typeof marked !== 'undefined') marked.setOptions({ breaks: true, gfm: true });
function mdHtml(text) {
if (typeof marked === 'undefined') return { html: esc(text), fmt: false };
try { return { html: marked.parse(text || ''), fmt: true }; }
try {
// 公式先抠成占位、marked 解析完再填回 KaTeX 输出(见 math.js)
const html = typeof mdParseWithMath === 'function'
? mdParseWithMath(text || '', marked)
: marked.parse(text || '');
return { html: html, fmt: true };
}
catch (_) { return { html: esc(text), fmt: false }; }
}
function fmtTime(ts) {