Skip to content

Commit ee06497

Browse files
KianNHRebeccaTamachiro
authored andcommitted
[Docs Site] Fix JSON schema for sidebar frontmatter (#20785)
1 parent e601f97 commit ee06497

File tree

2 files changed

+83
-27
lines changed

2 files changed

+83
-27
lines changed

src/schemas/base.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { z } from "astro:schema";
2-
import { BadgeConfigSchema } from "./types/badge";
32
import type { SchemaContext } from "astro:content";
43

4+
import { sidebar } from "./types/sidebar";
5+
56
const spotlightAuthorDetails = z
67
.object({
78
author: z.string(),
@@ -98,32 +99,7 @@ export const baseSchema = ({ image }: SchemaContext) =>
9899
.describe(
99100
"If true, this property adds a `noindex` declaration to the page, which will tell internal / external search crawlers to ignore this page. Helpful for pages that are historically accurate, but no longer recommended, such as [Workers Sites](/workers/configuration/sites/).",
100101
),
101-
sidebar: z
102-
.object({
103-
order: z.number().optional(),
104-
label: z.string().optional(),
105-
group: z
106-
.object({
107-
label: z
108-
.string()
109-
.optional()
110-
.describe(
111-
"Overrides the default 'Overview' label for index pages in the sidebar. Refer to https://developers.cloudflare.com/style-guide/frontmatter/sidebar/.",
112-
),
113-
hideIndex: z
114-
.boolean()
115-
.default(false)
116-
.describe(
117-
"Hides the index page from the sidebar. Refer to [Sidebar](/style-guide/frontmatter/sidebar/).",
118-
),
119-
badge: BadgeConfigSchema(),
120-
})
121-
.optional(),
122-
})
123-
.optional()
124-
.describe(
125-
"Used to configure various sidebar options. Refer to [Sidebar](/style-guide/frontmatter/sidebar/).",
126-
),
102+
sidebar,
127103
hideChildren: z
128104
.boolean()
129105
.optional()

src/schemas/types/sidebar.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Existing configuration options in Starlight's `sidebar` object are duplicated
3+
* here due to https://github.com/StefanTerdell/zod-to-json-schema/issues/68
4+
*
5+
* Existing options can be found in
6+
* https://github.com/withastro/starlight/blob/main/packages/starlight/schema.ts
7+
*/
8+
9+
import { z } from "astro:schema";
10+
import type { AstroBuiltinAttributes } from "astro";
11+
import type { HTMLAttributes } from "astro/types";
12+
13+
/**
14+
* From https://github.com/withastro/starlight/blob/main/packages/starlight/schemas/badge.ts
15+
*/
16+
17+
const linkHTMLAttributesSchema = z.record(
18+
z.union([z.string(), z.number(), z.boolean(), z.undefined()]),
19+
) as z.Schema<
20+
Omit<HTMLAttributes<"a">, keyof AstroBuiltinAttributes | "children">
21+
>;
22+
23+
const SidebarLinkItemHTMLAttributesSchema = () =>
24+
linkHTMLAttributesSchema.default({});
25+
26+
/**
27+
* https://github.com/withastro/starlight/blob/main/packages/starlight/schemas/sidebar.ts
28+
*/
29+
30+
const badgeBaseSchema = z.object({
31+
variant: z
32+
.enum(["note", "danger", "success", "caution", "tip", "default"])
33+
.default("default"),
34+
class: z.string().optional(),
35+
});
36+
37+
const badgeSchema = badgeBaseSchema.extend({
38+
text: z.string(),
39+
});
40+
41+
const BadgeConfigSchema = () =>
42+
z
43+
.union([z.string(), badgeSchema])
44+
.transform((badge) => {
45+
if (typeof badge === "string") {
46+
return { variant: "default" as const, text: badge };
47+
}
48+
return badge;
49+
})
50+
.optional();
51+
52+
export const sidebar = z
53+
.object({
54+
order: z.number().optional(),
55+
label: z.string().optional(),
56+
hidden: z.boolean().default(false),
57+
badge: BadgeConfigSchema(),
58+
attrs: SidebarLinkItemHTMLAttributesSchema(),
59+
group: z
60+
.object({
61+
label: z
62+
.string()
63+
.optional()
64+
.describe(
65+
"Overrides the default 'Overview' label for index pages in the sidebar. Refer to https://developers.cloudflare.com/style-guide/frontmatter/sidebar/.",
66+
),
67+
hideIndex: z
68+
.boolean()
69+
.default(false)
70+
.describe(
71+
"Hides the index page from the sidebar. Refer to [Sidebar](/style-guide/frontmatter/sidebar/).",
72+
),
73+
badge: BadgeConfigSchema(),
74+
})
75+
.optional(),
76+
})
77+
.default({})
78+
.describe(
79+
"Used to configure various sidebar options. Refer to [Sidebar](/style-guide/frontmatter/sidebar/).",
80+
);

0 commit comments

Comments
 (0)