-
Notifications
You must be signed in to change notification settings - Fork 0
/
satori.ts
57 lines (52 loc) · 1.33 KB
/
satori.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
import satori from "satori";
import { createIntlSegmenterPolyfill } from "intl-segmenter-polyfill";
export const reactToSVG = async (
Component: React.ReactElement,
props: { width: number; height?: number }
) => {
const fonts: any = await initFonts();
const svg = await satori(Component, {
width: props.width,
height: props.height,
fonts,
});
return svg;
};
export async function initFonts() {
if (typeof window === "undefined") return [];
const [medium, regular, Segmenter] =
window.__resource ||
(window.__resource = await Promise.all([
fetch("/Inter-Medium.ttf").then((res) => res.arrayBuffer()),
fetch("/Inter-Regular.ttf").then((res) => res.arrayBuffer()),
!globalThis.Intl || !globalThis.Intl.Segmenter
? createIntlSegmenterPolyfill(
fetch(
new URL(
"intl-segmenter-polyfill/dist/break_iterator.wasm",
import.meta.url
)
)
)
: null,
]));
if (Segmenter) {
globalThis.Intl = globalThis.Intl || {};
//@ts-expect-error
globalThis.Intl.Segmenter = Segmenter;
}
return [
{
name: "Inter",
data: medium,
style: "medium",
weight: 500,
},
{
name: "Inter",
data: regular,
style: "regular",
weight: 400,
},
];
}