Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sidebar frontmatter use in internal links #2613

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
16 changes: 11 additions & 5 deletions packages/starlight/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { createPathFormatter } from './createPathFormatter';
import { formatPath } from './format-path';
import { BuiltInDefaultLocale, pickLang } from './i18n';
import { ensureLeadingSlash, ensureTrailingSlash, stripLeadingAndTrailingSlashes } from './path';
import { getLocaleRoutes, routes, type Route } from './routing';
import { getLocaleRoutes, routes, type Route, type StarlightDocsEntry } from './routing';
delucis marked this conversation as resolved.
Show resolved Hide resolved
import { localeToLang, slugToPathname } from './slugs';
import type { StarlightConfig } from './user-config';

Expand Down 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
Loading