diff --git a/.changeset/wild-donkeys-join.md b/.changeset/wild-donkeys-join.md new file mode 100644 index 00000000000..573e1de207c --- /dev/null +++ b/.changeset/wild-donkeys-join.md @@ -0,0 +1,5 @@ +--- +'@astrojs/starlight': patch +--- + +Fixes support for `sidebar` frontmatter options in sidebar entries using `slug` or the string shorthand for internal links diff --git a/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts b/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts index 341a20ede4e..ee62e268876 100644 --- a/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts +++ b/packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts @@ -12,7 +12,13 @@ vi.mock('astro:content', async () => ['fr/manual-setup.mdx', { title: 'Installation manuelle' }], ['environmental-impact.md', { title: 'Eco-friendly docs' }], ['fr/environmental-impact.md', { title: 'Documents écologiques' }], - ['guides/pages.mdx', { title: 'Pages' }], + [ + 'guides/pages.mdx', + { + title: 'Pages', + sidebar: { label: 'Pages Guide', badge: 'Test', attrs: { class: 'test' } }, + }, + ], ['fr/guides/pages.mdx', { title: 'Pages' }], ['guides/authoring-content.mdx', { title: 'Authoring Content in Markdown' }], ['fr/guides/authoring-content.mdx', { title: 'Création de contenu en Markdown' }], @@ -69,11 +75,16 @@ describe('getSidebar', () => { "collapsed": false, "entries": [ { - "attrs": {}, - "badge": undefined, + "attrs": { + "class": "test", + }, + "badge": { + "text": "Test", + "variant": "default", + }, "href": "/guides/pages", "isCurrent": false, - "label": "Pages", + "label": "Pages Guide", "type": "link", }, { diff --git a/packages/starlight/utils/navigation.ts b/packages/starlight/utils/navigation.ts index 1e474476859..f9dffff1a3b 100644 --- a/packages/starlight/utils/navigation.ts +++ b/packages/starlight/utils/navigation.ts @@ -142,8 +142,8 @@ function linkFromInternalSidebarLinkItem( // Astro passes root `index.[md|mdx]` entries with a slug of `index` const slug = item.slug === 'index' ? '' : item.slug; const localizedSlug = locale ? (slug ? locale + '/' + slug : locale) : slug; - const entry = routes.find((entry) => localizedSlug === entry.slug); - if (!entry) { + const route = routes.find((entry) => localizedSlug === entry.slug); + if (!route) { const hasExternalSlashes = item.slug.at(0) === '/' || item.slug.at(-1) === '/'; if (hasExternalSlashes) { throw new AstroError( @@ -158,9 +158,15 @@ function linkFromInternalSidebarLinkItem( ); } } + const frontmatter = route.entry.data; const label = - pickLang(item.translations, localeToLang(locale)) || item.label || entry.entry.data.title; - return makeSidebarLink(entry.slug, label, getSidebarBadge(item.badge, locale, label), item.attrs); + pickLang(item.translations, localeToLang(locale)) || + item.label || + frontmatter.sidebar?.label || + frontmatter.title; + const badge = item.badge ?? frontmatter.sidebar?.badge; + const attrs = { ...frontmatter.sidebar?.attrs, ...item.attrs }; + return makeSidebarLink(route.slug, label, getSidebarBadge(badge, locale, label), attrs); } /** Process sidebar link options to create a link entry. */