Skip to content

Commit

Permalink
Fix sidebar frontmatter use in internal links (#2613)
Browse files Browse the repository at this point in the history
Co-authored-by: HiDeoo <[email protected]>
  • Loading branch information
delucis and HiDeoo authored Nov 19, 2024
1 parent 10b15a7 commit a73780f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-donkeys-join.md
Original file line number Diff line number Diff line change
@@ -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
19 changes: 15 additions & 4 deletions packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }],
Expand Down Expand Up @@ -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",
},
{
Expand Down
14 changes: 10 additions & 4 deletions packages/starlight/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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. */
Expand Down

0 comments on commit a73780f

Please sign in to comment.