-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy path_resolveImage.ts
51 lines (39 loc) · 1.27 KB
/
_resolveImage.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
const allImages = import.meta.glob<{ default: ImageMetadata }>(
'/src/content/{blog,caseStudies}/_images/**/*.{png,jpg,jpeg,webp}',
);
export async function resolveBlogImage(url: string | undefined) {
if (!url) return;
if (!(url in allImages)) {
throw new Error(`[blog] Image "${url}" not found! Is there a typo?`);
}
const { default: image } = await allImages[url]();
return image;
}
export async function resolveCoverImage(entry: {
data: { title: string; coverImage?: string | undefined };
}) {
if (!entry.data.coverImage) {
return undefined;
}
if (!(entry.data.coverImage in allImages)) {
throw new Error(
`[blog] Cover image for "${entry.data.title}" not found! Provided: "${entry.data.coverImage}", is there a typo?`,
);
}
const { default: image } = await allImages[entry.data.coverImage]();
return image;
}
export async function resolveSocialImage(entry: {
data: { title: string; socialImage?: string | undefined };
}) {
if (!entry.data.socialImage) {
return undefined;
}
if (!(entry.data.socialImage in allImages)) {
throw new Error(
`[blog] Social image for "${entry.data.title}" not found! Provided: "${entry.data.socialImage}", is there a typo?`,
);
}
const { default: image } = await allImages[entry.data.socialImage]();
return image;
}