summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Swithinbank2024-11-19 10:39:48 +0100
committerGitHub2024-11-19 10:39:48 +0100
commita73780fb280feb5b96ffa94539633b2930bd8e76 (patch)
treecca245075af2a400ac23a9fbd7b189c3d7702c58
parent10b15a7fcda4805dda622f893b6671b96a349161 (diff)
downloadIT.starlight-a73780fb280feb5b96ffa94539633b2930bd8e76.tar.gz
IT.starlight-a73780fb280feb5b96ffa94539633b2930bd8e76.tar.bz2
IT.starlight-a73780fb280feb5b96ffa94539633b2930bd8e76.zip
Fix `sidebar` frontmatter use in internal links (#2613)
Co-authored-by: HiDeoo <494699+HiDeoo@users.noreply.github.com>
-rw-r--r--.changeset/wild-donkeys-join.md5
-rw-r--r--packages/starlight/__tests__/i18n-sidebar/i18n-sidebar.test.ts19
-rw-r--r--packages/starlight/utils/navigation.ts14
3 files changed, 30 insertions, 8 deletions
diff --git a/.changeset/wild-donkeys-join.md b/.changeset/wild-donkeys-join.md
new file mode 100644
index 00000000..573e1de2
--- /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 341a20ed..ee62e268 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 1e474476..f9dffff1 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. */