Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export default defineConfig({
markdown: {
headingLinks: false,
},
routeMiddleware: "./src/plugins/starlight/route-data.ts",
}),
liveCode({}),
icon(),
Expand Down
29 changes: 29 additions & 0 deletions src/plugins/starlight/route-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { defineRouteMiddleware } from "@astrojs/starlight/route-data";
import { tags as allowedTags } from "~/schemas/tags";

export const onRequest = defineRouteMiddleware(({ locals }) => {
const { entry } = locals.starlightRoute;
const { tags } = entry.data;

if (tags) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding an additional check for empty tag arrays to avoid unnecessary processing:

Suggested change
if (tags) {
if (tags && tags.length > 0) {

const transformed = tags.map((tag) => {
const values = Object.values(allowedTags).flat();

const match = values.find(
(val) =>
val.label.toLowerCase() === tag.toLowerCase() ||
val.variants?.find((v) => v.toLowerCase() === tag.toLowerCase()),
);

if (!match) {
throw new Error(
`Invalid tag on ${entry.id}: ${tag}, please refer to the allowlist in /src/schemas/tags.ts`,
);
}

return match.label;
});

entry.data.tags = transformed;
}
});
20 changes: 11 additions & 9 deletions src/schemas/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ in our page frontmatter. Refer to https://developers.cloudflare.com/style-guide/
and https://developers.cloudflare.com/style-guide/frontmatter/tags/ for more details.
*/

const data_structures: Array<object> = [
type Tag = { label: string; variants?: string[] };

const data_structures: Array<Tag> = [
{ label: "JSON" },
{ label: "TOML" },
{ label: "XML" },
{ label: "YAML" },
];

const frameworks: Array<object> = [
const frameworks: Array<Tag> = [
{ label: "Angular" },
{ label: "Astro" },
{ label: "Hono" },
Expand All @@ -24,7 +26,7 @@ const frameworks: Array<object> = [
{ label: "Vue.js", variants: ["vue", "vuejs"] },
];

const integrations: Array<object> = [
const integrations: Array<Tag> = [
{ label: "Azure", variants: ["Microsoft Azure", "MS Azure"] },
{ label: "AWS", variants: ["Amazon Web Services"] },
{ label: "GCP", variants: ["Google Cloud", "Google Cloud Platform"] },
Expand All @@ -45,7 +47,7 @@ const integrations: Array<object> = [
{ label: "WordPress" },
];

const languages: Array<object> = [
const languages: Array<Tag> = [
{ label: "Go" },
{ label: "GraphQL" },
{ label: "JavaScript", variants: ["js"] },
Expand All @@ -60,22 +62,22 @@ const languages: Array<object> = [
{ label: "WebAssembly", variants: ["Web Assembly", "wasm"] },
];

const operating_systems: Array<object> = [
const operating_systems: Array<Tag> = [
{ label: "Android", variants: ["ChromeOS"] },
{ label: "iOS" },
{ label: "Linux" },
{ label: "MacOS", variants: ["OS X"] },
{ label: "Windows", variants: ["ms windows"] },
];

const presentation: Array<object> = [{ label: "Video" }];
const presentation: Array<Tag> = [{ label: "Video" }];

const product_features: Array<object> = [
const product_features: Array<Tag> = [
{ label: "Web Crypto", variants: ["webcrypto"] },
{ label: "RPC" },
];

const protocols: Array<object> = [
const protocols: Array<Tag> = [
{ label: "FTP", variants: ["file transfer protocol", "ftps"] },
{ label: "ICMP" },
{ label: "IPsec" },
Expand All @@ -100,7 +102,7 @@ const protocols: Array<object> = [
{ label: "Wireguard" },
];

const use_cases: Array<object> = [
const use_cases: Array<Tag> = [
{ label: "AI" },
{ label: "Authentication", variants: ["auth"] },
{ label: "A/B testing", variants: ["ab test"] },
Expand Down
Loading