Skip to content

Commit 272b6b3

Browse files
KianNHelithrar
authored andcommitted
[Docs Site] Support group badges with new frontmatter property (#17032)
* [Docs Site] Support group badges with new frontmatter property * Style guide changes
1 parent 219a33b commit 272b6b3

File tree

5 files changed

+81
-7
lines changed

5 files changed

+81
-7
lines changed

src/components/overrides/Sidebar.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ async function handleGroup(group: Group): Promise<SidebarEntry> {
8686
8787
group.label = frontmatter.sidebar.group?.label ?? frontmatter.title;
8888
group.order = frontmatter.sidebar.order ?? Number.MAX_VALUE;
89+
group.badge = frontmatter.sidebar.group?.badge;
8990
9091
if (frontmatter.hideChildren) {
9192
return {

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ description: |
44
You can customize individual Markdown and MDX pages in Starlight by setting values in their frontmatter.
55
For example, a regular page might set title and description fields.
66
sidebar:
7-
badge:
8-
variant: tip
9-
text: New!
107
order: 3
118
banner:
129
content: |
@@ -25,10 +22,7 @@ description: |
2522
You can customize individual Markdown and MDX pages in Starlight by setting values in their frontmatter.
2623
For example, a regular page might set title and description fields.
2724
sidebar:
28-
label: Overview
29-
badge:
30-
variant: tip
31-
text: New!
25+
order: 3
3226
banner:
3327
content: |
3428
<h2>Hello, world!</h2>

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,40 @@ Since these pages are still accessible via other links and directly navigating t
157157
### Hiding child pages of a group
158158

159159
To make a group render as if it was a single page, which links to the index page, use the top-level `hideChildren` property.
160+
161+
## Badges
162+
163+
### Links
164+
165+
To specify a badge next to the link, use the `sidebar.badge` property.
166+
167+
```mdx title="/src/content/docs/examples/example.mdx"
168+
---
169+
title: Example
170+
sidebar:
171+
badge: New!
172+
---
173+
```
174+
175+
<FileTree>
176+
- Examples
177+
- Example [New!]
178+
</FileTree>
179+
180+
### Groups
181+
182+
To specify a badge next to the group label, use the `sidebar.group.badge` inside the group's `index.mdx` frontmatter.
183+
184+
```mdx title="/src/content/docs/examples/index.mdx"
185+
---
186+
title: Examples
187+
sidebar:
188+
group:
189+
badge: New!
190+
---
191+
```
192+
193+
<FileTree>
194+
- Examples [New!]
195+
- Example
196+
</FileTree>

src/schemas/base.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { z } from "astro:schema";
2+
import { BadgeConfigSchema } from "./types/badge";
23

34
const spotlightAuthorDetails = z
45
.object({
@@ -89,6 +90,7 @@ export const baseSchema = z.object({
8990
.describe(
9091
"Hides the index page from the sidebar. Refer to https://developers.cloudflare.com/style-guide/frontmatter/sidebar/.",
9192
),
93+
badge: BadgeConfigSchema(),
9294
})
9395
.optional(),
9496
})

src/schemas/types/badge.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Vendored from https://github.com/withastro/starlight/blob/a171a996b842f1fdb37a0bdbb2c9d86e1073e1a4/packages/starlight/schemas/badge.ts#
2+
import { z } from 'astro:schema';
3+
4+
const badgeBaseSchema = z.object({
5+
variant: z.enum(['note', 'danger', 'success', 'caution', 'tip', 'default']).default('default'),
6+
class: z.string().optional(),
7+
});
8+
9+
const badgeSchema = badgeBaseSchema.extend({
10+
text: z.string(),
11+
});
12+
13+
const i18nBadgeSchema = badgeBaseSchema.extend({
14+
text: z.union([z.string(), z.record(z.string())]),
15+
});
16+
17+
export const BadgeComponentSchema = badgeSchema
18+
.extend({
19+
size: z.enum(['small', 'medium', 'large']).default('small'),
20+
})
21+
.passthrough();
22+
23+
export type BadgeComponentProps = z.input<typeof BadgeComponentSchema>;
24+
25+
export const BadgeConfigSchema = () =>
26+
z
27+
.union([z.string(), badgeSchema])
28+
.transform((badge) => {
29+
if (typeof badge === 'string') {
30+
return { variant: 'default' as const, text: badge };
31+
}
32+
return badge;
33+
})
34+
.optional();
35+
36+
export const I18nBadgeConfigSchema = () => z.union([z.string(), i18nBadgeSchema]).optional();
37+
38+
export type Badge = z.output<typeof badgeSchema>;
39+
export type I18nBadge = z.output<typeof i18nBadgeSchema>;
40+
export type I18nBadgeConfig = z.output<ReturnType<typeof I18nBadgeConfigSchema>>;

0 commit comments

Comments
 (0)