/
index.ts
118 lines (110 loc) 路 3.47 KB
/
index.ts
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
import type { RequestHandler } from '@builder.io/qwik-city';
import { fetchFont, html, ImageResponse } from 'og-img';
export const onGet: RequestHandler = async ({ cacheControl, send, url }) => {
// Disable caching
cacheControl('no-cache');
// Get data from search params
const title = url.searchParams.get('title');
const description = url.searchParams.get('description');
const path = url.searchParams.get('path');
// Create icon and font directory URL
const iconUrl = import.meta.env.PUBLIC_WEBSITE_URL + '/icon-192px.png';
const fontDirUrl = import.meta.env.PUBLIC_WEBSITE_URL + '/fonts';
// Create Lexend 400 font object
const lexend400 = {
name: 'Lexend',
data: await fetchFont(fontDirUrl + '/lexend-400.ttf'),
style: 'normal',
weight: 400,
} as const;
// Create Lexend 500 font object
const lexend500 = {
name: 'Lexend',
data: await fetchFont(fontDirUrl + '/lexend-500.ttf'),
style: 'normal',
weight: 500,
} as const;
// Create Lexend Exa 500 font object
const lexendExa500 = {
name: 'Lexend Exa',
data: await fetchFont(fontDirUrl + '/lexend-exa-500.ttf'),
style: 'normal',
weight: 500,
} as const;
// If title is available, return image with text
if (title) {
send(
new ImageResponse(
html`
<div
tw="flex h-full w-full flex-col justify-between bg-gray-900 p-16"
style="font-family: 'Lexend'"
>
<div tw="flex items-center justify-between">
<div tw="flex items-center">
<img tw="w-16 h-16" src="${iconUrl}" />
<div
tw="text-4xl font-medium text-slate-300 ml-4"
style="font-family: 'Lexend Exa'"
>
Valibot
</div>
</div>
<div
tw="max-w-[50%] text-4xl text-slate-500"
style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap"
>
valibot.dev${path ? `/` + path : ''}
</div>
</div>
<div tw="flex flex-col">
<h1
tw="max-w-[80%] text-6xl font-medium leading-normal text-slate-200"
style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap"
>
${title}
</h1>
<p
tw="text-4xl text-slate-400 leading-loose"
style="${description ? '' : 'display: none'}}"
>
${description
? description.length > 110
? description.slice(0, 110).trimEnd() + '...'
: description
: ''}
</p>
</div>
</div>
`,
{
width: 1200,
height: 630,
fonts: [lexend400, lexend500, lexendExa500],
}
)
);
// Otherwise, return image just with logo
} else {
send(
new ImageResponse(
html`
<div
tw="flex h-full w-full items-center justify-center bg-gray-900"
style="font-family: 'Lexend Exa'"
>
<div tw="flex items-center">
<img tw="w-36 h-36" src="${iconUrl}" />
<div tw="text-8xl font-medium text-slate-300 ml-10">Valibot</div>
</div>
</div>
`,
{
width: 1200,
height: 630,
fonts: [lexendExa500],
}
)
);
}
};