Skip to content

Commit

Permalink
Add support for customising sidebar labels from frontmatter (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Jul 28, 2023
1 parent 90141e9 commit 4485d90
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .changeset/cuddly-vans-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@astrojs/starlight': minor
---

Add support for customising autogenerated sidebar link labels from page frontmatter, overriding the page title:

```md
---
title: About this project
sidebar:
label: About
---
```
17 changes: 16 additions & 1 deletion docs/src/content/docs/reference/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,25 @@ next: false

### `sidebar`

**type:** `{ order?: number }`
**type:** `{ label?: string; order?: number }`

Control how this page is displayed in the [sidebar](/reference/configuration/#sidebar), when using an autogenerated link group.

#### `label`

**type:** `string`
**default:** the page [`title`](#title-required)

Set the label for this page in the sidebar when displayed in an autogenerated group of links.

```md
---
title: About this project
sidebar:
label: About
---
```

#### `order`

**type:** `number`
Expand Down
56 changes: 56 additions & 0 deletions packages/starlight/__tests__/basics/navigation-labels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, test, vi } from 'vitest';
import { getSidebar } from '../../utils/navigation';

vi.mock('astro:content', async () =>
(await import('../test-utils')).mockedAstroContent({
docs: [
['index.mdx', { title: 'Home Page' }],
[
'environmental-impact.md',
{ title: 'Eco-friendly docs', sidebar: { label: 'Environmental impact' } },
],
['guides/authoring-content.md', { title: 'Authoring Markdown' }],
['guides/components.mdx', { title: 'Using components', sidebar: { label: 'Components' } }],
],
})
);

describe('getSidebar', () => {
test('returns sidebar entries sorted by frontmatter order', () => {
expect(getSidebar('/', undefined)).toMatchInlineSnapshot(`
[
{
"href": "/",
"isCurrent": true,
"label": "Home Page",
"type": "link",
},
{
"href": "/environmental-impact/",
"isCurrent": false,
"label": "Environmental impact",
"type": "link",
},
{
"collapsed": false,
"entries": [
{
"href": "/guides/authoring-content/",
"isCurrent": false,
"label": "Authoring Markdown",
"type": "link",
},
{
"href": "/guides/components/",
"isCurrent": false,
"label": "Components",
"type": "link",
},
],
"label": "guides",
"type": "group",
},
]
`);
});
});
6 changes: 6 additions & 0 deletions packages/starlight/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ export function docsSchema() {
* If two pages have the same order value, they will be sorted alphabetically by slug.
*/
order: z.number().optional(),

/**
* The label for this page in the navigation.
* Defaults to the page `title` if not set.
*/
label: z.string().optional(),
})
.default({}),
});
Expand Down
6 changes: 5 additions & 1 deletion packages/starlight/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ function treeify(routes: Route[], baseDir: string): Dir {

/** Create a link entry for a given content collection entry. */
function linkFromRoute(route: Route, currentPathname: string): Link {
return makeLink(slugToPathname(route.slug), route.entry.data.title, currentPathname);
return makeLink(
slugToPathname(route.slug),
route.entry.data.sidebar.label || route.entry.data.title,
currentPathname
);
}

/**
Expand Down

0 comments on commit 4485d90

Please sign in to comment.