Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .changeset/slow-dragons-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'@astrojs/starlight': minor
---

**⚠️ BREAKING CHANGE:** This release changes how autogenerated links work in Starlight’s sidebar configuration.

If you have sidebar groups using the `autogenerate` key, you must now wrap that configuration in an `items` array:

```diff
{
label: 'My group',
- autogenerate: { directory: 'some-dir' },
+ items: [{ autogenerate: { directory: 'some-dir' } }],
}
```

This change unlocks the possibility to mix autogenerated links and other links in a single group, for example:

```js
{
label: 'Mixed group',
items: [
'example-page',
{ autogenerate: { directory: 'examples' } },
{ label: 'More examples', link: 'https://example.com' },
],
}
```
8 changes: 4 additions & 4 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default defineConfig({
hi: 'गाइड',
uk: 'Ґайди',
},
autogenerate: { directory: 'guides' },
items: [{ autogenerate: { directory: 'guides' } }],
},
{
label: 'Components',
Expand All @@ -144,7 +144,7 @@ export default defineConfig({
'zh-CN': '组件',
uk: 'Компоненти',
},
autogenerate: { directory: 'components' },
items: [{ autogenerate: { directory: 'components' } }],
},
{
label: 'Reference',
Expand All @@ -163,7 +163,7 @@ export default defineConfig({
hi: 'संदर्भ',
uk: 'Довідник',
},
autogenerate: { directory: 'reference' },
items: [{ autogenerate: { directory: 'reference' } }],
},
{
label: 'Resources',
Expand All @@ -178,7 +178,7 @@ export default defineConfig({
ko: '리소스',
uk: 'Ресурси',
},
autogenerate: { directory: 'resources' },
items: [{ autogenerate: { directory: 'resources' } }],
},
],
expressiveCode: { shiki: { langs: [markdocGrammar] } },
Expand Down
4 changes: 2 additions & 2 deletions docs/src/components/sidebar-preview.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import type {
AutoSidebarGroup,
AutoSidebarEntries,
SidebarItem,
InternalSidebarLinkItem,
} from '../../../packages/starlight/schemas/sidebar';
Expand All @@ -12,7 +12,7 @@ interface Props {
config: SidebarConfig;
}

type SidebarConfig = (Exclude<SidebarItem, AutoSidebarGroup | InternalSidebarLinkItem> & {
type SidebarConfig = (Exclude<SidebarItem, AutoSidebarEntries | InternalSidebarLinkItem> & {
badge?: Badge;
})[];

Expand Down
58 changes: 26 additions & 32 deletions docs/src/content/docs/guides/sidebar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The following sidebar will be automatically generated:
]}
/>

Learn more about autogenerated sidebars in the [autogenerated groups](#autogenerated-groups) section.
Learn more about autogenerated sidebars in the [autogenerated links](#autogenerated-links) section.

## Add links and link groups

Expand Down Expand Up @@ -186,22 +186,24 @@ The configuration above generates the following sidebar:
]}
/>

### Autogenerated groups
### Autogenerated links

Starlight can automatically generate a group in your sidebar based on a directory of your docs.
Starlight can automatically generate links in your sidebar based on a directory of your docs.
This is helpful when you do not want to manually enter each sidebar item in a group.

By default, pages are sorted in alphabetical order according to the file [`id`](/reference/route-data/#id).

Add an autogenerated group using an object with `label` and `autogenerate` properties. Your `autogenerate` configuration must specify the `directory` to use for sidebar entries. For example, with the following configuration:
Add autogenerated links using an object with the `autogenerate` property.
Your `autogenerate` configuration must specify the `directory` to use for sidebar entries.
For example, with the following configuration:

```js "label:" "autogenerate:"
starlight({
sidebar: [
{
label: 'Constellations',
// Autogenerate a group of links for the 'constellations' directory.
autogenerate: { directory: 'constellations' },
// Autogenerate links for the 'constellations' directory.
items: [{ autogenerate: { directory: 'constellations' } }],
},
],
});
Expand Down Expand Up @@ -262,7 +264,7 @@ sidebar:
---
```

An autogenerated group including a page with the frontmatter above will generate the following sidebar:
A group with autogenerated links including a page with the frontmatter above will generate the following sidebar:

<SidebarPreview
config={[
Expand All @@ -282,12 +284,12 @@ An autogenerated group including a page with the frontmatter above will generate
/>

:::note
The `sidebar` frontmatter configuration is only used for links in autogenerated groups and docs links defined with the `slug` property. It does not apply to links defined with the `link` property.
The `sidebar` frontmatter configuration is only used for autogenerated links and docs links defined with the `slug` property. It does not apply to links defined with the `link` property.
:::

## Badges

Links, groups, and autogenerated groups can also include a `badge` property to display a badge next to their label.
Links and groups can also include a `badge` property to display a badge next to their label.

```js {9,16}
starlight({
Expand All @@ -302,11 +304,11 @@ starlight({
},
],
},
// An autogenerated group with an "Outdated" badge.
// A group with an "Outdated" badge.
{
label: 'Moons',
badge: 'Outdated',
autogenerate: { directory: 'moons' },
items: [{ autogenerate: { directory: 'moons' } }],
},
],
});
Expand Down Expand Up @@ -438,20 +440,19 @@ The configuration above generates the following sidebar:

### Custom HTML attributes for autogenerated links

Customize HTML attributes of all links in [autogenerated groups](#autogenerated-groups) by defining the `attrs` property in the `autogenerate` configuration.
Customize HTML attributes of all [autogenerated links](#autogenerated-links) by defining the `attrs` property in the `autogenerate` configuration.
Individual pages can specify custom attributes using the [`sidebar.attrs` frontmatter field](/reference/frontmatter/#attrs) which will be merged with the `autogenerate.attrs` configuration.

For example, with the following configuration:

```js {9}
```js {8}
starlight({
sidebar: [
{
label: 'Constellations',
autogenerate: {
// Autogenerate a group of links for the 'constellations' directory.
// Autogenerate links for the 'constellations' directory.
directory: 'constellations',
// Italicize all link labels in this group.
// Italicize all autogenerated link labels.
attrs: { style: 'font-style: italic' },
},
},
Expand All @@ -478,25 +479,16 @@ The following sidebar will be generated with all autogenerated links italicized:

<SidebarPreview
config={[
{ label: 'Carina', link: '', attrs: { style: 'font-style: italic' } },
{ label: 'Centaurus', link: '', attrs: { style: 'font-style: italic' } },
{
label: 'Constellations',
label: 'seasonal',
items: [
{ label: 'Carina', link: '', attrs: { style: 'font-style: italic' } },
{
label: 'Centaurus',
label: 'Andromeda',
link: '',
attrs: { style: 'font-style: italic' },
},
{
label: 'seasonal',
items: [
{
label: 'Andromeda',
link: '',
attrs: { style: 'font-style: italic' },
},
],
},
],
},
]}
Expand Down Expand Up @@ -666,7 +658,7 @@ The configuration above generates the following sidebar:
]}
/>

[Autogenerated groups](#autogenerated-groups) respect the `collapsed` value of their parent group:
[Autogenerated links](#autogenerated-links) respect the `collapsed` value of their parent group:

```js {5-6}
starlight({
Expand All @@ -675,7 +667,7 @@ starlight({
label: 'Constellations',
// Collapse the group and its autogenerated subgroups by default.
collapsed: true,
autogenerate: { directory: 'constellations' },
items: [{ autogenerate: { directory: 'constellations' } }],
},
],
});
Expand Down Expand Up @@ -711,7 +703,9 @@ starlight({
// Do not collapse the "Constellations" group but collapse its
// autogenerated subgroups.
collapsed: false,
autogenerate: { directory: 'constellations', collapsed: true },
items: [
{ autogenerate: { directory: 'constellations', collapsed: true } },
],
},
],
});
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/reference/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ draft: true
```

Because draft pages are not included in build output, you cannot add draft pages directly to your site sidebar config using [slugs](/guides/sidebar/#internal-links).
Draft pages in directories used for [autogenerated sidebar groups](/guides/sidebar/#autogenerated-groups) are automatically excluded in production builds.
Draft pages in directories used for [autogenerated sidebar links](/guides/sidebar/#autogenerated-links) are automatically excluded in production builds.

### `sidebar`

Expand Down
2 changes: 1 addition & 1 deletion examples/basics/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineConfig({
},
{
label: 'Reference',
autogenerate: { directory: 'reference' },
items: [{ autogenerate: { directory: 'reference' } }],
},
],
}),
Expand Down
2 changes: 1 addition & 1 deletion examples/markdoc/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
},
{
label: 'Reference',
autogenerate: { directory: 'reference' },
items: [{ autogenerate: { directory: 'reference' } }],
},
],
}),
Expand Down
2 changes: 1 addition & 1 deletion examples/tailwind/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineConfig({
},
{
label: 'Reference',
autogenerate: { directory: 'reference' },
items: [{ autogenerate: { directory: 'reference' } }],
},
],
customCss: ['./src/styles/global.css'],
Expand Down
64 changes: 62 additions & 2 deletions packages/starlight/__tests__/basics/config-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ test('errors with bad nested sidebar config', () => {
parseStarlightConfigWithFriendlyErrors({
title: 'Test',
sidebar: [
// @ts-expect-error - Testing invalid config
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to investigate why this error was hoisted (I can guess why, but need to know the specific reason) and see if we can move it back down at the group item level without losing some other type behaviors we expect.

{
label: 'Example',
items: [
{ label: 'Nested Example 1', link: '/' },
// @ts-expect-error - Testing invalid config
{ label: 'Nested Example 2', link: true },
],
},
Expand Down Expand Up @@ -238,7 +238,14 @@ test('errors with sidebar entry that includes `link` and `autogenerate`', () =>
expect(() =>
parseStarlightConfigWithFriendlyErrors({
title: 'Test',
sidebar: [{ label: 'Parent', link: '/parent', autogenerate: { directory: 'test' } }],
sidebar: [
{
label: 'Parent',
link: '/parent',
// @ts-expect-error - Testing invalid config
autogenerate: { directory: 'test' },
},
],
})
).toThrowErrorMatchingInlineSnapshot(`
"[AstroUserError]:
Expand All @@ -258,6 +265,7 @@ test('errors with sidebar entry that includes `items` and `autogenerate`', () =>
{
label: 'Parent',
items: [{ label: 'Child', link: '/parent/child' }],
// @ts-expect-error - Testing invalid config
autogenerate: { directory: 'test' },
},
],
Expand Down Expand Up @@ -318,3 +326,55 @@ test('errors if an invalid customCss file path is provided', () => {
You should move these CSS files into the \`src/\` directory and update the path in \`customCss\` to match."
`);
});

test('errors on removed autogenerated sidebar groups with no attributes', () => {
expect(() =>
parseStarlightConfigWithFriendlyErrors({
title: 'Test',
sidebar: [
{
label: 'Example',
// @ts-expect-error - Testing invalid config
autogenerate: { directory: 'test', collapsed: true },
},
],
})
).toThrowErrorMatchingInlineSnapshot(`
"[AstroUserError]:
Invalid config passed to starlight integration
Hint:
Found an \`autogenerate\` object with a \`label\`. Support for autogenerated sidebar groups was removed in Starlight v0.38.0.
You should instead create a group with the desired \`label\` and an \`items\` array containing the autogenerate config:

{
label: 'Example',
items: [{ autogenerate: { "directory": "test", "collapsed": true } }]
}"
`);
});

test('errors on removed autogenerated sidebar groups with attributes', () => {
expect(() =>
parseStarlightConfigWithFriendlyErrors({
title: 'Test',
sidebar: [
{
label: 'Example',
// @ts-expect-error - Testing invalid config
autogenerate: { directory: 'test', attrs: { 'data-test': 'test' } },
},
],
})
).toThrowErrorMatchingInlineSnapshot(`
"[AstroUserError]:
Invalid config passed to starlight integration
Hint:
Found an \`autogenerate\` object with a \`label\`. Support for autogenerated sidebar groups was removed in Starlight v0.38.0.
You should instead create a group with the desired \`label\` and an \`items\` array containing the autogenerate config:

{
label: 'Example',
items: [{ autogenerate: { "directory": "test", "attrs": { "data-test": "test" } } }]
}"
`);
});
Loading
Loading