-
-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathgenerate.server.ts
More file actions
79 lines (72 loc) · 2 KB
/
Copy pathgenerate.server.ts
File metadata and controls
79 lines (72 loc) · 2 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
import { ImageResponse, type ImageResponseOptions } from 'takumi-js/response'
import { findLibrary } from '~/libraries'
import type { LibraryId } from '~/libraries'
import { loadOgAssets as loadNodeOgAssets } from './assets.server'
import { getAccentColor } from './colors'
import { buildOgTree } from './template'
import {
MAX_OG_DESCRIPTION_LENGTH,
MAX_OG_TITLE_LENGTH,
clampOgText,
} from '~/utils/og-limits'
const ISLAND_KEY = 'island'
type GenerateInput = {
libraryId: LibraryId | string
requestUrl?: string
title?: string
description?: string
}
export type OgLibraryNotFoundError = {
kind: 'library-not-found'
libraryId: string
}
export async function generateOgImageResponse(
input: GenerateInput,
init?: ResponseInit,
): Promise<ImageResponse | OgLibraryNotFoundError> {
const library = findLibrary(input.libraryId)
if (!library) {
return { kind: 'library-not-found', libraryId: input.libraryId }
}
const assets = await loadNodeOgAssets(input.requestUrl)
const tree = buildOgTree({
libraryName: library.name,
accentColor: getAccentColor(library.id),
islandSrc: ISLAND_KEY,
pitch: clampOgText(library.tagline ?? '', MAX_OG_DESCRIPTION_LENGTH),
docTitle: input.title?.trim()
? clampOgText(input.title, MAX_OG_TITLE_LENGTH)
: undefined,
description: input.description?.trim()
? clampOgText(input.description, MAX_OG_DESCRIPTION_LENGTH)
: undefined,
})
const options: ImageResponseOptions = {
width: 1200,
height: 630,
format: 'png',
fonts: [
{
name: 'Inter',
data: assets.interRegular,
weight: 400,
style: 'normal',
},
{
name: 'Inter',
data: assets.interExtraBold,
weight: 800,
style: 'normal',
},
{
name: 'Inter',
data: assets.interBlack,
weight: 900,
style: 'normal',
},
],
images: [{ src: ISLAND_KEY, data: assets.islandPng }],
...init,
}
return new ImageResponse(tree, options)
}