-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
social-image.tsx
307 lines (277 loc) · 7.88 KB
/
social-image.tsx
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import ky from 'ky'
import { type NextApiRequest, type NextApiResponse } from 'next'
import { ImageResponse } from 'next/og'
import { type PageBlock } from 'notion-types'
import {
getBlockIcon,
getBlockTitle,
getPageProperty,
isUrl,
parsePageId
} from 'notion-utils'
import * as libConfig from '@/lib/config'
import interSemiBoldFont from '@/lib/fonts/inter-semibold'
import { mapImageUrl } from '@/lib/map-image-url'
import { notion } from '@/lib/notion-api'
import { type NotionPageInfo, type PageError } from '@/lib/types'
export const runtime = 'edge'
export default async function OGImage(
req: NextApiRequest,
res: NextApiResponse
) {
const { searchParams } = new URL(req.url)
const pageId = parsePageId(
searchParams.get('id') || libConfig.rootNotionPageId
)
if (!pageId) {
return new Response('Invalid notion page id', { status: 400 })
}
const pageInfoOrError = await getNotionPageInfo({ pageId })
if (pageInfoOrError.type === 'error') {
return res.status(pageInfoOrError.error.statusCode).send({
error: pageInfoOrError.error.message
})
}
const pageInfo = pageInfoOrError.data
console.log(pageInfo)
return new ImageResponse(
(
<div
style={{
position: 'relative',
width: '100%',
height: '100%',
display: 'flex',
flexDirection: 'column',
backgroundColor: '#1F2027',
alignItems: 'center',
justifyContent: 'center',
color: 'black'
}}
>
{pageInfo.image && (
<img
src={pageInfo.image}
style={{
position: 'absolute',
width: '100%',
height: '100%',
objectFit: 'cover'
// TODO: satori doesn't support background-size: cover and seems to
// have inconsistent support for filter + transform to get rid of the
// blurred edges. For now, we'll go without a blur filter on the
// background, but Satori is still very new, so hopefully we can re-add
// the blur soon.
// backgroundImage: pageInfo.image
// ? `url(${pageInfo.image})`
// : undefined,
// backgroundSize: '100% 100%'
// TODO: pageInfo.imageObjectPosition
// filter: 'blur(8px)'
// transform: 'scale(1.05)'
}}
/>
)}
<div
style={{
position: 'relative',
width: 900,
height: 465,
display: 'flex',
flexDirection: 'column',
border: '16px solid rgba(0,0,0,0.3)',
borderRadius: 8,
zIndex: '1'
}}
>
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
flexDirection: 'column',
justifyContent: 'space-around',
backgroundColor: '#fff',
padding: 24,
alignItems: 'center',
textAlign: 'center'
}}
>
{pageInfo.detail && (
<div style={{ fontSize: 32, opacity: 0 }}>{pageInfo.detail}</div>
)}
<div
style={{
fontSize: 70,
fontWeight: 700,
fontFamily: 'Inter'
}}
>
{pageInfo.title}
</div>
{pageInfo.detail && (
<div style={{ fontSize: 32, opacity: 0.6 }}>
{pageInfo.detail}
</div>
)}
</div>
</div>
{pageInfo.authorImage && (
<div
style={{
position: 'absolute',
top: 47,
left: 104,
height: 128,
width: 128,
display: 'flex',
borderRadius: '50%',
border: '4px solid #fff',
zIndex: '5'
}}
>
<img
src={pageInfo.authorImage}
style={{
width: '100%',
height: '100%'
// transform: 'scale(1.04)'
}}
/>
</div>
)}
</div>
),
{
width: 1200,
height: 630,
fonts: [
{
name: 'Inter',
data: interSemiBoldFont,
style: 'normal',
weight: 700
}
]
}
)
}
export async function getNotionPageInfo({
pageId
}: {
pageId: string
}): Promise<
| { type: 'success'; data: NotionPageInfo }
| { type: 'error'; error: PageError }
> {
const recordMap = await notion.getPage(pageId)
const keys = Object.keys(recordMap?.block || {})
const block = recordMap?.block?.[keys[0]]?.value
if (!block) {
throw new Error('Invalid recordMap for page')
}
const blockSpaceId = block.space_id
if (
blockSpaceId &&
libConfig.rootNotionSpaceId &&
blockSpaceId !== libConfig.rootNotionSpaceId
) {
return {
type: 'error',
error: {
statusCode: 400,
message: `Notion page "${pageId}" belongs to a different workspace.`
}
}
}
const isBlogPost =
block.type === 'page' && block.parent_table === 'collection'
const title = getBlockTitle(block, recordMap) || libConfig.name
const imageCoverPosition =
(block as PageBlock).format?.page_cover_position ??
libConfig.defaultPageCoverPosition
const imageObjectPosition = imageCoverPosition
? `center ${(1 - imageCoverPosition) * 100}%`
: null
const imageBlockUrl = mapImageUrl(
getPageProperty<string>('Social Image', block, recordMap) ||
(block as PageBlock).format?.page_cover,
block
)
const imageFallbackUrl = mapImageUrl(libConfig.defaultPageCover, block)
const blockIcon = getBlockIcon(block, recordMap)
const authorImageBlockUrl = mapImageUrl(
blockIcon && isUrl(blockIcon) ? blockIcon : null,
block
)
const authorImageFallbackUrl = mapImageUrl(libConfig.defaultPageIcon, block)
const [authorImage, image] = await Promise.all([
getCompatibleImageUrl(authorImageBlockUrl, authorImageFallbackUrl),
getCompatibleImageUrl(imageBlockUrl, imageFallbackUrl)
])
const author =
getPageProperty<string>('Author', block, recordMap) || libConfig.author
// const socialDescription =
// getPageProperty<string>('Description', block, recordMap) ||
// libConfig.description
// const lastUpdatedTime = getPageProperty<number>(
// 'Last Updated',
// block,
// recordMap
// )
const publishedTime = getPageProperty<number>('Published', block, recordMap)
const datePublished = publishedTime ? new Date(publishedTime) : undefined
// const dateUpdated = lastUpdatedTime
// ? new Date(lastUpdatedTime)
// : publishedTime
// ? new Date(publishedTime)
// : undefined
const date =
isBlogPost && datePublished
? `${datePublished.toLocaleString('en-US', {
month: 'long'
})} ${datePublished.getFullYear()}`
: undefined
const detail = date || author || libConfig.domain
const pageInfo: NotionPageInfo = {
pageId,
title,
image,
imageObjectPosition,
author,
authorImage,
detail
}
return {
type: 'success',
data: pageInfo
}
}
async function isUrlReachable(url: string | null): Promise<boolean> {
if (!url) {
return false
}
try {
await ky.head(url)
return true
} catch {
return false
}
}
async function getCompatibleImageUrl(
url: string | null,
fallbackUrl: string | null
): Promise<string | null> {
const image = (await isUrlReachable(url)) ? url : fallbackUrl
if (image) {
const imageUrl = new URL(image)
if (imageUrl.host === 'images.unsplash.com') {
if (!imageUrl.searchParams.has('w')) {
imageUrl.searchParams.set('w', '1200')
imageUrl.searchParams.set('fit', 'max')
return imageUrl.toString()
}
}
}
return image
}