kist 0.1.34 → 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 `
|
|
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 canvas_1 = require("canvas");
|
|
20
|
+
const canvg_1 = require("canvg");
|
|
19
21
|
const fs_1 = __importDefault(require("fs"));
|
|
20
22
|
const jsdom_1 = require("jsdom");
|
|
21
23
|
const path_1 = __importDefault(require("path"));
|
|
22
|
-
const sharp_1 = __importDefault(require("sharp"));
|
|
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 `
|
|
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,30 +68,28 @@ 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
|
-
// Create a JSDOM instance to parse the SVG
|
|
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
|
-
|
|
78
|
-
svgElement.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
// Serialize the updated SVG content
|
|
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());
|
|
84
86
|
const updatedSvgContent = svgElement.outerHTML;
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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");
|
|
92
|
+
fs_1.default.writeFileSync(outputPath, pngBuffer);
|
|
90
93
|
}
|
|
91
94
|
catch (error) {
|
|
92
95
|
throw new Error(`Error converting SVG to PNG: ${error.message}`);
|
|
@@ -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.";
|
|
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.
|
|
3
|
+
"version": "0.1.37",
|
|
4
4
|
"description": "Package Pipeline Processor",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kist",
|
|
@@ -62,12 +62,12 @@
|
|
|
62
62
|
"@babel/preset-typescript": "^7.23.3",
|
|
63
63
|
"@types/fs-extra": "^11.0.4",
|
|
64
64
|
"@types/glob": "^8.1.0",
|
|
65
|
-
"@types/jsdom": "^21.1.6",
|
|
66
65
|
"@types/micromatch": "^4.0.9",
|
|
67
|
-
"@types/node": "^22.13.0",
|
|
68
66
|
"@types/nunjucks": "^3.2.6",
|
|
69
67
|
"@types/svg-sprite": "^0.0.39",
|
|
70
68
|
"autoprefixer": "^10.4.21",
|
|
69
|
+
"canvas": "^3.1.0",
|
|
70
|
+
"canvg": "^4.0.3",
|
|
71
71
|
"chokidar": "^4.0.0",
|
|
72
72
|
"cssnano": "^7.0.4",
|
|
73
73
|
"del": "^8.0.0",
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"fs-extra": "^11.2.0",
|
|
80
80
|
"glob": "^11.0.0",
|
|
81
81
|
"js-yaml": "^4.1.0",
|
|
82
|
-
"jsdom": "^26.
|
|
82
|
+
"jsdom": "^26.1.0",
|
|
83
83
|
"lodash": "^4.17.21",
|
|
84
84
|
"nunjucks": "^3.2.4",
|
|
85
85
|
"postcss": "^8.4.32",
|
|
@@ -88,7 +88,6 @@
|
|
|
88
88
|
"sass": "^1.69.7",
|
|
89
89
|
"sassdoc": "^2.7.4",
|
|
90
90
|
"semver": "^7.5.4",
|
|
91
|
-
"sharp": "^0.34.1",
|
|
92
91
|
"svg-sprite": "^2.0.2",
|
|
93
92
|
"svgo": "^3.1.0",
|
|
94
93
|
"terser": "^5.26.0",
|
|
@@ -2,25 +2,31 @@
|
|
|
2
2
|
// Imports
|
|
3
3
|
// ============================================================================
|
|
4
4
|
|
|
5
|
+
import { createCanvas } from "canvas";
|
|
6
|
+
import { Canvg } from "canvg";
|
|
5
7
|
import fs from "fs";
|
|
6
8
|
import { JSDOM } from "jsdom";
|
|
7
9
|
import path from "path";
|
|
8
|
-
import sharp from "sharp";
|
|
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 `
|
|
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
|
-
// Create a JSDOM instance to parse the SVG
|
|
70
74
|
const dom = new JSDOM(svgContent);
|
|
71
75
|
const svgElement = dom.window.document.querySelector("svg");
|
|
72
76
|
|
|
@@ -74,22 +78,26 @@ export class SvgToPngAction extends Action {
|
|
|
74
78
|
throw new Error("Invalid SVG content");
|
|
75
79
|
}
|
|
76
80
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
const w =
|
|
82
|
+
width ||
|
|
83
|
+
parseInt(svgElement.getAttribute("width") || "800", 10);
|
|
84
|
+
const h =
|
|
85
|
+
height ||
|
|
86
|
+
parseInt(svgElement.getAttribute("height") || "600", 10);
|
|
80
87
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
88
|
+
svgElement.setAttribute("width", w.toString());
|
|
89
|
+
svgElement.setAttribute("height", h.toString());
|
|
84
90
|
|
|
85
|
-
// Serialize the updated SVG content
|
|
86
91
|
const updatedSvgContent = svgElement.outerHTML;
|
|
87
92
|
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
await
|
|
93
|
+
const canvas = createCanvas(w, h);
|
|
94
|
+
const ctx = getSafeRenderingContext(canvas) as unknown as any;
|
|
95
|
+
|
|
96
|
+
const canvg = await Canvg.from(ctx, updatedSvgContent);
|
|
97
|
+
await canvg.render();
|
|
98
|
+
|
|
99
|
+
const pngBuffer = canvas.toBuffer("image/png");
|
|
100
|
+
fs.writeFileSync(outputPath, pngBuffer);
|
|
93
101
|
} catch (error) {
|
|
94
102
|
throw new Error(
|
|
95
103
|
`Error converting SVG to PNG: ${(error as Error).message}`,
|
|
@@ -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.";
|
|
113
|
+
return "Converts SVG content to PNG format with optional resizing using canvg and canvas.";
|
|
106
114
|
}
|
|
107
115
|
}
|
|
108
116
|
|