Skip to content

Commit 8c19678

Browse files
delucisHiDeoo
andauthored
Add built-in heading anchor link support (#3033)
Co-authored-by: HiDeoo <[email protected]>
1 parent eda2272 commit 8c19678

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+692
-45
lines changed

.changeset/late-onions-sparkle.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@astrojs/starlight': minor
3+
---
4+
5+
Adds support for generating clickable anchor links for headings.
6+
7+
By default, Starlight now renders an anchor link beside headings in Markdown and MDX content. A new `<AnchorHeading>` component is available to achieve the same thing in custom pages built using `<StarlightPage>`.
8+
9+
If you want to disable this new Markdown processing set the `markdown.headingLinks` option in your Starlight config to `false`:
10+
11+
```js
12+
starlight({
13+
title: 'My docs',
14+
markdown: {
15+
headingLinks: false,
16+
},
17+
}),
18+
```
19+
20+
⚠️ **BREAKING CHANGE:** The minimum supported version of Astro is now v5.5.0.
21+
22+
Please update Starlight and Astro together:
23+
24+
```sh
25+
npx @astrojs/upgrade
26+
```

.changeset/quick-items-dream.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
'@astrojs/starlight-markdoc': minor
3+
---
4+
5+
Adds support for generating clickable anchor links for headings.
6+
7+
By default, the Starlight Markdoc preset now includes a default `heading` node, which renders an anchor link beside headings in your Markdoc content.
8+
9+
If you want to disable this new feature, pass `headingLinks: false` in your Markdoc config:
10+
11+
```js
12+
export default defineMarkdocConfig({
13+
// Disable the default heading anchor link support
14+
extends: [starlightMarkdoc({ headingLinks: false })],
15+
});
16+
```
17+
18+
⚠️ **BREAKING CHANGE:** The minimum supported peer version of Starlight is now v0.34.0.
19+
20+
Please update Starlight and the Starlight Markdoc preset together:
21+
22+
```sh
23+
npx @astrojs/upgrade
24+
```

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ pnpm-lock.yaml
1818
# https://github.com/withastro/prettier-plugin-astro/issues/337
1919
packages/starlight/user-components/Tabs.astro
2020

21+
# Prettier forces whitespace between elements we want to avoid for consistency with the rehype version
22+
packages/starlight/components/AnchorHeading.astro
23+
2124
# Malformed YAML file used for testing
2225
packages/starlight/__tests__/i18n/malformed-yaml-src/content/i18n/*.yml

docs/src/content/docs/guides/authoring-content.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,22 @@ If you already have a Starlight site and want to add Markdoc, follow these steps
650650
</Steps>
651651

652652
To learn more about the Markdoc syntax and features, see the [Markdoc documentation](https://markdoc.dev/docs/syntax) or the [Astro Markdoc integration guide](https://docs.astro.build/en/guides/integrations-guide/markdoc/).
653+
654+
### Configuring the Markdoc preset
655+
656+
The `starlightMarkdoc()` preset accepts the following configuration options:
657+
658+
#### `headingLinks`
659+
660+
**type:** `boolean`
661+
**default:** `true`
662+
663+
Controls whether or not headings are rendered with a clickable anchor link.
664+
Equivalent to the [`markdown.headingLinks`](/reference/configuration/#markdown) option, which applies to Markdown and MDX files.
665+
666+
```js "headingLinks: false"
667+
export default defineMarkdocConfig({
668+
// Disable the default heading anchor link support
669+
extends: [starlightMarkdoc({ headingLinks: false })],
670+
});
671+
```

docs/src/content/docs/guides/pages.mdx

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: Pages
33
description: Learn how to create and manage your documentation site’s pages with Starlight.
44
sidebar:
55
order: 1
6+
tableOfContents:
7+
maxHeadingLevel: 4
68
---
79

810
Starlight generates your site’s HTML pages based on your content, with flexible options provided via Markdown frontmatter.
@@ -73,28 +75,49 @@ Read more in the [“Pages” guide in the Astro docs](https://docs.astro.build/
7375

7476
### Using Starlight’s design in custom pages
7577

76-
To use the Starlight layout in custom pages, wrap your page content with the `<StarlightPage />` component.
78+
To use the Starlight layout in custom pages, wrap your page content with the [`<StarlightPage>` component](#starlightpage-component).
7779
This can be helpful if you are generating content dynamically but still want to use Starlight’s design.
7880

81+
To add anchor links to headings that match Starlight’s Markdown anchor link styles, you can use the [`<AnchorHeading>` component](#anchorheading-component) in your custom pages.
82+
7983
```astro
8084
---
8185
// src/pages/custom-page/example.astro
8286
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
87+
import AnchorHeading from '@astrojs/starlight/components/AnchorHeading.astro';
8388
import CustomComponent from './CustomComponent.astro';
8489
---
8590
8691
<StarlightPage frontmatter={{ title: 'My custom page' }}>
8792
<p>This is a custom page with a custom component:</p>
8893
<CustomComponent />
94+
95+
<AnchorHeading level="2" id="learn-more">Learn more</AnchorHeading>
96+
<p>
97+
<a href="https://starlight.astro.build/">Read more in the Starlight docs</a>
98+
</p>
8999
</StarlightPage>
90100
```
91101

92-
#### Props
102+
#### `<StarlightPage>` component
103+
104+
The `<StarlightPage />` component renders a full page of content using Starlight’s layout and styles.
105+
106+
```astro
107+
---
108+
import StarlightPage from '@astrojs/starlight/components/AnchorHeading.astro';
109+
---
110+
111+
<StarlightPage frontmatter={{ title: 'My custom page' }}>
112+
<!-- Custom page content -->
113+
</StarlightPage>
114+
```
93115

94116
The `<StarlightPage />` component accepts the following props.
95117

96-
##### `frontmatter` (required)
118+
##### `frontmatter`
97119

120+
**required**
98121
**type:** `StarlightPageFrontmatter`
99122

100123
Set the [frontmatter properties](/reference/frontmatter/) for this page, similar to frontmatter in Markdown pages.
@@ -173,3 +196,33 @@ Set the BCP-47 language tag for this page’s content, e.g. `en`, `zh-CN`, or `p
173196
**default:** `false`
174197

175198
Indicate if this page is using [fallback content](/guides/i18n/#fallback-content) because there is no translation for the current language.
199+
200+
#### `<AnchorHeading>` component
201+
202+
The `<AnchorHeading />` component renders an HTML heading element with a clickable anchor link that matches Starlight’s Markdown styles.
203+
204+
```astro
205+
---
206+
import AnchorHeading from '@astrojs/starlight/components/AnchorHeading.astro';
207+
---
208+
209+
<AnchorHeading level="2" id="sub-heading">Sub heading</AnchorHeading>
210+
```
211+
212+
It accepts the following props as well as any other valid [global HTML attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes).
213+
214+
##### `level`
215+
216+
**required**
217+
**type:** `1 | 2 | 3 | 4 | 5 | 6`
218+
219+
The heading level to render.
220+
For example, `level="1"` would render an `<h1>` element.
221+
222+
##### `id`
223+
224+
**required**
225+
**type:** `string`
226+
227+
The unique ID for this heading.
228+
This will be used as the `id` attribute of the rendered heading and the anchor icon will link to it.

docs/src/content/docs/reference/configuration.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,29 @@ starlight({
372372
});
373373
```
374374

375+
### `markdown`
376+
377+
**type:** `{ headingLinks?: boolean }`
378+
**default:** `{ headingLinks: true }`
379+
380+
Configure Starlight’s Markdown processing.
381+
382+
#### `headingLinks`
383+
384+
**type:** `boolean`
385+
**default:** `true`
386+
387+
Controls whether or not headings are rendered with a clickable anchor link.
388+
389+
```js
390+
starlight({
391+
markdown: {
392+
// Disable Starlight’s clickable heading anchor links.
393+
headingLinks: false,
394+
},
395+
}),
396+
```
397+
375398
### `expressiveCode`
376399

377400
**type:** `StarlightExpressiveCodeOptions | boolean`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"examples/basics/dist/_astro/*.css",
4343
"!examples/basics/dist/_astro/print.*.css"
4444
],
45-
"limit": "14.5 kB",
45+
"limit": "14.75 kB",
4646
"gzip": true
4747
}
4848
],

packages/markdoc/components.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { default as Code } from './Code.astro';
2+
export { default as Heading } from '@astrojs/starlight/components/AnchorHeading.astro';

packages/markdoc/index.mjs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { component } from '@astrojs/markdoc/config';
1+
import { component, nodes } from '@astrojs/markdoc/config';
22
import { WellKnownElementAttributes, WellKnownAnchorAttributes } from './html.mjs';
33

44
/**
@@ -287,7 +287,23 @@ export const StarlightMarkdocPreset = {
287287
},
288288
};
289289

290-
/** @return {import('@astrojs/markdoc/config').AstroMarkdocConfig} */
291-
export default function starlightMarkdoc() {
292-
return StarlightMarkdocPreset;
290+
/**
291+
* Markdoc preset that configures Starlight’s built-in components.
292+
* @return {import('@astrojs/markdoc/config').AstroMarkdocConfig}
293+
*/
294+
export default function starlightMarkdoc({ headingLinks = true } = {}) {
295+
return {
296+
...StarlightMarkdocPreset,
297+
nodes: {
298+
...StarlightMarkdocPreset.nodes,
299+
...(headingLinks
300+
? {
301+
heading: {
302+
...nodes.heading,
303+
render: component('@astrojs/starlight-markdoc/components', 'Heading'),
304+
},
305+
}
306+
: {}),
307+
},
308+
};
293309
}

packages/markdoc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"peerDependencies": {
2525
"@astrojs/markdoc": ">=0.12.1",
26-
"@astrojs/starlight": ">=0.30.0"
26+
"@astrojs/starlight": ">=0.34.0"
2727
},
2828
"publishConfig": {
2929
"provenance": true

0 commit comments

Comments
 (0)