koishi-plugin-latex-render 1.1.1 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +14 -5
  2. package/lib/index.js +14 -5
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -80,7 +80,7 @@ function autoWrapLatex(text) {
80
80
  }
81
81
  const leading = chunk.slice(0, chunk.indexOf(trimmed));
82
82
  const trailing = chunk.slice(chunk.indexOf(trimmed) + trimmed.length);
83
- const mathMatch = trimmed.match(/^([\s::,,。;;]*)(.*?)([\s::,,。;;]*)$/);
83
+ const mathMatch = trimmed.match(/^([\s::,,。;;\*\#`~]*)(.*?)([\s::,,。;;\*\#`~]*)$/);
84
84
  const prefix = mathMatch ? mathMatch[1] : "";
85
85
  const coreMath = mathMatch ? mathMatch[2] : trimmed;
86
86
  const suffix = mathMatch ? mathMatch[3] : "";
@@ -156,7 +156,9 @@ function generateHtml(content, config) {
156
156
  const katexOptions = {
157
157
  throwOnError: false,
158
158
  // 保持不报错降级
159
- strict: false
159
+ strict: false,
160
+ output: "html"
161
+ // 强制只输出 HTML 排版,绝不生成辅助阅读的 MathML
160
162
  };
161
163
  if (hasMarkdown) {
162
164
  const latexCache = [];
@@ -219,8 +221,13 @@ ${placeholder}
219
221
  <html>
220
222
  <head>
221
223
  <meta charset="UTF-8">
222
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">
224
+ <link rel="stylesheet" href="https://cdn.staticfile.net/KaTeX/0.16.9/katex.min.css">
223
225
  <style>
226
+ /* 🌟 彻底干掉辅助阅读的 MathML,防止重影 */
227
+ .katex-mathml {
228
+ display: none !important;
229
+ }
230
+
224
231
  * { margin: 0; padding: 0; box-sizing: border-box; }
225
232
  body {
226
233
  font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
@@ -296,10 +303,12 @@ async function renderLatex(ctx, content, config) {
296
303
  try {
297
304
  page = await ctx.puppeteer.page();
298
305
  await page.setContent(html, {
299
- waitUntil: "networkidle0",
306
+ waitUntil: "networkidle2",
307
+ // 修改 1:改成 networkidle2,防止国内 CDN 少量挂起导致超时
300
308
  timeout: 3e4
301
309
  });
302
- await new Promise((resolve) => setTimeout(resolve, 800));
310
+ await page.evaluateHandle("document.fonts.ready");
311
+ await new Promise((resolve) => setTimeout(resolve, 500));
303
312
  const actualHeight = await page.evaluate(() => document.body ? document.body.scrollHeight : 0);
304
313
  const finalHeight = Math.max(actualHeight + 20, height);
305
314
  const buffer = await page.screenshot({
package/lib/index.js CHANGED
@@ -80,7 +80,7 @@ function autoWrapLatex(text) {
80
80
  }
81
81
  const leading = chunk.slice(0, chunk.indexOf(trimmed));
82
82
  const trailing = chunk.slice(chunk.indexOf(trimmed) + trimmed.length);
83
- const mathMatch = trimmed.match(/^([\s::,,。;;]*)(.*?)([\s::,,。;;]*)$/);
83
+ const mathMatch = trimmed.match(/^([\s::,,。;;\*\#`~]*)(.*?)([\s::,,。;;\*\#`~]*)$/);
84
84
  const prefix = mathMatch ? mathMatch[1] : "";
85
85
  const coreMath = mathMatch ? mathMatch[2] : trimmed;
86
86
  const suffix = mathMatch ? mathMatch[3] : "";
@@ -156,7 +156,9 @@ function generateHtml(content, config) {
156
156
  const katexOptions = {
157
157
  throwOnError: false,
158
158
  // 保持不报错降级
159
- strict: false
159
+ strict: false,
160
+ output: "html"
161
+ // 强制只输出 HTML 排版,绝不生成辅助阅读的 MathML
160
162
  };
161
163
  if (hasMarkdown) {
162
164
  const latexCache = [];
@@ -219,8 +221,13 @@ ${placeholder}
219
221
  <html>
220
222
  <head>
221
223
  <meta charset="UTF-8">
222
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">
224
+ <link rel="stylesheet" href="https://cdn.staticfile.net/KaTeX/0.16.9/katex.min.css">
223
225
  <style>
226
+ /* 🌟 彻底干掉辅助阅读的 MathML,防止重影 */
227
+ .katex-mathml {
228
+ display: none !important;
229
+ }
230
+
224
231
  * { margin: 0; padding: 0; box-sizing: border-box; }
225
232
  body {
226
233
  font-family: "Microsoft YaHei", "PingFang SC", sans-serif;
@@ -296,10 +303,12 @@ async function renderLatex(ctx, content, config) {
296
303
  try {
297
304
  page = await ctx.puppeteer.page();
298
305
  await page.setContent(html, {
299
- waitUntil: "networkidle0",
306
+ waitUntil: "networkidle2",
307
+ // 修改 1:改成 networkidle2,防止国内 CDN 少量挂起导致超时
300
308
  timeout: 3e4
301
309
  });
302
- await new Promise((resolve) => setTimeout(resolve, 800));
310
+ await page.evaluateHandle("document.fonts.ready");
311
+ await new Promise((resolve) => setTimeout(resolve, 500));
303
312
  const actualHeight = await page.evaluate(() => document.body ? document.body.scrollHeight : 0);
304
313
  const finalHeight = Math.max(actualHeight + 20, height);
305
314
  const buffer = await page.screenshot({
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-latex-render",
3
3
  "description": "ChatLuna 专用 - 将 AI 返回的 LaTeX 公式渲染为图片",
4
- "version": "1.1.1",
4
+ "version": "1.1.5",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [