@analogjs/content
Version:
Content Rendering for Analog
49 lines (45 loc) • 1.57 kB
JavaScript
import satori from 'satori';
import { html } from 'satori-html';
import sharp from 'sharp';
// Credit for modified source: https://github.com/etherCorps/sveltekit-og/blob/main/src/lib/api.ts
const generateImage = async (element, options) => {
const elementHtml = html(element);
const svg = await satori(elementHtml, {
width: options.width || 1200,
height: options.height || 630,
fonts: options.fonts?.length ? options.fonts : [],
tailwindConfig: options.tailwindConfig,
});
const svgBuffer = Buffer.from(svg);
const png = sharp(svgBuffer).png().toBuffer();
const pngBuffer = await png;
return pngBuffer;
};
class ImageResponse extends Response {
constructor(element, options = {}) {
super();
const body = new ReadableStream({
async start(controller) {
const buffer = await generateImage(element, options);
controller.enqueue(buffer);
controller.close();
},
});
return new Response(body, {
headers: {
'Content-Type': 'image/png',
'Cache-Control': options.debug
? 'no-cache, no-store'
: 'public, immutable, no-transform, max-age=31536000',
...options.headers,
},
status: options.status || 200,
statusText: options.statusText,
});
}
}
/**
* Generated bundle index. Do not edit.
*/
export { ImageResponse };
//# sourceMappingURL=analogjs-content-og.mjs.map