|
| 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