Skip to content

Commit

Permalink
Add support for custom HTML attributes to sidebar links (withastro#774)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <[email protected]>
  • Loading branch information
HiDeoo and delucis authored Oct 6, 2023
1 parent b7b23a2 commit 903a579
Show file tree
Hide file tree
Showing 21 changed files with 390 additions and 88 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-foxes-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': minor
---

Support adding HTML attributes to sidebar links from config and frontmatter
5 changes: 2 additions & 3 deletions docs/src/components/sidebar-preview.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import type { AutoSidebarGroup, SidebarItem } from '../../../packages/starlight/utils/user-config';
import type { AutoSidebarGroup, SidebarItem } from '../../../packages/starlight/schemas/sidebar';
import SidebarSublist from '../../../packages/starlight/components/SidebarSublist.astro';
import type { SidebarEntry } from '../../../packages/starlight/utils/navigation';
Expand All @@ -20,11 +20,10 @@ function makeEntries(items: SidebarConfig): SidebarEntry[] {
href: item.link,
isCurrent: false,
badge: item.badge,
attrs: item.attrs ?? {},
};
}
item;
return {
type: 'group',
label: item.label,
Expand Down
44 changes: 44 additions & 0 deletions docs/src/content/docs/guides/sidebar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,50 @@ The configuration above generates the following sidebar:
]}
/>

## Custom HTML attributes

Links can also include an `attrs` property to add custom HTML attributes to the link element.

In the following example, `attrs` is used to add a `target="_blank"` attribute, so that the link opens in a new tab, and to apply a custom `style` attribute to italicize the link label:

```js
starlight({
sidebar: [
{
label: 'Guides',
items: [
// An external link to the Astro docs opening in a new tab.
{
label: 'Astro Docs',
link: 'https://docs.astro.build/',
attrs: { target: '_blank', style: 'font-style: italic' },
},
],
},
],
});
```

The configuration above generates the following sidebar:

<SidebarPreview
config={[
{
label: 'Guides',
items: [
{
label: 'Astro Docs',
link: 'https://docs.astro.build/',
attrs: {
target: '_blank',
style: 'font-style: italic',
},
},
],
},
]}
/>

## Internationalization

