Просмотр исходного кода

feat(Puppeteer): 支持直接传入模板内容

在 `HtmlImg` 类的 `render` 方法中,新增 `templateIsPath` 参数,允许用户直接传入模板内容而不必指定文件路径。这样可以更灵活地处理模板,避免不必要的文件读取操作。
Sakulin 3 месяцев назад
Родитель
Сommit
a3d3ed0862
2 измененных файлов с 12 добавлено и 5 удалено
  1. 7 2
      src/lib/Plugins.ts
  2. 5 3
      src/lib/Puppeteer.ts

+ 7 - 2
src/lib/Plugins.ts

@@ -425,8 +425,12 @@ async function handleCommand(context: PrivateFriendMessage | PrivateGroupMessage
         // 检查是否有模板配置
         if (result?.template?.enabled) {
             try {
+                let templateIsPath: boolean = true;
+                const templateHtml = result.template.html;
                 const templatePath = result.template.path;
-                if (!templatePath || !fs.existsSync(templatePath)) {
+                if (templateHtml) {
+                    templateIsPath = false;
+                } else if (!templatePath || !fs.existsSync(templatePath)) {
                     throw new Error(`Template not found: ${templatePath}`);
                 }
 
@@ -434,7 +438,8 @@ async function handleCommand(context: PrivateFriendMessage | PrivateGroupMessage
                 const htmlImg = new HtmlImg();
                 try {
                     const img = await htmlImg.render({
-                        template: templatePath,
+                        template: templateIsPath ? templatePath : templateHtml,
+                        templateIsPath,
                         data: result,
                         width: result.template.render?.width || 800,
                         height: result.template.render?.height || 600,

+ 5 - 3
src/lib/Puppeteer.ts

@@ -18,6 +18,7 @@ export class HtmlImg {
 
     async render(options: {
         template: string;
+        templateIsPath?: boolean;
         data: any;
         width?: number;
         height?: number;
@@ -28,9 +29,10 @@ export class HtmlImg {
     }) {
         try {
             await this.init();
-            
+
             const {
                 template,
+                templateIsPath = true,
                 data,
                 width = 800,
                 height = 600,
@@ -41,8 +43,8 @@ export class HtmlImg {
             } = options;
 
             // 读取模板
-            const templateContent = fs.readFileSync(template, 'utf-8');
-            
+            const templateContent = templateIsPath ? fs.readFileSync(template, 'utf-8') : template;
+
             // 渲染HTML
 
             const html = art.render(templateContent, data);