|
1 | | -import satori from "satori" |
2 | | -import fs from "fs/promises" |
3 | | -import path from "path" |
4 | | -import sharp from "sharp" |
5 | | - |
6 | | -const [bgImg, logo, regularFont, boldFont] = await Promise.all([ |
7 | | - fs.readFile(path.resolve("./src/assets/og/graph-paper.svg"), "base64"), |
8 | | - fs.readFile(path.resolve("./src/assets/og/logo.png"), "base64"), |
9 | | - fs.readFile(path.resolve("./src/assets/og/Barlow-Regular.ttf")), |
10 | | - fs.readFile(path.resolve("./src/assets/og/Barlow-Bold.ttf")), |
11 | | -]) |
12 | | - |
13 | | -const satoriOptions = { |
14 | | - width: 1200, |
15 | | - height: 630, |
16 | | - fonts: [ |
17 | | - { |
18 | | - name: "Barlow", |
19 | | - data: regularFont, |
20 | | - weight: 400, |
21 | | - style: "normal", |
22 | | - }, |
23 | | - { |
24 | | - name: "Barlow", |
25 | | - data: boldFont, |
26 | | - weight: 600, |
27 | | - style: "normal", |
28 | | - }, |
29 | | - ], |
30 | | -} |
31 | | - |
32 | | -export async function get({ props: { post } }) { |
33 | | - const { title, tags, date } = post |
34 | | - |
35 | | - const svg = await satori( |
36 | | - { |
37 | | - type: "div", |
38 | | - props: { |
39 | | - style: { |
40 | | - display: "flex", |
41 | | - flexDirection: "column", |
42 | | - justifyContent: "center", |
43 | | - alignItems: "center", |
44 | | - width: "100%", |
45 | | - height: "100%", |
46 | | - padding: "3rem", |
47 | | - background: "#111", |
48 | | - boxSizing: "border-box", |
49 | | - position: "relative", |
50 | | - gap: "1rem", |
51 | | - color: "#F0F0F0", |
52 | | - fontFamily: "Barlow", |
53 | | - }, |
54 | | - children: [ |
55 | | - { |
56 | | - type: "div", |
57 | | - props: { |
58 | | - style: { |
59 | | - backgroundImage: `url("data:image/svg+xml;base64,${bgImg}")`, |
60 | | - position: "absolute", |
61 | | - inset: 0, |
62 | | - width: "100%", |
63 | | - height: "100%", |
64 | | - maskImage: |
65 | | - "radial-gradient(60% 60%, hsl(0, 0%, 0%, .80), transparent)", |
66 | | - }, |
67 | | - }, |
68 | | - }, |
69 | | - { |
70 | | - type: "div", |
71 | | - props: { |
72 | | - style: { |
73 | | - display: "flex", |
74 | | - gap: "1rem", |
75 | | - }, |
76 | | - children: tags.map(tag => ({ |
77 | | - type: "div", |
78 | | - props: { |
79 | | - style: { |
80 | | - border: "1px solid #CCC", |
81 | | - color: "#CCC", |
82 | | - padding: "0.5rem 1rem", |
83 | | - borderRadius: "999px", |
84 | | - fontSize: "1.25rem", |
85 | | - background: "#111", |
86 | | - }, |
87 | | - children: tag, |
88 | | - }, |
89 | | - })), |
90 | | - }, |
91 | | - }, |
92 | | - { |
93 | | - type: "h1", |
94 | | - props: { |
95 | | - style: { |
96 | | - textAlign: "center", |
97 | | - fontSize: "5.5rem", |
98 | | - fontWeight: "bold", |
99 | | - margin: 0, |
100 | | - lineHeight: "1", |
101 | | - textWrap: "balance", |
102 | | - }, |
103 | | - children: title, |
104 | | - }, |
105 | | - }, |
106 | | - { |
107 | | - type: "div", |
108 | | - props: { |
109 | | - style: { |
110 | | - display: "flex", |
111 | | - alignItems: "center", |
112 | | - gap: "1rem", |
113 | | - }, |
114 | | - children: [ |
115 | | - { |
116 | | - type: "img", |
117 | | - props: { |
118 | | - src: `data:image/png;base64,${logo}`, |
119 | | - style: { |
120 | | - width: "5rem", |
121 | | - }, |
122 | | - }, |
123 | | - }, |
124 | | - { |
125 | | - type: "div", |
126 | | - props: { |
127 | | - style: { |
128 | | - display: "flex", |
129 | | - flexDirection: "column", |
130 | | - }, |
131 | | - children: [ |
132 | | - { |
133 | | - type: "div", |
134 | | - props: { |
135 | | - style: { |
136 | | - fontSize: "2rem", |
137 | | - color: "#CCC", |
138 | | - }, |
139 | | - children: "Web Dev Simplified", |
140 | | - }, |
141 | | - }, |
142 | | - { |
143 | | - type: "div", |
144 | | - props: { |
145 | | - style: { |
146 | | - fontSize: "1.5rem", |
147 | | - color: "#AAA", |
148 | | - }, |
149 | | - children: Intl.DateTimeFormat("en-US", { |
150 | | - dateStyle: "medium", |
151 | | - }).format(new Date(date)), |
152 | | - }, |
153 | | - }, |
154 | | - ], |
155 | | - }, |
156 | | - }, |
157 | | - ], |
158 | | - }, |
159 | | - }, |
160 | | - ], |
161 | | - }, |
162 | | - }, |
163 | | - satoriOptions, |
164 | | - ) |
165 | | - |
166 | | - const webp = await sharp(Buffer.from(svg)).webp({ quality: 100 }).toBuffer() |
167 | | - |
168 | | - return new Response(webp, { headers: { "Content-Type": "image/webp" } }) |
| 1 | +import { generateOpenGraphImage } from "src/utils/generateOpenGraphImage" |
| 2 | + |
| 3 | +export function get({ props: { post } }) { |
| 4 | + return generateOpenGraphImage({ |
| 5 | + title: post.title, |
| 6 | + tags: post.tags ?? [], |
| 7 | + secondaryText: post.author || "Web Dev Simplified", |
| 8 | + tertiaryText: Intl.DateTimeFormat("en-US", { |
| 9 | + dateStyle: "medium", |
| 10 | + }).format(new Date(post.updatedDate ?? post.date)), |
| 11 | + }) |
169 | 12 | } |
170 | 13 |
|
171 | 14 | export async function getStaticPaths() { |
|
0 commit comments