Skip to content

Commit ec2cfc2

Browse files
KianNHkodster28
andauthored
[Docs Site] Add preview_image property to frontmatter for custom images (#20319)
* [Docs Site] Add cover property to frontmatter for custom images * Use Astro.url for base, add to changelog schema * Support changelog entries * changelog default image * Remove example that doesn't make sense * fix type import * move to new file * Add test image to R2 entry * Update property name * Add style guide entry --------- Co-authored-by: Kody Jackson <[email protected]>
1 parent 221b5f3 commit ec2cfc2

File tree

6 files changed

+238
-209
lines changed

6 files changed

+238
-209
lines changed

src/components/overrides/Head.astro

Lines changed: 56 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,41 @@ import { differenceInCalendarDays } from "date-fns";
55
import "tippy.js/dist/tippy.css";
66
77
import { getEntry } from "astro:content";
8+
import { getOgImage } from "~/util/og";
9+
import type { CollectionEntry } from "astro:content";
810
911
// grab the current top-level folder. Remove . characters for 1.1.1.1 URL
1012
const currentSection = Astro.url.pathname.split("/")[1].replaceAll(".", "");
13+
const head = Astro.props.entry.data.head;
1114
1215
if (currentSection) {
1316
const product = await getEntry("products", currentSection);
1417
1518
if (product) {
1619
if (product.data.meta.title) {
17-
const titleIdx = Astro.props.entry.data.head.findIndex(
18-
(x) => x.tag === "title",
19-
);
20+
const titleIdx = head.findIndex((x) => x.tag === "title");
2021
2122
let title: string;
2223
2324
if (titleIdx !== -1) {
24-
const existingTitle = Astro.props.entry.data.head[titleIdx].content;
25+
const existingTitle = head[titleIdx].content;
2526
title = `${existingTitle} · ${product.data.meta.title}`;
2627
27-
Astro.props.entry.data.head[titleIdx] = {
28+
head[titleIdx] = {
2829
tag: "title",
2930
attrs: {},
3031
content: title,
3132
};
3233
} else {
3334
title = `${Astro.props.entry.data.title} · ${product.data.meta.title}`;
34-
Astro.props.entry.data.head.push({
35+
head.push({
3536
tag: "title",
3637
attrs: {},
3738
content: title,
3839
});
3940
}
4041
41-
Astro.props.entry.data.head.push({
42+
head.push({
4243
tag: "meta",
4344
attrs: { property: "og:title", content: title },
4445
content: "",
@@ -47,15 +48,15 @@ if (currentSection) {
4748
4849
if (product.data.product.title) {
4950
const productName = product.data.product.title;
50-
Astro.props.entry.data.head.push({
51+
head.push({
5152
tag: "meta",
5253
attrs: {
5354
name: "pcx_product",
5455
content: productName,
5556
},
5657
content: "",
5758
});
58-
Astro.props.entry.data.head.push({
59+
head.push({
5960
tag: "meta",
6061
attrs: {
6162
name: "algolia_product_filter",
@@ -66,58 +67,7 @@ if (currentSection) {
6667
}
6768
6869
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({
70+
head.push({
12171
tag: "meta",
12272
attrs: {
12373
name: "pcx_content_group",
@@ -129,7 +79,7 @@ if (currentSection) {
12979
}
13080
13181
if (currentSection === "style-guide") {
132-
Astro.props.entry.data.head.push({
82+
head.push({
13383
tag: "meta",
13484
attrs: {
13585
name: "robots",
@@ -138,46 +88,12 @@ if (currentSection) {
13888
content: "",
13989
});
14090
}
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-
}
17591
}
17692
17793
// Add noindex tag if present in frontmatter
17894
17995
if (Astro.props.entry.data.noindex) {
180-
Astro.props.entry.data.head.push({
96+
head.push({
18197
tag: "meta",
18298
attrs: {
18399
name: "robots",
@@ -191,15 +107,15 @@ if (Astro.props.entry.data.noindex) {
191107
// content type
192108
if (Astro.props.entry.data.pcx_content_type) {
193109
const contentType = Astro.props.entry.data.pcx_content_type;
194-
Astro.props.entry.data.head.push({
110+
head.push({
195111
tag: "meta",
196112
attrs: {
197113
name: "pcx_content_type",
198114
content: contentType,
199115
},
200116
content: "",
201117
});
202-
Astro.props.entry.data.head.push({
118+
head.push({
203119
tag: "meta",
204120
attrs: {
205121
name: "algolia_content_type",
@@ -212,7 +128,7 @@ if (Astro.props.entry.data.pcx_content_type) {
212128
// other products
213129
if (Astro.props.entry.data.products) {
214130
const additionalProducts = Astro.props.entry.data.products;
215-
Astro.props.entry.data.head.push({
131+
head.push({
216132
tag: "meta",
217133
attrs: {
218134
name: "pcx_additional_products",
@@ -225,7 +141,7 @@ if (Astro.props.entry.data.products) {
225141
// other products
226142
if (Astro.props.entry.data.tags) {
227143
const pageTags = Astro.props.entry.data.tags;
228-
Astro.props.entry.data.head.push({
144+
head.push({
229145
tag: "meta",
230146
attrs: {
231147
name: "pcx_tags",
@@ -240,7 +156,7 @@ if (Astro.props.entry.data.updated) {
240156
new Date(),
241157
Astro.props.entry.data.updated,
242158
);
243-
Astro.props.entry.data.head.push({
159+
head.push({
244160
tag: "meta",
245161
attrs: {
246162
name: "pcx_last_reviewed",
@@ -256,7 +172,7 @@ if (Astro.props.entry.data.pcx_content_type === "changelog") {
256172
const href = new URL(Astro.site ?? "");
257173
href.pathname = Astro.props.entry.slug.concat("/index.xml");
258174
259-
Astro.props.entry.data.head.push({
175+
head.push({
260176
tag: "link",
261177
attrs: {
262178
rel: "alternate",
@@ -268,15 +184,15 @@ if (Astro.props.entry.data.pcx_content_type === "changelog") {
268184
}
269185
270186
if (Astro.props.entry.data.external_link) {
271-
Astro.props.entry.data.head.push({
187+
head.push({
272188
tag: "meta",
273189
attrs: {
274190
content: "noindex",
275191
name: "robots",
276192
},
277193
content: "",
278194
});
279-
Astro.props.entry.data.head.push({
195+
head.push({
280196
tag: "meta",
281197
attrs: {
282198
content: `0; url=${Astro.props.entry.data.external_link}`,
@@ -285,6 +201,41 @@ if (Astro.props.entry.data.external_link) {
285201
content: "",
286202
});
287203
}
204+
205+
const ogImagePath = await getOgImage(
206+
(Astro.props.originalEntry as CollectionEntry<"changelog">) ??
207+
Astro.props.entry,
208+
);
209+
const ogImageUrl = new URL(ogImagePath, Astro.url.origin).toString();
210+
211+
const ogImageTags = [
212+
{
213+
tag: "meta",
214+
attrs: {
215+
name: "image",
216+
content: ogImageUrl,
217+
},
218+
content: "",
219+
},
220+
{
221+
tag: "meta",
222+
attrs: {
223+
name: "og:image",
224+
content: ogImageUrl,
225+
},
226+
content: "",
227+
},
228+
{
229+
tag: "meta",
230+
attrs: {
231+
name: "twitter:image",
232+
content: ogImageUrl,
233+
},
234+
content: "",
235+
},
236+
] as const;
237+
238+
head.push(...ogImageTags);
288239
---
289240

290241
<script src="src/scripts/footnotes.ts"></script>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Custom properties
3+
sidebar:
4+
order: 4
5+
---
6+
7+
import { Type, MetaInfo } from "~/components";
8+
9+
We have added specific custom [frontmatter](/style-guide/frontmatter/) to meet specific needs.
10+
11+
- `difficulty` <Type text="string" /> <MetaInfo text="optional" />: Difficulty is displayed as a column in the [ListTutorials component](/style-guide/components/list-tutorials/).
12+
- `external_link` <Type text="string" /> <MetaInfo text="optional" />: Path to another page in our docs or elsewhere. Used to add a crosslink entry to the lefthand navigation sidebar.
13+
- `pcx_content_type` <Type text="string" /> <MetaInfo text="optional" />: The purpose of the page, and defined through specific pages in [Content strategy](/style-guide/documentation-content-strategy/content-types/).
14+
- `preview_image` <Type text="string" /> <MetaInfo text="optional" />: An `src` path to the image that you want to use as a custom preview image for social sharing.
15+
- `products` <Type text="Array<String>" /> <MetaInfo text="optional" />: The names of related products, which show on some grids for Examples, [Tutorials](/style-guide/documentation-content-strategy/content-types/tutorial/), and [Reference Architectures](/style-guide/documentation-content-strategy/content-types/reference-architecture/)
16+
- `tags` <Type text="Array<String>" /> <MetaInfo text="optional" />: A group of related keywords relating to the purpose of the page.
17+
- `updated` <Type text="Date" /> <MetaInfo text="optional" />: This is used to automatically add the [LastReviewed component](/style-guide/components/last-reviewed/).

src/pages/changelog/[...slug].astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const props = {
3636
headings,
3737
hideTitle: true,
3838
hideBreadcrumbs: true,
39+
originalEntry: note,
3940
} as StarlightPageProps;
4041
---
4142

0 commit comments

Comments
 (0)