Skip to content

Commit 5b6c002

Browse files
committed
try inlining font-face and pre-rendering illustration as png
1 parent 64d7725 commit 5b6c002

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

packages/dev/s2-docs/scripts/generateOGImages.mjs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,39 @@ async function getComponentSvg(title) {
271271
svgContent = svgContent.replace(/background:\s*#f4f6fc/g, 'background: #f8f8f8');
272272
svgContent = svgContent.replace(/var\(--anatomy-font\)/g, 'adobe-clean');
273273

274-
// Convert SVG to data URI for use as image source
275-
const svgBase64 = Buffer.from(svgContent).toString('base64');
276-
return `data:image/svg+xml;base64,${svgBase64}`;
274+
const adobeCleanRegularBase64 = Buffer.from(adobeCleanRegular).toString('base64');
275+
const adobeCleanBoldBase64 = Buffer.from(adobeCleanBold).toString('base64');
276+
277+
const fontFaceStyle = `
278+
@font-face {
279+
font-family: 'adobe-clean';
280+
font-style: normal;
281+
font-weight: 400;
282+
src: url(data:font/woff2;base64,${adobeCleanRegularBase64}) format('woff2');
283+
}
284+
@font-face {
285+
font-family: 'adobe-clean';
286+
font-style: normal;
287+
font-weight: 700;
288+
src: url(data:font/woff2;base64,${adobeCleanBoldBase64}) format('woff2');
289+
}
290+
`;
291+
292+
// Inject style tag into SVG
293+
if (svgContent.includes('<defs>')) {
294+
svgContent = svgContent.replace('<defs>', `<defs><style>${fontFaceStyle}</style>`);
295+
} else if (svgContent.includes('<svg')) {
296+
svgContent = svgContent.replace(/<svg([^>]*)>/, `<svg$1><defs><style>${fontFaceStyle}</style></defs>`);
297+
}
298+
299+
// Convert SVG to PNG
300+
const pngBuffer = await sharp(Buffer.from(svgContent))
301+
.png()
302+
.toBuffer();
303+
304+
// Convert PNG to data URI to use as image source
305+
const pngBase64 = pngBuffer.toString('base64');
306+
return `data:image/png;base64,${pngBase64}`;
277307
} catch (error) {
278308
console.warn(`Could not load SVG for ${title}: ${error.message}`);
279309
return null;

0 commit comments

Comments
 (0)