Skip to content

Commit bc92afa

Browse files
pyxelrdelucis
andauthored
Apply suggestions from code review
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
1 parent 18a01d0 commit bc92afa

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

packages/starlight/schemas/tableOfContents.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,20 @@ export const FrontmatterTableOfContentsSchema = () =>
3535
}),
3636
z.boolean(),
3737
])
38-
.optional();
38+
const TableOfContentsBaseSchema = z
39+
.union([
40+
z.object({
41+
/** The level to start including headings at in the table of contents. Default: 2. */
42+
minHeadingLevel: z.int().min(1).max(6).optional().default(2),
43+
/** The level to stop including headings at in the table of contents. Default: 3. */
44+
maxHeadingLevel: z.int().min(1).max(6).optional().default(3),
45+
}),
46+
z.boolean().transform((enabled) => (enabled ? defaults : false)),
47+
])
48+
.refine((toc) => (toc ? toc.minHeadingLevel <= toc.maxHeadingLevel : true), {
49+
error: 'minHeadingLevel must be less than or equal to maxHeadingLevel',
50+
});
51+
52+
export const UserConfigTableOfContentsSchema = () => TableOfContentsBaseSchema.default(defaults);
53+
54+
export const FrontmatterTableOfContentsSchema = () => TableOfContentsBaseSchema.optional();

packages/starlight/utils/routing/data.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,12 @@ export function generateRouteData({
6161
}
6262

6363
export function getToC({ entry, lang, headings }: PageProps) {
64-
if (entry.data.template === 'splash') return;
65-
66-
const frontmatterToC = entry.data.tableOfContents;
67-
const globalToC = config.tableOfContents;
68-
69-
// Resolve the effective ToC config from frontmatter and global settings.
70-
let tocConfig: false | { minHeadingLevel: number; maxHeadingLevel: number };
71-
if (frontmatterToC === undefined || frontmatterToC === true) {
72-
// No override or explicit enable — use global config.
73-
tocConfig = globalToC;
74-
} else if (typeof frontmatterToC === 'object' && globalToC !== false) {
75-
// Partial override — merge with global config for missing values.
76-
tocConfig = {
77-
minHeadingLevel: frontmatterToC.minHeadingLevel ?? globalToC.minHeadingLevel,
78-
maxHeadingLevel: frontmatterToC.maxHeadingLevel ?? globalToC.maxHeadingLevel,
79-
};
80-
} else {
81-
tocConfig = false;
82-
}
83-
64+
const tocConfig =
65+
entry.data.template === 'splash'
66+
? false
67+
: entry.data.tableOfContents !== undefined
68+
? entry.data.tableOfContents
69+
: config.tableOfContents;
8470
if (!tocConfig) return;
8571
const t = useTranslations(lang);
8672
return {

0 commit comments

Comments
 (0)