Put back sharp

Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
This commit is contained in:
Joshua Castle
2024-03-24 02:02:02 -07:00
parent 79a0aeb126
commit 0ca1cf0334
5 changed files with 194 additions and 90 deletions
+1
View File
@@ -9,6 +9,7 @@ export type ImageGeneratorResult = {
export declare class ImageGenerator {
private args;
private satori;
private sharp;
private cache;
private outDir;
constructor(args: {
+3 -4
View File
@@ -6,10 +6,10 @@ const fs = tslib_1.__importStar(require("fs"));
const fsp = tslib_1.__importStar(require("fs/promises"));
const object_hash_1 = tslib_1.__importDefault(require("object-hash"));
const path_1 = tslib_1.__importDefault(require("path"));
const resvg_js_1 = require("@resvg/resvg-js");
class ImageGenerator {
args;
satori;
sharp;
cache = {};
outDir = '';
constructor(args) {
@@ -18,6 +18,7 @@ class ImageGenerator {
}
init = async () => {
this.satori = await import('satori').then((mod) => mod.default);
this.sharp = await import('sharp').then((mod) => mod.default);
if (!fs.existsSync(this.outDir))
await fsp.mkdir(this.outDir, { recursive: true });
};
@@ -29,9 +30,7 @@ class ImageGenerator {
const absolutePath = path_1.default.join(this.outDir, imageName);
const relativePath = path_1.default.join('/', this.args.dir, absolutePath.slice(this.outDir.length));
const svg = await this.satori(element, options);
const resvg = new resvg_js_1.Resvg(svg);
const pngOutput = resvg.render();
await fsp.writeFile(absolutePath, pngOutput.asPng());
await this.sharp(Buffer.from(svg)).png().toFile(absolutePath);
const url = new URL(this.args.websiteUrl);
url.pathname = relativePath;
this.cache[hash] = {