From 35cd82e7f8622772a5155add99ad8baf61ae08a1 Mon Sep 17 00:00:00 2001 From: HiDeoo Date: Thu, 10 Aug 2023 23:15:39 +0200 Subject: Respect `hidden` sidebar frontmatter when sidebar config is unset (#489) --- .changeset/three-bobcats-lay.md | 5 ++++ .../starlight/__tests__/basics/navigation.test.ts | 1 + packages/starlight/utils/navigation.ts | 35 +++++++++++----------- 3 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 .changeset/three-bobcats-lay.md diff --git a/.changeset/three-bobcats-lay.md b/.changeset/three-bobcats-lay.md new file mode 100644 index 00000000..65facc64 --- /dev/null +++ b/.changeset/three-bobcats-lay.md @@ -0,0 +1,5 @@ +--- +'@astrojs/starlight': patch +--- + +Respect `hidden` sidebar frontmatter property when no sidebar configuration is provided diff --git a/packages/starlight/__tests__/basics/navigation.test.ts b/packages/starlight/__tests__/basics/navigation.test.ts index f4d0dbb1..29da09df 100644 --- a/packages/starlight/__tests__/basics/navigation.test.ts +++ b/packages/starlight/__tests__/basics/navigation.test.ts @@ -7,6 +7,7 @@ vi.mock('astro:content', async () => ['index.mdx', { title: 'Home Page' }], ['environmental-impact.md', { title: 'Eco-friendly docs' }], ['guides/authoring-content.md', { title: 'Authoring Markdown' }], + ['reference/frontmatter.md', { title: 'Frontmatter Reference', sidebar: { hidden: true } }], ['guides/components.mdx', { title: 'Components' }], ], }) diff --git a/packages/starlight/utils/navigation.ts b/packages/starlight/utils/navigation.ts index 816c5e79..d12c6914 100644 --- a/packages/starlight/utils/navigation.ts +++ b/packages/starlight/utils/navigation.ts @@ -83,11 +83,9 @@ function groupFromAutogenerateConfig( const dirDocs = routes.filter( (doc) => // Match against `foo.md` or `foo/index.md`. - (stripExtension(doc.id) === localeDir || - // Match against `foo/anything/else.md`. - doc.id.startsWith(localeDir + '/')) && - // Remove any entries that should be hidden - !doc.entry.data.sidebar.hidden + stripExtension(doc.id) === localeDir || + // Match against `foo/anything/else.md`. + doc.id.startsWith(localeDir + '/') ); const tree = treeify(dirDocs, localeDir); return { @@ -145,20 +143,23 @@ function getBreadcrumbs(path: string, baseDir: string): string[] { /** Turn a flat array of routes into a tree structure. */ function treeify(routes: Route[], baseDir: string): Dir { const treeRoot: Dir = makeDir(); - routes.forEach((doc) => { - const breadcrumbs = getBreadcrumbs(doc.id, baseDir); + routes + // Remove any entries that should be hidden + .filter((doc) => !doc.entry.data.sidebar.hidden) + .forEach((doc) => { + const breadcrumbs = getBreadcrumbs(doc.id, baseDir); - // Walk down the route’s path to generate the tree. - let currentDir = treeRoot; - breadcrumbs.forEach((dir) => { - // Create new folder if needed. - if (typeof currentDir[dir] === 'undefined') currentDir[dir] = makeDir(); - // Go into the subdirectory. - currentDir = currentDir[dir] as Dir; + // Walk down the route’s path to generate the tree. + let currentDir = treeRoot; + breadcrumbs.forEach((dir) => { + // Create new folder if needed. + if (typeof currentDir[dir] === 'undefined') currentDir[dir] = makeDir(); + // Go into the subdirectory. + currentDir = currentDir[dir] as Dir; + }); + // We’ve walked through the path. Register the route in this directory. + currentDir[basename(doc.slug)] = doc; }); - // We’ve walked through the path. Register the route in this directory. - currentDir[basename(doc.slug)] = doc; - }); return treeRoot; } -- cgit