Skip to content

Commit

Permalink
feat: add support for adding sidebar badges to group headings (#995)
Browse files Browse the repository at this point in the history
Co-authored-by: HiDeoo <494699+HiDeoo@users.noreply.github.com>
Co-authored-by: Chris Swithinbank <357379+delucis@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 1, 2023
1 parent d2822a1 commit 5bf4457
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-pants-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': minor
---

Adds support for adding sidebar badges to group headings
1 change: 1 addition & 0 deletions docs/src/components/sidebar-preview.astro
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function makeEntries(items: SidebarConfig): SidebarEntry[] {
label: item.label,
entries: makeEntries(item.items as SidebarConfig),
collapsed: item.collapsed,
badge: item.badge,
};
});
}
Expand Down
26 changes: 25 additions & 1 deletion docs/src/content/docs/guides/sidebar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ The `sidebar` frontmatter configuration is only used for autogenerated links and

## Badges

Links can also include a `badge` property to display a badge next to the link label.
Links, groups, and autogenerated groups can also include a `badge` property to display a badge next to their label.

```js
starlight({
Expand All @@ -251,6 +251,12 @@ starlight({
},
],
},
// An autogenerated group with a "Deprecated" badge.
{
label: 'Reference',
badge: 'Deprecated',
autogenerate: { directory: 'reference' },
},
],
});
```
Expand All @@ -269,6 +275,24 @@ The configuration above generates the following sidebar:
},
],
},
{
label: 'Reference',
badge: { text: 'Deprecated', variant: 'default'},
items: [
{
label: 'Configuration Reference',
link: '/reference/configuration/',
},
{
label: 'Frontmatter Reference',
link: '/reference/frontmatter/',
},
{
label: 'Overrides Reference',
link: '/reference/overrides/',
},
],
},
]}
/>

Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ sidebar: [
type SidebarItem = {
label: string;
translations?: Record<string, string>;
badge?: string | BadgeConfig;
} & (
| {
link: string;
badge?: string | BadgeConfig;
attrs?: Record<string, string | number | boolean | undefined>;
}
| { items: SidebarItem[]; collapsed?: boolean }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('getSidebar', () => {
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('getSidebar', () => {
expect(getSidebar('/', undefined)).toMatchInlineSnapshot(`
[
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down
1 change: 1 addition & 0 deletions packages/starlight/__tests__/basics/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('getSidebar', () => {
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down
4 changes: 4 additions & 0 deletions packages/starlight/__tests__/i18n/navigation-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('getSidebar', () => {
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down Expand Up @@ -84,6 +85,7 @@ describe('getSidebar', () => {
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand All @@ -99,6 +101,7 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand All @@ -122,6 +125,7 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('getSidebar', () => {
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down Expand Up @@ -72,6 +73,10 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": {
"text": "Experimental",
"variant": "default",
},
"collapsed": false,
"entries": [
{
Expand All @@ -90,6 +95,7 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('getSidebar', () => {
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down Expand Up @@ -79,6 +80,10 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": {
"text": "Experimental",
"variant": "default",
},
"collapsed": false,
"entries": [
{
Expand Down Expand Up @@ -108,6 +113,7 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('getSidebar', () => {
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down Expand Up @@ -68,6 +69,10 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": {
"text": "Experimental",
"variant": "default",
},
"collapsed": false,
"entries": [
{
Expand All @@ -83,6 +88,7 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down
6 changes: 6 additions & 0 deletions packages/starlight/__tests__/sidebar/navigation-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('getSidebar', () => {
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down Expand Up @@ -68,6 +69,10 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": {
"text": "Experimental",
"variant": "default",
},
"collapsed": false,
"entries": [
{
Expand All @@ -91,6 +96,7 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down
6 changes: 6 additions & 0 deletions packages/starlight/__tests__/sidebar/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('getSidebar', () => {
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down Expand Up @@ -68,6 +69,10 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": {
"text": "Experimental",
"variant": "default",
},
"collapsed": false,
"entries": [
{
Expand All @@ -91,6 +96,7 @@ describe('getSidebar', () => {
"type": "group",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
Expand Down
1 change: 1 addition & 0 deletions packages/starlight/__tests__/sidebar/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default defineVitestConfig({
// A group linking to all pages in the reference directory.
{
label: 'Reference',
badge: 'Experimental',
autogenerate: { directory: 'reference' },
},
// A group linking to all pages in the api/v1 directory.
Expand Down
16 changes: 13 additions & 3 deletions packages/starlight/components/SidebarSublist.astro
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ interface Props {
open={flattenSidebar(entry.entries).some((i) => i.isCurrent) || !entry.collapsed}
>
<summary>
<span class="large">{entry.label}</span>
<div class="group-label">
<span class="large">{entry.label}</span>
{entry.badge && (
<>
{' '}
<Badge text={entry.badge.text} variant={entry.badge.variant} />
</>
)}
</div>
<Icon name="right-caret" class="caret" size="1.25rem" />
</summary>
<Astro.self sublist={entry.entries} nested />
Expand Down Expand Up @@ -78,7 +86,8 @@ interface Props {
display: flex;
align-items: center;
justify-content: space-between;
padding-inline: var(--sl-sidebar-item-padding-inline);
padding: 0.2em var(--sl-sidebar-item-padding-inline);
line-height: 1.4;
cursor: pointer;
user-select: none;
}
Expand Down Expand Up @@ -120,7 +129,8 @@ interface Props {
background-color: var(--sl-color-text-accent);
}

a > *:not(:last-child) {
a > *:not(:last-child),
.group-label > *:not(:last-child) {
margin-inline-end: 0.25em;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/starlight/schemas/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const SidebarBaseSchema = z.object({
label: z.string(),
/** Translations of the `label` for each supported language. */
translations: z.record(z.string()).default({}),
/** Adds a badge to the link item */
badge: BadgeConfigSchema(),
});

const SidebarGroupSchema = SidebarBaseSchema.extend({
Expand All @@ -29,8 +31,6 @@ export const SidebarLinkItemHTMLAttributesSchema = () => linkHTMLAttributesSchem
const SidebarLinkItemSchema = SidebarBaseSchema.extend({
/** The link to this item鈥檚 content. Can be a relative link to local files or the full URL of an external page. */
link: z.string(),
/** Adds a badge to the link item */
badge: BadgeConfigSchema(),
/** HTML attributes to add to the link item. */
attrs: SidebarLinkItemHTMLAttributesSchema(),
});
Expand Down
4 changes: 4 additions & 0 deletions packages/starlight/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface Group {
label: string;
entries: (Link | Group)[];
collapsed: boolean;
badge: Badge | undefined;
}

export type SidebarEntry = Link | Group;
Expand Down Expand Up @@ -75,6 +76,7 @@ function configItemToEntry(
label: pickLang(item.translations, localeToLang(locale)) || item.label,
entries: item.items.map((i) => configItemToEntry(i, currentPathname, locale, routes)),
collapsed: item.collapsed,
badge: item.badge,
};
}
}
Expand All @@ -101,6 +103,7 @@ function groupFromAutogenerateConfig(
label: pickLang(item.translations, localeToLang(locale)) || item.label,
entries: sidebarFromDir(tree, currentPathname, locale, subgroupCollapsed ?? item.collapsed),
collapsed: item.collapsed,
badge: item.badge,
};
}

Expand Down Expand Up @@ -231,6 +234,7 @@ function groupFromDir(
label: dirName,
entries,
collapsed,
badge: undefined,
};
}

Expand Down

0 comments on commit 5bf4457

Please sign in to comment.