Skip to content

Commit b064b21

Browse files
committed
move to new file
1 parent c3e3997 commit b064b21

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

src/components/overrides/Head.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { differenceInCalendarDays } from "date-fns";
55
import "tippy.js/dist/tippy.css";
66
77
import { getEntry } from "astro:content";
8-
import { getOgImage } from "~/util/props";
8+
import { getOgImage } from "~/util/og";
99
import type { CollectionEntry } from "astro:content";
1010
1111
// grab the current top-level folder. Remove . characters for 1.1.1.1 URL

src/util/og.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { getImage } from "astro:assets";
2+
import { type CollectionEntry, getEntry } from "astro:content";
3+
4+
const DEFAULT_OG_IMAGE = "/cf-twitter-card.png";
5+
6+
const CHANGELOG_OG_IMAGE = "/changelog-preview.png";
7+
8+
const PRODUCT_AREA_OG_IMAGES: Record<string, string> = {
9+
"cloudflare essentials": "/core-services-preview.png",
10+
"cloudflare one": "/zt-preview.png",
11+
"developer platform": "/dev-products-preview.png",
12+
"network security": "/core-services-preview.png",
13+
"application performance": "/core-services-preview.png",
14+
"application security": "/core-services-preview.png",
15+
};
16+
17+
export async function getOgImage(entry: CollectionEntry<"docs" | "changelog">) {
18+
if (entry.data.cover) {
19+
if (!entry.data.cover.src) {
20+
throw new Error(
21+
`${entry.id} has a cover property in frontmatter that is not a valid image path`,
22+
);
23+
}
24+
25+
const image = await getImage({
26+
src: entry.data.cover,
27+
format: "png",
28+
});
29+
30+
return image.src;
31+
}
32+
33+
if (entry.collection === "changelog") {
34+
return CHANGELOG_OG_IMAGE;
35+
}
36+
37+
const section = entry.id.split("/").filter(Boolean).at(0);
38+
39+
if (!section) {
40+
return DEFAULT_OG_IMAGE;
41+
}
42+
43+
const product = await getEntry("products", section);
44+
45+
if (product && product.data.product.group) {
46+
const image =
47+
PRODUCT_AREA_OG_IMAGES[product.data.product.group.toLowerCase()];
48+
49+
if (image) {
50+
return image;
51+
}
52+
}
53+
54+
return DEFAULT_OG_IMAGE;
55+
}

src/util/props.ts

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import he from "he";
44
import { remark } from "remark";
55
import strip from "strip-markdown";
66
import { rehypeExternalLinksOptions } from "~/plugins/rehype/external-links";
7-
import { getImage } from "astro:assets";
8-
import { getEntry, type CollectionEntry } from "astro:content";
97

108
type TableOfContentsItems = NonNullable<Props["toc"]>["items"];
119

@@ -90,56 +88,3 @@ export async function generateDescription({
9088
?.replaceAll(rehypeExternalLinksOptions.content.value, "")
9189
.trim();
9290
}
93-
94-
const DEFAULT_OG_IMAGE = "/cf-twitter-card.png";
95-
96-
const CHANGELOG_OG_IMAGE = "/changelog-preview.png";
97-
98-
const PRODUCT_AREA_OG_IMAGES: Record<string, string> = {
99-
"cloudflare essentials": "/core-services-preview.png",
100-
"cloudflare one": "/zt-preview.png",
101-
"developer platform": "/dev-products-preview.png",
102-
"network security": "/core-services-preview.png",
103-
"application performance": "/core-services-preview.png",
104-
"application security": "/core-services-preview.png",
105-
};
106-
107-
export async function getOgImage(entry: CollectionEntry<"docs" | "changelog">) {
108-
if (entry.data.cover) {
109-
if (!entry.data.cover.src) {
110-
throw new Error(
111-
`${entry.id} has a cover property in frontmatter that is not a valid image path`,
112-
);
113-
}
114-
115-
const image = await getImage({
116-
src: entry.data.cover,
117-
format: "png",
118-
});
119-
120-
return image.src;
121-
}
122-
123-
if (entry.collection === "changelog") {
124-
return CHANGELOG_OG_IMAGE;
125-
}
126-
127-
const section = entry.id.split("/").filter(Boolean).at(0);
128-
129-
if (!section) {
130-
return DEFAULT_OG_IMAGE;
131-
}
132-
133-
const product = await getEntry("products", section);
134-
135-
if (product && product.data.product.group) {
136-
const image =
137-
PRODUCT_AREA_OG_IMAGES[product.data.product.group.toLowerCase()];
138-
139-
if (image) {
140-
return image;
141-
}
142-
}
143-
144-
return DEFAULT_OG_IMAGE;
145-
}

0 commit comments

Comments
 (0)