koishi-plugin-luogu-saver-bot 0.1.5 → 0.1.7
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.
- package/lib/index.js +37 -32
- package/package.json +3 -3
package/lib/index.js
CHANGED
|
@@ -54,8 +54,6 @@ __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"));
|
|
59
57
|
var name = "luogu-saver-bot";
|
|
60
58
|
var inject = ["puppeteer"];
|
|
61
59
|
var Config = import_koishi.Schema.object({
|
|
@@ -131,25 +129,17 @@ var LuoguSaverClient = class {
|
|
|
131
129
|
return res.data;
|
|
132
130
|
}
|
|
133
131
|
};
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
} catch (__) {
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + "</code></pre>";
|
|
149
|
-
}, "highlight")
|
|
150
|
-
});
|
|
151
|
-
md.use(import_markdown_it_katex.default);
|
|
152
|
-
function generateHtml(title, authorInfo, markdownContent) {
|
|
132
|
+
async function generateHtml(title, authorInfo, markdownContent) {
|
|
133
|
+
const { katex } = await import("@mdit/plugin-katex");
|
|
134
|
+
const md = new import_markdown_it.default({
|
|
135
|
+
html: true,
|
|
136
|
+
breaks: true
|
|
137
|
+
}).use(katex, {
|
|
138
|
+
allowFunctionInTextMode: true,
|
|
139
|
+
// 允许在文本模式下使用函数
|
|
140
|
+
strict: false
|
|
141
|
+
// 禁用严格模式,防止因不标准语法报错
|
|
142
|
+
});
|
|
153
143
|
const renderedBody = md.render(markdownContent);
|
|
154
144
|
return `<!doctype html>
|
|
155
145
|
<html>
|
|
@@ -339,32 +329,43 @@ function apply(ctx, config = {}) {
|
|
|
339
329
|
const rawContent = art.content ?? art.renderedContent ?? "";
|
|
340
330
|
const title = art.title ?? "";
|
|
341
331
|
const authorInfo = `作者 UID: ${art.authorId}`;
|
|
342
|
-
const html = generateHtml(title, authorInfo, rawContent);
|
|
332
|
+
const html = await generateHtml(title, authorInfo, rawContent);
|
|
343
333
|
if (!ctx.puppeteer) return "当前没有可用的 puppeteer 服务。";
|
|
344
334
|
const page = await ctx.puppeteer.page();
|
|
345
335
|
try {
|
|
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
|
-
|
|
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)
|
|
367
|
-
|
|
365
|
+
if (loaded === imgs.length) {
|
|
366
|
+
clearTimeout(timeout);
|
|
367
|
+
resolve(null);
|
|
368
|
+
}
|
|
368
369
|
}));
|
|
369
370
|
} catch (e) {
|
|
370
371
|
}
|
|
@@ -384,13 +385,17 @@ function apply(ctx, config = {}) {
|
|
|
384
385
|
const rawContent = paste.content ?? paste.renderedContent ?? "";
|
|
385
386
|
const title = `剪贴板: ${paste.id}`;
|
|
386
387
|
const authorInfo = paste.author ? `创建者: ${paste.author.name} (UID: ${paste.author.id})` : `创建者 UID: ${paste.authorId}`;
|
|
387
|
-
const html = generateHtml(title, authorInfo, rawContent);
|
|
388
|
+
const html = await generateHtml(title, authorInfo, rawContent);
|
|
388
389
|
if (!ctx.puppeteer) return "当前没有可用的 puppeteer 服务。";
|
|
389
390
|
const page = await ctx.puppeteer.page();
|
|
390
391
|
try {
|
|
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.
|
|
4
|
+
"version": "0.1.7",
|
|
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
|
}
|