Use the `translations` property on link and group entries to translate the link or group label for each supported language.
Expand Down
1 change: 1 addition & 0 deletions docs/src/content/docs/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ type SidebarItem = {
| {
link: string;
badge?: string | BadgeConfig;
attrs?: Record<string, string | number | boolean | undefined>;
}
| { items: SidebarItem[]; collapsed?: boolean }
| {
Expand Down
30 changes: 29 additions & 1 deletion docs/src/content/docs/reference/frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,22 @@ pagefind: false

### `sidebar`

**type:** `{ label?: string; order?: number; hidden?: boolean; badge?: string | BadgeConfig }`
**type:** [`SidebarConfig`](#sidebarconfig)

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

#### `SidebarConfig`

```ts
interface SidebarConfig {
label?: string;
order?: number;
hidden?: boolean;
badge?: string | BadgeConfig;
attrs?: Record<string, string | number | boolean | undefined>;
}
```

#### `label`

**type:** `string`
Expand Down Expand Up @@ -299,3 +311,19 @@ sidebar:
variant: caution
---
```

#### `attrs`

**type:** `Record<string, string | number | boolean | undefined>`

HTML attributes to add to the page link in the sidebar when displayed in an autogenerated group of links.

```md
---
title: Page opening in a new tab
sidebar:
# Opens the page in a new tab
attrs:
target: _blank
---
```
4 changes: 4 additions & 0 deletions packages/starlight/__tests__/basics/navigation-labels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ describe('getSidebar', () => {
expect(getSidebar('/', undefined)).toMatchInlineSnapshot(`
[
{
"attrs": {},
"badge": undefined,
"href": "/",
"isCurrent": true,
"label": "Home Page",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/environmental-impact/",
"isCurrent": false,
Expand All @@ -37,13 +39,15 @@ describe('getSidebar', () => {
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": undefined,
"href": "/guides/authoring-content/",
"isCurrent": false,
"label": "Authoring Markdown",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/guides/components/",
"isCurrent": false,
Expand Down
4 changes: 4 additions & 0 deletions packages/starlight/__tests__/basics/navigation-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ describe('getSidebar', () => {
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": undefined,
"href": "/guides/components/",
"isCurrent": false,
"label": "Components",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/guides/authoring-content/",
"isCurrent": false,
Expand All @@ -38,13 +40,15 @@ describe('getSidebar', () => {
"type": "group",
},
{
"attrs": {},
"badge": undefined,
"href": "/environmental-impact/",
"isCurrent": false,
"label": "Eco-friendly docs",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/",
"isCurrent": true,
Expand Down
11 changes: 11 additions & 0 deletions packages/starlight/__tests__/basics/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ describe('getSidebar', () => {
expect(getSidebar('/', undefined)).toMatchInlineSnapshot(`
[
{
"attrs": {},
"badge": undefined,
"href": "/",
"isCurrent": true,
"label": "Home Page",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/environmental-impact/",
"isCurrent": false,
Expand All @@ -35,13 +37,15 @@ describe('getSidebar', () => {
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": undefined,
"href": "/guides/authoring-content/",
"isCurrent": false,
"label": "Authoring Markdown",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/guides/components/",
"isCurrent": false,
Expand Down Expand Up @@ -103,27 +107,31 @@ describe('flattenSidebar', () => {
expect(flattened).toMatchInlineSnapshot(`
[
{
"attrs": {},
"badge": undefined,
"href": "/",
"isCurrent": true,
"label": "Home Page",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/environmental-impact/",
"isCurrent": false,
"label": "Eco-friendly docs",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/guides/authoring-content/",
"isCurrent": false,
"label": "Authoring Markdown",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/guides/components/",
"isCurrent": false,
Expand All @@ -142,13 +150,15 @@ describe('getPrevNextLinks', () => {
expect(links).toMatchInlineSnapshot(`
{
"next": {
"attrs": {},
"badge": undefined,
"href": "/guides/authoring-content/",
"isCurrent": false,
"label": "Authoring Markdown",
"type": "link",
},
"prev": {
"attrs": {},
"badge": undefined,
"href": "/",
"isCurrent": false,
Expand Down Expand Up @@ -235,6 +245,7 @@ describe('getPrevNextLinks', () => {
href: '/x/',
label: 'X',
isCurrent: false,
attrs: {},
});
});

Expand Down
10 changes: 10 additions & 0 deletions packages/starlight/__tests__/i18n/navigation-order.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ describe('getSidebar', () => {
expect(getSidebar('/en/', 'en')).toMatchInlineSnapshot(`
[
{
"attrs": {},
"badge": undefined,
"href": "/en/",
"isCurrent": true,
"label": "Home page",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/en/404/",
"isCurrent": false,
Expand All @@ -47,6 +49,7 @@ describe('getSidebar', () => {
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": undefined,
"href": "/en/guides/authoring-content/",
"isCurrent": false,
Expand All @@ -65,13 +68,15 @@ describe('getSidebar', () => {
expect(getSidebar('/fr/', 'fr')).toMatchInlineSnapshot(`
[
{
"attrs": {},
"badge": undefined,
"href": "/fr/",
"isCurrent": true,
"label": "Accueil",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/fr/404/",
"isCurrent": false,
Expand All @@ -82,6 +87,7 @@ describe('getSidebar', () => {
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": undefined,
"href": "/fr/guides/authoring-content/",
"isCurrent": false,
Expand All @@ -96,13 +102,15 @@ describe('getSidebar', () => {
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": undefined,
"href": "/fr/référence/bénéfice/",
"isCurrent": false,
"label": "Bénéfice",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/fr/référence/bricolage/",
"isCurrent": false,
Expand All @@ -117,13 +125,15 @@ describe('getSidebar', () => {
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": undefined,
"href": "/fr/route/décoder/",
"isCurrent": false,
"label": "Décoder",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/fr/route/distribuer/",
"isCurrent": false,
Expand Down
Loading

0 comments on commit 903a579

Please sign in to comment.