-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathog-image-astro.ts
More file actions
259 lines (243 loc) · 8 KB
/
Copy pathog-image-astro.ts
File metadata and controls
259 lines (243 loc) · 8 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* Dynamic OG Image Generator for Astro 5
*
* Generates a 1200x630 PNG at build time via Satori + resvg.
* Place this file at: src/pages/og-image.png.ts
*
* Dependencies:
* pnpm add satori @resvg/resvg-js @fontsource/inter
*
* Usage:
* The file is auto-served at /og-image.png
* Reference it from your Layout:
* <meta property="og:image" content="/og-image.png" />
*
* IMPORTANT: Delete any existing public/og-image.png
* Static files in public/ take priority over API routes in dev mode.
*/
import type { APIRoute } from 'astro'
import satori from 'satori'
import { Resvg } from '@resvg/resvg-js'
import { readdirSync, readFileSync } from 'fs'
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
// ---------------------------------------------------------------------------
// Optional: count dynamic stats from your content at build time
// Remove if you don't need this
// ---------------------------------------------------------------------------
function countContentFiles(contentDir: string, extension = '.md'): number {
try {
let total = 0
const entries = readdirSync(contentDir, { withFileTypes: true })
for (const entry of entries) {
if (entry.isDirectory()) {
const files = readdirSync(resolve(contentDir, entry.name))
total += files.filter(f => f.endsWith(extension)).length
} else if (entry.name.endsWith(extension)) {
total++
}
}
return total
} catch {
return 0
}
}
// ---------------------------------------------------------------------------
// Stats to display — edit these to match your project
// ---------------------------------------------------------------------------
function getStats() {
// Example: count quiz questions dynamically
const questionCount = countContentFiles(
resolve(__dirname, '../content/questions')
)
return [
{ value: questionCount > 0 ? `${questionCount}` : '200+', label: 'QUIZ QUESTIONS' },
{ value: '50+', label: 'TEMPLATES' },
{ value: '10k+', label: 'LINES OF DOCS' },
{ value: '500+', label: 'GITHUB STARS' },
]
}
// ---------------------------------------------------------------------------
// Stat card component (reused for each stat)
// ---------------------------------------------------------------------------
function statCard(value: string, label: string) {
return {
type: 'div',
props: {
style: {
background: '#161b22',
border: '1px solid #30363d',
borderRadius: '10px',
padding: '16px 24px',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
minWidth: '200px',
},
children: [
{
type: 'span',
props: {
style: { fontSize: '36px', fontWeight: 800, color: '#f0883e', lineHeight: 1.2 },
children: value,
},
},
{
type: 'span',
props: {
style: { fontSize: '13px', color: '#8b949e', letterSpacing: '0.1em', fontWeight: 500, marginTop: '4px' },
children: label,
},
},
],
},
}
}
// ---------------------------------------------------------------------------
// Main route handler
// ---------------------------------------------------------------------------
export const GET: APIRoute = () => {
const stats = getStats()
// Font: use local woff (not woff2) from @fontsource — satori requires woff1 or TTF
// woff2 and remote CDN URLs will fail silently or throw "Unsupported OpenType signature"
const fontPath = resolve(
__dirname,
'../../node_modules/@fontsource/inter/files/inter-latin-400-normal.woff'
)
const fontData: ArrayBuffer = readFileSync(fontPath).buffer as ArrayBuffer
const svg = satori(
{
type: 'div',
props: {
style: {
width: '1200px',
height: '630px',
background: 'linear-gradient(135deg, #0d1117 0%, #161b22 50%, #0d1117 100%)',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
fontFamily: 'Inter, sans-serif',
position: 'relative',
padding: '60px',
},
children: [
// Subtle dot grid overlay
{
type: 'div',
props: {
style: {
position: 'absolute',
top: 0, left: 0, right: 0, bottom: 0,
backgroundImage: 'radial-gradient(circle, #ffffff08 1px, transparent 1px)',
backgroundSize: '40px 40px',
},
},
},
// Top badge pill — edit label to match your project type
{
type: 'div',
props: {
style: {
background: '#21262d',
border: '1px solid #30363d',
borderRadius: '20px',
padding: '6px 16px',
color: '#e6edf3',
fontSize: '14px',
letterSpacing: '0.08em',
fontWeight: 600,
marginBottom: '24px',
},
children: 'FREE & OPEN SOURCE', // edit this
},
},
// Title block — two lines, first white, second orange
{
type: 'div',
props: {
style: { display: 'flex', flexDirection: 'column', alignItems: 'center', marginBottom: '12px' },
children: [
{
type: 'span',
props: {
style: { fontSize: '72px', fontWeight: 800, color: '#e6edf3', lineHeight: 1.1 },
children: 'Your Project', // edit this
},
},
{
type: 'span',
props: {
style: { fontSize: '72px', fontWeight: 800, color: '#f0883e', lineHeight: 1.1 },
children: 'Name Here', // edit this
},
},
],
},
},
// Subtitle
{
type: 'div',
props: {
style: { fontSize: '20px', color: '#8b949e', marginBottom: '40px', textAlign: 'center' },
children: 'Your project tagline goes here', // edit this
},
},
// Stats row
{
type: 'div',
props: {
style: { display: 'flex', gap: '16px', marginBottom: '40px' },
children: stats.map(s => statCard(s.value, s.label)),
},
},
// Author signature — bottom left
// Remove this block if you don't want it
{
type: 'div',
props: {
style: {
position: 'absolute',
bottom: '36px',
left: '48px',
display: 'flex',
alignItems: 'center',
gap: '14px',
},
children: [
{
type: 'span',
props: {
style: { fontSize: '28px', fontWeight: 800, color: '#c0522a', letterSpacing: '-0.02em' },
children: 'FB.', // your initials
},
},
{
type: 'span',
props: {
style: { fontSize: '16px', color: '#8b949e', fontWeight: 400 },
children: 'your-domain.com', // your domain
},
},
],
},
},
],
},
},
{
width: 1200,
height: 630,
fonts: [{ name: 'Inter', data: fontData, weight: 400, style: 'normal' }],
}
)
const resvg = new Resvg(svg as unknown as string, { fitTo: { mode: 'width', value: 1200 } })
const png = resvg.render().asPng()
return new Response(png.buffer as ArrayBuffer, {
headers: {
'Content-Type': 'image/png',
'Cache-Control': 'public, max-age=86400',
},
})
}