Skip to content

Commit e10d704

Browse files
committed
[Docs Site] Enforce and transform frontmatter tags
1 parent c6f7e0d commit e10d704

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

astro.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export default defineConfig({
160160
markdown: {
161161
headingLinks: false,
162162
},
163+
routeMiddleware: "./src/plugins/starlight/route-data.ts",
163164
}),
164165
liveCode({}),
165166
icon(),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { defineRouteMiddleware } from "@astrojs/starlight/route-data";
2+
import { tags as allowedTags } from "~/schemas/tags";
3+
4+
export const onRequest = defineRouteMiddleware(({ locals }) => {
5+
const { entry } = locals.starlightRoute;
6+
const { tags } = entry.data;
7+
8+
if (tags) {
9+
const transformed = tags.map((tag) => {
10+
const values = Object.values(allowedTags).flat();
11+
12+
const match = values.find(
13+
(val) =>
14+
val.label.toLowerCase() === tag.toLowerCase() ||
15+
val.variants?.find((v) => v.toLowerCase() === tag.toLowerCase()),
16+
);
17+
18+
if (!match) {
19+
throw new Error(
20+
`Invalid tag on ${entry.id}: ${tag}, please refer to the allowlist in /src/schemas/tags.ts`,
21+
);
22+
}
23+
24+
return match.label;
25+
});
26+
27+
entry.data.tags = transformed;
28+
}
29+
});

src/schemas/tags.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ in our page frontmatter. Refer to https://developers.cloudflare.com/style-guide/
33
and https://developers.cloudflare.com/style-guide/frontmatter/tags/ for more details.
44
*/
55

6-
const data_structures: Array<object> = [
6+
type Tag = { label: string; variants?: string[] };
7+
8+
const data_structures: Array<Tag> = [
79
{ label: "JSON" },
810
{ label: "TOML" },
911
{ label: "XML" },
1012
{ label: "YAML" },
1113
];
1214

13-
const frameworks: Array<object> = [
15+
const frameworks: Array<Tag> = [
1416
{ label: "Angular" },
1517
{ label: "Astro" },
1618
{ label: "Hono" },
@@ -24,7 +26,7 @@ const frameworks: Array<object> = [
2426
{ label: "Vue.js", variants: ["vue", "vuejs"] },
2527
];
2628

27-
const integrations: Array<object> = [
29+
const integrations: Array<Tag> = [
2830
{ label: "Azure", variants: ["Microsoft Azure", "MS Azure"] },
2931
{ label: "AWS", variants: ["Amazon Web Services"] },
3032
{ label: "GCP", variants: ["Google Cloud", "Google Cloud Platform"] },
@@ -45,7 +47,7 @@ const integrations: Array<object> = [
4547
{ label: "WordPress" },
4648
];
4749

48-
const languages: Array<object> = [
50+
const languages: Array<Tag> = [
4951
{ label: "Go" },
5052
{ label: "GraphQL" },
5153
{ label: "JavaScript", variants: ["js"] },
@@ -60,22 +62,22 @@ const languages: Array<object> = [
6062
{ label: "WebAssembly", variants: ["Web Assembly", "wasm"] },
6163
];
6264

63-
const operating_systems: Array<object> = [
65+
const operating_systems: Array<Tag> = [
6466
{ label: "Android", variants: ["ChromeOS"] },
6567
{ label: "iOS" },
6668
{ label: "Linux" },
6769
{ label: "MacOS", variants: ["OS X"] },
6870
{ label: "Windows", variants: ["ms windows"] },
6971
];
7072

71-
const presentation: Array<object> = [{ label: "Video" }];
73+
const presentation: Array<Tag> = [{ label: "Video" }];
7274

73-
const product_features: Array<object> = [
75+
const product_features: Array<Tag> = [
7476
{ label: "Web Crypto", variants: ["webcrypto"] },
7577
{ label: "RPC" },
7678
];
7779

78-
const protocols: Array<object> = [
80+
const protocols: Array<Tag> = [
7981
{ label: "FTP", variants: ["file transfer protocol", "ftps"] },
8082
{ label: "ICMP" },
8183
{ label: "IPsec" },
@@ -100,7 +102,7 @@ const protocols: Array<object> = [
100102
{ label: "Wireguard" },
101103
];
102104

103-
const use_cases: Array<object> = [
105+
const use_cases: Array<Tag> = [
104106
{ label: "AI" },
105107
{ label: "Authentication", variants: ["auth"] },
106108
{ label: "A/B testing", variants: ["ab test"] },

0 commit comments

Comments
 (0)