forked from daytonaio/ai-enablement-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-image.js
73 lines (60 loc) · 2.21 KB
/
generate-image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const playwright = require('playwright');
const fs = require('fs');
const generateHTML = require('./template');
const path = require('path');
function imageToDataURL(imagePath) {
if (!imagePath) return '';
try {
const imageBuffer = fs.readFileSync(path.resolve(__dirname, imagePath));
const imageExt = path.extname(imagePath).substring(1).toLowerCase();
// Special handling for SVG files
if (imageExt === 'svg') {
return `data:image/svg+xml;base64,${imageBuffer.toString('base64')}`;
}
// For other image formats
return `data:image/${imageExt};base64,${imageBuffer.toString('base64')}`;
} catch (error) {
console.error(`Error loading image: ${imagePath}`, error);
return '';
}
}
async function generateImage() {
// Read and parse the JSON file
const data = JSON.parse(fs.readFileSync('./ai-enablement-stack.json', 'utf8'));
// Process the data while preserving the company object structure
const processedData = {
...data,
layers: data.layers.map(layer => ({
...layer,
sections: layer.sections.map(section => ({
...section,
companies: section.companies.map(company => {
if (typeof company === 'string') {
return { name: company, logo: '' };
}
return {
...company,
logo: imageToDataURL(company.logo)
};
})
}))
}))
};
// Reverse the layers array if it exists in your data structure
if (processedData.layers) {
processedData.layers = processedData.layers.reverse();
}
const html = generateHTML(processedData);
const browser = await playwright.chromium.launch();
const page = await browser.newPage();
await page.setContent(html);
await page.setViewportSize({ width: 1600, height: 1000 });
// Wait for any animations/fonts to load
await page.waitForTimeout(1000);
await page.screenshot({
path: 'ai-enablement-stack.png',
fullPage: true
});
await browser.close();
}
generateImage().catch(console.error);