koishi-plugin-vercel-satori-png-service 0.0.2 → 0.0.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.
- package/lib/index.d.ts +8 -2
- package/lib/index.js +39 -6
- package/package.json +5 -5
- package/readme.md +4 -0
package/lib/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Context, Schema, Service } from "koishi";
|
|
|
2
2
|
import { ReactElement } from "react";
|
|
3
3
|
import { Readable } from "stream";
|
|
4
4
|
import type { unstable_createNodejsStream } from "@vercel/og";
|
|
5
|
+
import type { Font } from "satori";
|
|
5
6
|
export type ImageOptions = Parameters<typeof unstable_createNodejsStream>[1];
|
|
6
7
|
declare module "koishi" {
|
|
7
8
|
interface Context {
|
|
@@ -12,12 +13,17 @@ declare class VercelSatoriPngService extends Service {
|
|
|
12
13
|
private _ctx;
|
|
13
14
|
private _config;
|
|
14
15
|
private createNodejsStream;
|
|
16
|
+
private fonts;
|
|
15
17
|
constructor(ctx: Context, config: VercelSatoriPngService.Config);
|
|
16
18
|
start(): Promise<void>;
|
|
17
19
|
jsxToReactElement(jsxCode: string, data?: Record<any, any>): Promise<ReactElement<any, any>>;
|
|
18
|
-
jsxToPng(jsxCode: string, options: ImageOptions, data?: Record<any, any>): Promise<Readable>;
|
|
19
20
|
htmlToReactElement(htmlCode: string): ReactElement<any, any>;
|
|
20
|
-
|
|
21
|
+
addFont(fonts: Font[]): void;
|
|
22
|
+
removeFont(fonts: Font[]): void;
|
|
23
|
+
private buildOptions;
|
|
24
|
+
jsxToPng(jsxCode: string, options?: ImageOptions, data?: Record<any, any>): Promise<Readable>;
|
|
25
|
+
htmlToPng(htmlCode: string, options?: ImageOptions): Promise<Readable>;
|
|
26
|
+
reactElementToPng(reactElement: ReactElement<any, any>, options?: ImageOptions): Promise<Readable>;
|
|
21
27
|
}
|
|
22
28
|
declare namespace VercelSatoriPngService {
|
|
23
29
|
const usage: string;
|
package/lib/index.js
CHANGED
|
@@ -46,6 +46,7 @@ var VercelSatoriPngService = class extends import_koishi.Service {
|
|
|
46
46
|
_ctx;
|
|
47
47
|
_config;
|
|
48
48
|
createNodejsStream;
|
|
49
|
+
fonts = [];
|
|
49
50
|
constructor(ctx, config) {
|
|
50
51
|
super(ctx, serviceName);
|
|
51
52
|
this._ctx = ctx;
|
|
@@ -65,23 +66,55 @@ var VercelSatoriPngService = class extends import_koishi.Service {
|
|
|
65
66
|
"_args_623601",
|
|
66
67
|
"with (_args_623601) {\nreturn " + hCode.replace(/^\s+/, "") + "\n}"
|
|
67
68
|
);
|
|
68
|
-
let res
|
|
69
|
+
let res;
|
|
70
|
+
try {
|
|
71
|
+
res = await fn(import_react.default, data || {});
|
|
72
|
+
} catch (e) {
|
|
73
|
+
e.message = fn.toString() + "\n" + e.message;
|
|
74
|
+
throw e;
|
|
75
|
+
}
|
|
69
76
|
let i = 0;
|
|
70
77
|
while (typeof res === "function" && i++ < 999) {
|
|
71
78
|
res = await res();
|
|
72
79
|
}
|
|
73
80
|
return res;
|
|
74
81
|
}
|
|
75
|
-
async jsxToPng(jsxCode, options, data) {
|
|
76
|
-
const reactElement = await this.jsxToReactElement(jsxCode, data);
|
|
77
|
-
return this.createNodejsStream(reactElement, options);
|
|
78
|
-
}
|
|
79
82
|
htmlToReactElement(htmlCode) {
|
|
80
83
|
return (0, import_html_react_parser.default)(htmlCode);
|
|
81
84
|
}
|
|
85
|
+
addFont(fonts) {
|
|
86
|
+
this.fonts.push(...fonts);
|
|
87
|
+
this.ctx.on("dispose", () => {
|
|
88
|
+
this.removeFont(fonts);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
removeFont(fonts) {
|
|
92
|
+
fonts.forEach((font) => {
|
|
93
|
+
const index = this.fonts.indexOf(font);
|
|
94
|
+
if (index === -1) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this.fonts.splice(index, 1);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
buildOptions(options) {
|
|
101
|
+
options ||= {};
|
|
102
|
+
if (this.fonts.length > 0) {
|
|
103
|
+
options.fonts ||= [];
|
|
104
|
+
options.fonts.push(...this.fonts);
|
|
105
|
+
}
|
|
106
|
+
return options;
|
|
107
|
+
}
|
|
108
|
+
async jsxToPng(jsxCode, options, data) {
|
|
109
|
+
const reactElement = await this.jsxToReactElement(jsxCode, data);
|
|
110
|
+
return this.createNodejsStream(reactElement, this.buildOptions(options));
|
|
111
|
+
}
|
|
82
112
|
htmlToPng(htmlCode, options) {
|
|
83
113
|
const reactElement = this.htmlToReactElement(htmlCode);
|
|
84
|
-
return this.createNodejsStream(reactElement, options);
|
|
114
|
+
return this.createNodejsStream(reactElement, this.buildOptions(options));
|
|
115
|
+
}
|
|
116
|
+
async reactElementToPng(reactElement, options) {
|
|
117
|
+
return this.createNodejsStream(reactElement, this.buildOptions(options));
|
|
85
118
|
}
|
|
86
119
|
};
|
|
87
120
|
((VercelSatoriPngService2) => {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-vercel-satori-png-service",
|
|
3
3
|
"description": "Use Vercel Satori and Resvg.js to convert html to png",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.5",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"jsx to png"
|
|
36
36
|
],
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"koishi": "^4.18.
|
|
38
|
+
"koishi": "^4.18.5"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@vercel/og": "^0.6.4",
|
|
@@ -43,14 +43,14 @@
|
|
|
43
43
|
"react": "^19.0.0",
|
|
44
44
|
"sucrase": "^3.35.0"
|
|
45
45
|
},
|
|
46
|
-
"devDependencies": {
|
|
47
|
-
"@types/react": "^19"
|
|
48
|
-
},
|
|
49
46
|
"koishi": {
|
|
50
47
|
"service": {
|
|
51
48
|
"implements": [
|
|
52
49
|
"vercelSatoriPngService"
|
|
53
50
|
]
|
|
54
51
|
}
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/react": "^19"
|
|
55
55
|
}
|
|
56
56
|
}
|
package/readme.md
CHANGED
|
@@ -7,7 +7,11 @@ Use Vercel Satori and Resvg.js to convert html to png
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
html to ReactElement [html-react-parser](https://www.npmjs.com/package/html-react-parser)
|
|
10
|
+
|
|
10
11
|
jsx to ReactElement [sucrase](https://www.npmjs.com/package/sucrase)
|
|
12
|
+
|
|
11
13
|
ReactElement to png [@vercel/og](https://www.npmjs.com/package/@vercel/og)
|
|
14
|
+
|
|
12
15
|
[og-playground](https://og-playground.vercel.app/)
|
|
16
|
+
|
|
13
17
|
[vercel/satori](https://github.com/vercel/satori#overview)
|