Skip to content

Commit b6c21e1

Browse files
committed
update tags docs and error
1 parent bdf5b31 commit b6c21e1

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

src/components/TagsUsage.astro

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
---
22
import { getCollection } from "astro:content";
3+
import { tags as allowedTags } from "~/schemas/tags";
4+
35
import AnchorHeading from "./AnchorHeading.astro";
46
import Details from "./Details.astro";
57
@@ -13,28 +15,57 @@ for (const entry of entries) {
1315
byTag[tag].push(entry.id);
1416
}
1517
}
18+
19+
const flattened = Object.values(allowedTags).flat();
1620
---
1721

1822
{
1923
Object.entries(byTag)
2024
.sort()
21-
.map(([tag, pages]) => (
22-
<>
23-
<AnchorHeading depth={3} title={tag} />
24-
<p>
25-
<code>{tag}</code> is used on <code>{pages.length}</code> pages.
26-
</p>
27-
<Details header={`Pages tagged with ${tag}`}>
28-
<ul>
29-
{pages.map((path) => (
25+
.map(([tag, pages]) => {
26+
const pretty = flattened.find(
27+
(x) =>
28+
x.label.toLowerCase() === tag.toLowerCase() ||
29+
x.variants?.find((v) => v.toLowerCase() === tag.toLowerCase()),
30+
);
31+
32+
if (!pretty) {
33+
throw new Error(
34+
`[TagsUsage] Tag ${tag} not found in /src/schemas/tags.ts`,
35+
);
36+
}
37+
38+
return (
39+
<>
40+
<AnchorHeading depth={3} title={pretty.label} />
41+
{pretty.variants && pretty.variants.length > 0 && (
42+
<p>
43+
Variants:
44+
</p>
45+
<ul>
46+
{pretty.variants?.map((variant) => (
3047
<li>
31-
<a href={`/${path}/`} target="_blank">
32-
{path}
33-
</a>
48+
<code>{variant}</code>
3449
</li>
3550
))}
3651
</ul>
37-
</Details>
38-
</>
39-
))
52+
)}
53+
<p>
54+
Used on <code>{pages.length}</code>{" "}
55+
pages.
56+
</p>
57+
<Details header={`Pages tagged with ${pretty.label}`}>
58+
<ul>
59+
{pages.map((path) => (
60+
<li>
61+
<a href={`/${path}/`} target="_blank">
62+
{path}
63+
</a>
64+
</li>
65+
))}
66+
</ul>
67+
</Details>
68+
</>
69+
);
70+
})
4071
}

src/content/docs/style-guide/frontmatter/tags.mdx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TagsUsage } from "~/components";
66

77
Tags are currently used to filter content in the [`ExternalResources`](/style-guide/components/external-resources/), [`ProductsByTag`](/style-guide/components/products-by-tag/) and the [`ResourcesBySelector`](/style-guide/components/resources-by-selector/) components.
88

9-
## Examples
9+
## Example
1010

1111
```mdx
1212
---
@@ -17,6 +17,15 @@ tags:
1717
---
1818
```
1919

20-
## Tags
20+
## Allowed tags and where they are being used
2121

22-
<TagsUsage />
22+
Tags are validated against an allowlist in [`/src/schemas/tags.ts`](https://github.com/cloudflare/cloudflare-docs/blob/production/src/schemas/tags.ts) which defines the user-facing representation (`label`) and any associated variants.
23+
24+
The matching is case-insensitive. For example, all of the following values are accepted in the `tags` frontmatter array and will be transformed into `Node.js`:
25+
26+
- `node.js`
27+
- `NoDe.JS`
28+
- `node`
29+
- `nodejs`
30+
31+
<TagsUsage />

src/plugins/starlight/route-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const onRequest = defineRouteMiddleware(({ locals }) => {
1717

1818
if (!match) {
1919
throw new Error(
20-
`Invalid tag on ${entry.id}: ${tag}, please refer to the allowlist in /src/schemas/tags.ts`,
20+
`Invalid tag on ${entry.id}: ${tag}, please refer to the style guide: https://developers.cloudflare.com/style-guide/frontmatter/tags/`,
2121
);
2222
}
2323

0 commit comments

Comments
 (0)