Skip to content

Commit b52e0f1

Browse files
committed
[Docs Site] Add cover property to frontmatter for custom images
1 parent 18ec2ba commit b52e0f1

File tree

4 files changed

+202
-197
lines changed

4 files changed

+202
-197
lines changed

src/components/overrides/Head.astro

Lines changed: 52 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,40 @@ 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";
89
910
// grab the current top-level folder. Remove . characters for 1.1.1.1 URL
1011
const currentSection = Astro.url.pathname.split("/")[1].replaceAll(".", "");
12+
const head = Astro.props.entry.data.head;
1113
1214
if (currentSection) {
1315
const product = await getEntry("products", currentSection);
1416
1517
if (product) {
1618
if (product.data.meta.title) {
17-
const titleIdx = Astro.props.entry.data.head.findIndex(
18-
(x) => x.tag === "title",
19-
);
19+
const titleIdx = head.findIndex((x) => x.tag === "title");
2020
2121
let title: string;
2222
2323
if (titleIdx !== -1) {
24-
const existingTitle = Astro.props.entry.data.head[titleIdx].content;
24+
const existingTitle = head[titleIdx].content;
2525
title = `${existingTitle} · ${product.data.meta.title}`;
2626
27-
Astro.props.entry.data.head[titleIdx] = {
27+
head[titleIdx] = {
2828
tag: "title",
2929
attrs: {},
3030
content: title,
3131
};
3232
} else {
3333
title = `${Astro.props.entry.data.title} · ${product.data.meta.title}`;
34-
Astro.props.entry.data.head.push({
34+
head.push({
3535
tag: "title",
3636
attrs: {},
3737
content: title,
3838
});
3939
}
4040
41-
Astro.props.entry.data.head.push({
41+
head.push({
4242
tag: "meta",
4343
attrs: { property: "og:title", content: title },
4444
content: "",
@@ -47,15 +47,15 @@ if (currentSection) {
4747
4848
if (product.data.product.title) {
4949
const productName = product.data.product.title;
50-
Astro.props.entry.data.head.push({
50+
head.push({
5151
tag: "meta",
5252
attrs: {
5353
name: "pcx_product",
5454
content: productName,
5555
},
5656
content: "",
5757
});
58-
Astro.props.entry.data.head.push({
58+
head.push({
5959
tag: "meta",
6060
attrs: {
6161
name: "algolia_product_filter",
@@ -66,58 +66,7 @@ if (currentSection) {
6666
}
6767
6868
if (product.data.product.group) {
69-
const group = product.data.product.group.toLowerCase();
70-
71-
let ogImage = "https://developers.cloudflare.com/cf-twitter-card.png";
72-
73-
const images: Record<string, string> = {
74-
"cloudflare essentials":
75-
"https://developers.cloudflare.com/core-services-preview.png",
76-
"cloudflare one": "https://developers.cloudflare.com/zt-preview.png",
77-
"developer platform":
78-
"https://developers.cloudflare.com/dev-products-preview.png",
79-
"network security":
80-
"https://developers.cloudflare.com/core-services-preview.png",
81-
"application performance":
82-
"https://developers.cloudflare.com/core-services-preview.png",
83-
"application security":
84-
"https://developers.cloudflare.com/core-services-preview.png",
85-
};
86-
87-
if (images[group]) {
88-
ogImage = images[group];
89-
}
90-
91-
const tags = [
92-
{
93-
tag: "meta",
94-
attrs: {
95-
name: "image",
96-
content: ogImage,
97-
},
98-
content: "",
99-
},
100-
{
101-
tag: "meta",
102-
attrs: {
103-
name: "og:image",
104-
content: ogImage,
105-
},
106-
content: "",
107-
},
108-
{
109-
tag: "meta",
110-
attrs: {
111-
name: "twitter:image",
112-
content: ogImage,
113-
},
114-
content: "",
115-
},
116-
] as const;
117-
118-
Astro.props.entry.data.head.push(...tags);
119-
120-
Astro.props.entry.data.head.push({
69+
head.push({
12170
tag: "meta",
12271
attrs: {
12372
name: "pcx_content_group",
@@ -129,7 +78,7 @@ if (currentSection) {
12978
}
13079
13180
if (currentSection === "style-guide") {
132-
Astro.props.entry.data.head.push({
81+
head.push({
13382
tag: "meta",
13483
attrs: {
13584
name: "robots",
@@ -138,46 +87,12 @@ if (currentSection) {
13887
content: "",
13988
});
14089
}
141-
142-
if (currentSection === "changelog") {
143-
let changelogImage =
144-
"https://developers.cloudflare.com/changelog-preview.png";
145-
146-
const tags = [
147-
{
148-
tag: "meta",
149-
attrs: {
150-
name: "image",
151-
content: changelogImage,
152-
},
153-
content: "",
154-
},
155-
{
156-
tag: "meta",
157-
attrs: {
158-
name: "og:image",
159-
content: changelogImage,
160-
},
161-
content: "",
162-
},
163-
{
164-
tag: "meta",
165-
attrs: {
166-
name: "twitter:image",
167-
content: changelogImage,
168-
},
169-
content: "",
170-
},
171-
] as const;
172-
173-
Astro.props.entry.data.head.push(...tags);
174-
}
17590
}
17691
17792
// Add noindex tag if present in frontmatter
17893
17994
if (Astro.props.entry.data.noindex) {
180-
Astro.props.entry.data.head.push({
95+
head.push({
18196
tag: "meta",
18297
attrs: {
18398
name: "robots",
@@ -191,15 +106,15 @@ if (Astro.props.entry.data.noindex) {
191106
// content type
192107
if (Astro.props.entry.data.pcx_content_type) {
193108
const contentType = Astro.props.entry.data.pcx_content_type;
194-
Astro.props.entry.data.head.push({
109+
head.push({
195110
tag: "meta",
196111
attrs: {
197112
name: "pcx_content_type",
198113
content: contentType,
199114
},
200115
content: "",
201116
});
202-
Astro.props.entry.data.head.push({
117+
head.push({
203118
tag: "meta",
204119
attrs: {
205120
name: "algolia_content_type",
@@ -212,7 +127,7 @@ if (Astro.props.entry.data.pcx_content_type) {
212127
// other products
213128
if (Astro.props.entry.data.products) {
214129
const additionalProducts = Astro.props.entry.data.products;
215-
Astro.props.entry.data.head.push({
130+
head.push({
216131
tag: "meta",
217132
attrs: {
218133
name: "pcx_additional_products",
@@ -225,7 +140,7 @@ if (Astro.props.entry.data.products) {
225140
// other products
226141
if (Astro.props.entry.data.tags) {
227142
const pageTags = Astro.props.entry.data.tags;
228-
Astro.props.entry.data.head.push({
143+
head.push({
229144
tag: "meta",
230145
attrs: {
231146
name: "pcx_tags",
@@ -240,7 +155,7 @@ if (Astro.props.entry.data.updated) {
240155
new Date(),
241156
Astro.props.entry.data.updated,
242157
);
243-
Astro.props.entry.data.head.push({
158+
head.push({
244159
tag: "meta",
245160
attrs: {
246161
name: "pcx_last_reviewed",
@@ -256,7 +171,7 @@ if (Astro.props.entry.data.pcx_content_type === "changelog") {
256171
const href = new URL(Astro.site ?? "");
257172
href.pathname = Astro.props.entry.slug.concat("/index.xml");
258173
259-
Astro.props.entry.data.head.push({
174+
head.push({
260175
tag: "link",
261176
attrs: {
262177
rel: "alternate",
@@ -268,15 +183,15 @@ if (Astro.props.entry.data.pcx_content_type === "changelog") {
268183
}
269184
270185
if (Astro.props.entry.data.external_link) {
271-
Astro.props.entry.data.head.push({
186+
head.push({
272187
tag: "meta",
273188
attrs: {
274189
content: "noindex",
275190
name: "robots",
276191
},
277192
content: "",
278193
});
279-
Astro.props.entry.data.head.push({
194+
head.push({
280195
tag: "meta",
281196
attrs: {
282197
content: `0; url=${Astro.props.entry.data.external_link}`,
@@ -285,6 +200,38 @@ if (Astro.props.entry.data.external_link) {
285200
content: "",
286201
});
287202
}
203+
204+
const ogImagePath = await getOgImage(Astro.props.entry);
205+
const ogImageUrl = new URL(ogImagePath, Astro.site!.origin).toString();
206+
207+
const ogImageTags = [
208+
{
209+
tag: "meta",
210+
attrs: {
211+
name: "image",
212+
content: ogImageUrl,
213+
},
214+
content: "",
215+
},
216+
{
217+
tag: "meta",
218+
attrs: {
219+
name: "og:image",
220+
content: ogImageUrl,
221+
},
222+
content: "",
223+
},
224+
{
225+
tag: "meta",
226+
attrs: {
227+
name: "twitter:image",
228+
content: ogImageUrl,
229+
},
230+
content: "",
231+
},
232+
] as const;
233+
234+
head.push(...ogImageTags);
288235
---
289236

290237
<script src="src/scripts/footnotes.ts"></script>

src/content/docs/workers/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ sidebar:
77
head:
88
- tag: title
99
content: Cloudflare Workers
10+
cover: ~/assets/images/workers/kv-write.svg
1011
---
1112

1213
import {

0 commit comments

Comments
 (0)