-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.js
More file actions
155 lines (135 loc) · 4.95 KB
/
generate.js
File metadata and controls
155 lines (135 loc) · 4.95 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const fs = require('fs');
const https = require('https');
const data = JSON.parse(fs.readFileSync('sponsors.json', 'utf8'));
function fetchImage(url) {
return new Promise((resolve, reject) => {
const options = {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
};
https.get(url, options, (res) => {
// Handle redirects
if (res.statusCode === 301 || res.statusCode === 302) {
return fetchImage(res.headers.location).then(resolve).catch(reject);
}
if (res.statusCode !== 200) {
console.error(`Failed to fetch ${url}: ${res.statusCode}`);
reject(new Error(`HTTP ${res.statusCode}`));
return;
}
const chunks = [];
res.on('data', (chunk) => chunks.push(chunk));
res.on('end', () => {
const buffer = Buffer.concat(chunks);
const base64 = buffer.toString('base64');
const contentType = res.headers['content-type'] || 'image/png';
resolve(`data:${contentType};base64,${base64}`);
});
}).on('error', reject);
});
}
async function generateSVG(sponsors) {
const avatarSize = 60;
const spacing = 10;
const columns = 6;
const padding = 20;
const rows = Math.ceil(sponsors.length / columns);
const width = (avatarSize + spacing) * columns + padding * 2 - spacing;
const headerHeight = 40;
const height = headerHeight + (avatarSize + spacing + 20) * rows + padding;
let clipPaths = '';
let content = '';
let avatarStyles = '';
// Fetch all images and convert to base64
for (let i = 0; i < sponsors.length; i++) {
const sponsor = sponsors[i];
const col = i % columns;
const row = Math.floor(i / columns);
const x = padding + col * (avatarSize + spacing);
const y = headerHeight + padding + row * (avatarSize + spacing + 20);
const centerX = x + avatarSize / 2;
const centerY = y + avatarSize / 2;
let avatarUrl;
if (sponsor.github) {
avatarUrl = `https://avatars.githubusercontent.com/${sponsor.github}?size=128`;
} else {
avatarUrl = `https://ui-avatars.com/api/?name=${encodeURIComponent(sponsor.name)}&background=random&size=128&format=png`;
}
console.log(`Fetching avatar for ${sponsor.name} from ${avatarUrl}...`);
let base64Image;
try {
base64Image = await fetchImage(avatarUrl);
console.log(`✓ Successfully fetched ${sponsor.name}`);
} catch (err) {
console.error(`✗ Failed to fetch ${sponsor.name}:`, err.message);
// Fallback to generated avatar
avatarUrl = `https://ui-avatars.com/api/?name=${encodeURIComponent(sponsor.name)}&background=random&size=128&format=png`;
base64Image = await fetchImage(avatarUrl);
}
const link = sponsor.github ? `https://github.com/${sponsor.github}` : '#';
clipPaths += `
<clipPath id="circle-${i}">
<circle cx="${centerX}" cy="${centerY}" r="${avatarSize / 2}"/>
</clipPath>
`;
content += `
<a href="${link}" target="_blank" rel="noopener">
<g class="avatar-${i}">
<image
x="${x}"
y="${y}"
width="${avatarSize}"
height="${avatarSize}"
href="${base64Image}"
clip-path="url(#circle-${i})"
/>
</g>
<text x="${centerX}" y="${y + avatarSize + 15}" class="name">${sponsor.name}</text>
</a>
`;
avatarStyles += `
.avatar-${i} {
transform-origin: ${centerX}px ${centerY}px;
transition: transform 0.2s;
}
.avatar-${i}:hover {
transform: scale(1.1);
}
`;
}
return `<?xml version="1.0" encoding="UTF-8"?>
<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">
<defs>
<style>
.title {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
font-size: 24px;
font-weight: 600;
fill: #1f2328;
}
.name {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
font-size: 11px;
fill: #57606a;
text-anchor: middle;
}
${avatarStyles}
</style>
${clipPaths}
</defs>
<rect width="${width}" height="${height}" fill="#ffffff" rx="10"/>
<text x="${width / 2}" y="30" class="title" text-anchor="middle">💖 Supporters</text>
${content}
</svg>`;
}
(async () => {
try {
const svg = await generateSVG(data.sponsors);
fs.writeFileSync('sponsors.svg', svg);
console.log('✓ Generated sponsors.svg with embedded images');
} catch (err) {
console.error('✗ Error generating SVG:', err);
process.exit(1);
}
})();