kist 0.1.36 → 0.1.37

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.
@@ -2,7 +2,7 @@ import { Action } from "../../core/pipeline/Action";
2
2
  import { ActionOptionsType } from "../../types/ActionOptionsType";
3
3
  /**
4
4
  * SvgToPngAction converts SVG content to PNG format.
5
- * Uses `resvg-js` for conversion and `jsdom` for SVG element manipulation.
5
+ * Uses `canvg` for conversion and `jsdom` for SVG element manipulation.
6
6
  */
7
7
  export declare class SvgToPngAction extends Action {
8
8
  /**
@@ -16,21 +16,26 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
16
16
  };
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.SvgToPngAction = void 0;
19
- const resvg_js_1 = require("@resvg/resvg-js");
19
+ const canvas_1 = require("canvas");
20
+ const canvg_1 = require("canvg");
20
21
  const fs_1 = __importDefault(require("fs"));
21
22
  const jsdom_1 = require("jsdom");
22
23
  const path_1 = __importDefault(require("path"));
23
24
  const Action_1 = require("../../core/pipeline/Action");
24
25
  // ============================================================================
26
+ // Utilities
27
+ // ============================================================================
28
+ function getSafeRenderingContext(canvas) {
29
+ return canvas.getContext("2d");
30
+ }
31
+ // ============================================================================
25
32
  // Classes
26
33
  // ============================================================================
27
34
  /**
28
35
  * SvgToPngAction converts SVG content to PNG format.
29
- * Uses `resvg-js` for conversion and `jsdom` for SVG element manipulation.
36
+ * Uses `canvg` for conversion and `jsdom` for SVG element manipulation.
30
37
  */
31
38
  class SvgToPngAction extends Action_1.Action {
32
- // Methods
33
- // ========================================================================
34
39
  /**
35
40
  * Executes the SVG-to-PNG conversion process.
36
41
  * @param options - Options including SVG content, output path, width,
@@ -63,29 +68,27 @@ class SvgToPngAction extends Action_1.Action {
63
68
  convert(svgContent, outputPath, width, height) {
64
69
  return __awaiter(this, void 0, void 0, function* () {
65
70
  try {
66
- // Ensure the output directory exists
67
71
  const outputDir = path_1.default.dirname(outputPath);
68
72
  if (!fs_1.default.existsSync(outputDir)) {
69
73
  fs_1.default.mkdirSync(outputDir, { recursive: true });
70
74
  }
71
- // Parse SVG and apply width/height
72
75
  const dom = new jsdom_1.JSDOM(svgContent);
73
76
  const svgElement = dom.window.document.querySelector("svg");
74
77
  if (!svgElement) {
75
78
  throw new Error("Invalid SVG content");
76
79
  }
77
- if (width)
78
- svgElement.setAttribute("width", width.toString());
79
- if (height)
80
- svgElement.setAttribute("height", height.toString());
80
+ const w = width ||
81
+ parseInt(svgElement.getAttribute("width") || "800", 10);
82
+ const h = height ||
83
+ parseInt(svgElement.getAttribute("height") || "600", 10);
84
+ svgElement.setAttribute("width", w.toString());
85
+ svgElement.setAttribute("height", h.toString());
81
86
  const updatedSvgContent = svgElement.outerHTML;
82
- // Render using Resvg
83
- const resvg = new resvg_js_1.Resvg(updatedSvgContent, {
84
- fitTo: width && height
85
- ? { mode: "width", value: width }
86
- : undefined,
87
- });
88
- const pngBuffer = resvg.render().asPng();
87
+ const canvas = (0, canvas_1.createCanvas)(w, h);
88
+ const ctx = getSafeRenderingContext(canvas);
89
+ const canvg = yield canvg_1.Canvg.from(ctx, updatedSvgContent);
90
+ yield canvg.render();
91
+ const pngBuffer = canvas.toBuffer("image/png");
89
92
  fs_1.default.writeFileSync(outputPath, pngBuffer);
90
93
  }
91
94
  catch (error) {
@@ -98,7 +101,7 @@ class SvgToPngAction extends Action_1.Action {
98
101
  * @returns A string description of the action.
99
102
  */
100
103
  describe() {
101
- return "Converts SVG content to PNG format with optional resizing using resvg-js.";
104
+ return "Converts SVG content to PNG format with optional resizing using canvg and canvas.";
102
105
  }
103
106
  }
104
107
  exports.SvgToPngAction = SvgToPngAction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kist",
3
- "version": "0.1.36",
3
+ "version": "0.1.37",
4
4
  "description": "Package Pipeline Processor",
5
5
  "keywords": [
6
6
  "kist",
@@ -60,15 +60,14 @@
60
60
  "@babel/core": "^7.23.6",
61
61
  "@babel/preset-env": "^7.23.6",
62
62
  "@babel/preset-typescript": "^7.23.3",
63
- "@resvg/resvg-js": "^2.6.2",
64
63
  "@types/fs-extra": "^11.0.4",
65
64
  "@types/glob": "^8.1.0",
66
- "@types/jsdom": "^21.1.6",
67
65
  "@types/micromatch": "^4.0.9",
68
- "@types/node": "^22.13.0",
69
66
  "@types/nunjucks": "^3.2.6",
70
67
  "@types/svg-sprite": "^0.0.39",
71
68
  "autoprefixer": "^10.4.21",
69
+ "canvas": "^3.1.0",
70
+ "canvg": "^4.0.3",
72
71
  "chokidar": "^4.0.0",
73
72
  "cssnano": "^7.0.4",
74
73
  "del": "^8.0.0",
@@ -80,7 +79,7 @@
80
79
  "fs-extra": "^11.2.0",
81
80
  "glob": "^11.0.0",
82
81
  "js-yaml": "^4.1.0",
83
- "jsdom": "^26.0.0",
82
+ "jsdom": "^26.1.0",
84
83
  "lodash": "^4.17.21",
85
84
  "nunjucks": "^3.2.4",
86
85
  "postcss": "^8.4.32",
@@ -2,25 +2,31 @@
2
2
  // Imports
3
3
  // ============================================================================
4
4
 
5
- import { Resvg } from "@resvg/resvg-js";
5
+ import { createCanvas } from "canvas";
6
+ import { Canvg } from "canvg";
6
7
  import fs from "fs";
7
8
  import { JSDOM } from "jsdom";
8
9
  import path from "path";
9
10
  import { Action } from "../../core/pipeline/Action";
10
11
  import { ActionOptionsType } from "../../types/ActionOptionsType";
11
12
 
13
+ // ============================================================================
14
+ // Utilities
15
+ // ============================================================================
16
+
17
+ function getSafeRenderingContext(canvas: ReturnType<typeof createCanvas>) {
18
+ return canvas.getContext("2d");
19
+ }
20
+
12
21
  // ============================================================================
13
22
  // Classes
14
23
  // ============================================================================
15
24
 
16
25
  /**
17
26
  * SvgToPngAction converts SVG content to PNG format.
18
- * Uses `resvg-js` for conversion and `jsdom` for SVG element manipulation.
27
+ * Uses `canvg` for conversion and `jsdom` for SVG element manipulation.
19
28
  */
20
29
  export class SvgToPngAction extends Action {
21
- // Methods
22
- // ========================================================================
23
-
24
30
  /**
25
31
  * Executes the SVG-to-PNG conversion process.
26
32
  * @param options - Options including SVG content, output path, width,
@@ -60,13 +66,11 @@ export class SvgToPngAction extends Action {
60
66
  height?: number,
61
67
  ): Promise<void> {
62
68
  try {
63
- // Ensure the output directory exists
64
69
  const outputDir = path.dirname(outputPath);
65
70
  if (!fs.existsSync(outputDir)) {
66
71
  fs.mkdirSync(outputDir, { recursive: true });
67
72
  }
68
73
 
69
- // Parse SVG and apply width/height
70
74
  const dom = new JSDOM(svgContent);
71
75
  const svgElement = dom.window.document.querySelector("svg");
72
76
 
@@ -74,21 +78,25 @@ export class SvgToPngAction extends Action {
74
78
  throw new Error("Invalid SVG content");
75
79
  }
76
80
 
77
- if (width) svgElement.setAttribute("width", width.toString());
78
- if (height) svgElement.setAttribute("height", height.toString());
81
+ const w =
82
+ width ||
83
+ parseInt(svgElement.getAttribute("width") || "800", 10);
84
+ const h =
85
+ height ||
86
+ parseInt(svgElement.getAttribute("height") || "600", 10);
87
+
88
+ svgElement.setAttribute("width", w.toString());
89
+ svgElement.setAttribute("height", h.toString());
79
90
 
80
91
  const updatedSvgContent = svgElement.outerHTML;
81
92
 
82
- // Render using Resvg
83
- const resvg = new Resvg(updatedSvgContent, {
84
- fitTo:
85
- width && height
86
- ? { mode: "width", value: width }
87
- : undefined,
88
- });
93
+ const canvas = createCanvas(w, h);
94
+ const ctx = getSafeRenderingContext(canvas) as unknown as any;
89
95
 
90
- const pngBuffer = resvg.render().asPng();
96
+ const canvg = await Canvg.from(ctx, updatedSvgContent);
97
+ await canvg.render();
91
98
 
99
+ const pngBuffer = canvas.toBuffer("image/png");
92
100
  fs.writeFileSync(outputPath, pngBuffer);
93
101
  } catch (error) {
94
102
  throw new Error(
@@ -102,7 +110,7 @@ export class SvgToPngAction extends Action {
102
110
  * @returns A string description of the action.
103
111
  */
104
112
  describe(): string {
105
- return "Converts SVG content to PNG format with optional resizing using resvg-js.";
113
+ return "Converts SVG content to PNG format with optional resizing using canvg and canvas.";
106
114
  }
107
115
  }
108
116