koishi-plugin-luogu-saver-bot 0.1.5 → 0.1.6

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 (2) hide show
  1. package/lib/index.js +31 -26
  2. package/package.json +3 -3
package/lib/index.js CHANGED
@@ -54,8 +54,7 @@ __name(statusToString, "statusToString");
54
54
 
55
55
  // src/index.ts
56
56
  var import_markdown_it = __toESM(require("markdown-it"));
57
- var import_markdown_it_katex = __toESM(require("markdown-it-katex"));
58
- var import_highlight = __toESM(require("highlight.js"));
57
+ var import_plugin_katex = require("@mdit/plugin-katex");
59
58
  var name = "luogu-saver-bot";
60
59
  var inject = ["puppeteer"];
61
60
  var Config = import_koishi.Schema.object({
@@ -133,22 +132,13 @@ var LuoguSaverClient = class {
133
132
  };
134
133
  var md = new import_markdown_it.default({
135
134
  html: true,
136
- // 允许 HTML 标签
137
- breaks: true,
138
- // 转换换行符为 <br>
139
- linkify: true,
140
- // 自动识别链接
141
- highlight: /* @__PURE__ */ __name(function(str, lang) {
142
- if (lang && import_highlight.default.getLanguage(lang)) {
143
- try {
144
- return '<pre class="hljs"><code>' + import_highlight.default.highlight(str, { language: lang, ignoreIllegals: true }).value + "</code></pre>";
145
- } catch (__) {
146
- }
147
- }
148
- return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + "</code></pre>";
149
- }, "highlight")
135
+ breaks: true
136
+ }).use(import_plugin_katex.katex, {
137
+ allowFunctionInTextMode: true,
138
+ // 允许在文本模式下使用函数
139
+ strict: false
140
+ // 禁用严格模式,防止因不标准语法报错
150
141
  });
151
- md.use(import_markdown_it_katex.default);
152
142
  function generateHtml(title, authorInfo, markdownContent) {
153
143
  const renderedBody = md.render(markdownContent);
154
144
  return `<!doctype html>
@@ -346,25 +336,36 @@ function apply(ctx, config = {}) {
346
336
  const width = Number(options.width) || 960;
347
337
  await page.setViewport({ width, height: 800, deviceScaleFactor: 2 });
348
338
  await page.setContent(html, { waitUntil: "networkidle0" });
339
+ try {
340
+ await page.evaluate(() => document.fonts.ready);
341
+ } catch (e) {
342
+ ctx.logger.warn("等待字体加载超时或失败", e);
343
+ }
349
344
  try {
350
345
  await page.evaluate(() => new Promise((resolve) => {
351
346
  const imgs = Array.from(document.images);
352
347
  if (!imgs.length) return resolve(null);
353
348
  let loaded = 0;
349
+ const timeout = setTimeout(() => resolve(null), 5e3);
350
+ const handler = /* @__PURE__ */ __name(() => {
351
+ loaded++;
352
+ if (loaded === imgs.length) {
353
+ clearTimeout(timeout);
354
+ resolve(null);
355
+ }
356
+ }, "handler");
354
357
  imgs.forEach((img) => {
355
358
  if (img.complete) {
356
359
  loaded++;
357
- return;
360
+ } else {
361
+ img.addEventListener("load", handler);
362
+ img.addEventListener("error", handler);
358
363
  }
359
- const handler = /* @__PURE__ */ __name(() => {
360
- loaded++;
361
- if (loaded === imgs.length) resolve(null);
362
- }, "handler");
363
- img.addEventListener("load", handler);
364
- img.addEventListener("error", handler);
365
364
  });
366
- if (loaded === imgs.length) resolve(null);
367
- setTimeout(() => resolve(null), 5e3);
365
+ if (loaded === imgs.length) {
366
+ clearTimeout(timeout);
367
+ resolve(null);
368
+ }
368
369
  }));
369
370
  } catch (e) {
370
371
  }
@@ -391,6 +392,10 @@ function apply(ctx, config = {}) {
391
392
  const width = Number(options.width) || 960;
392
393
  await page.setViewport({ width, height: 800, deviceScaleFactor: 2 });
393
394
  await page.setContent(html, { waitUntil: "networkidle0" });
395
+ try {
396
+ await page.waitForFunction("window.__katex_render_done === true", { timeout: 2e4 });
397
+ } catch (e) {
398
+ }
394
399
  try {
395
400
  await page.evaluate(() => new Promise((resolve) => {
396
401
  const imgs = Array.from(document.images);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-luogu-saver-bot",
3
3
  "description": "洛谷保存站机器人",
4
- "version": "0.1.5",
4
+ "version": "0.1.6",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -19,8 +19,8 @@
19
19
  "koishi": "^4.18.7"
20
20
  },
21
21
  "dependencies": {
22
+ "@mdit/plugin-katex": "^0.24.1",
22
23
  "highlight.js": "^11.11.1",
23
- "markdown-it": "^14.1.0",
24
- "markdown-it-katex": "^2.0.3"
24
+ "markdown-it": "^14.1.0"
25
25
  }
26
26
  }