Astro already gives every Markdown and MDX heading an id, so /blog/post/#install works out of the box. To show a clickable anchor link next to each heading, add rehype-autolink-headings to the unified() Markdown processor, and put Astro’s rehypeHeadingIds in front of it so the links have IDs to point at.
Key Takeaways
- Astro adds slug IDs to headings in
.mdand.mdxfiles with no setup.render()returns those IDs inheadings, which you can use for a table of contents.- For visible anchor links, use
rehype-autolink-headingswith theunified()processor.- Add
rehypeHeadingIdsbefore the autolink plugin, or the links point at nothing.
The IDs you already have
Write ## Install the package in a Markdown file and Astro outputs <h2 id="install-the-package">. Every heading gets one, using GitHub-style slugs, so links like #install-the-package work immediately. The same list comes back from render():
---
// src/pages/blog/[id].astro (excerpt)
import { render } from 'astro:content';
const { post } = Astro.props;
const { Content, headings } = await render(post);
---
<nav aria-label="On this page">
{headings.filter((h) => h.depth === 2).map((h) => <a href={`#${h.slug}`}>{h.text}</a>)}
</nav>
<Content />
That’s the basis of the table of contents tip. For the link icon next to each heading, you need a plugin.
Add rehype-autolink-headings
npm install @astrojs/markdown-remark rehype-autolink-headings
// astro.config.mjs
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import { unified, rehypeHeadingIds } from '@astrojs/markdown-remark';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
export default defineConfig({
integrations: [mdx()],
markdown: {
processor: unified({
rehypePlugins: [
rehypeHeadingIds,
[
rehypeAutolinkHeadings,
{
behavior: 'append',
properties: { className: ['heading-anchor'], ariaHidden: 'true', tabIndex: -1 },
content: { type: 'text', value: '#' },
},
],
],
}),
},
});
Why rehypeHeadingIds first: Astro normally adds IDs after your plugins run, so the autolink plugin would find headings with no id and skip them. Running Astro’s own ID plugin earlier fixes that, and the IDs stay identical to the ones in headings. MDX files inherit this processor, so one setting covers both formats.
behavior controls where the link goes:
| Value | Output |
|---|---|
prepend |
Link before the heading text |
append |
Link after the heading text |
wrap |
The whole heading text becomes the link |
before / after |
Link as a sibling outside the heading |
Astro 7’s default processor is Sätteri, which doesn’t run rehype plugins. Switching to unified() is what makes this work; the Markdown pipeline explainer covers the trade-off between the two.
Style the anchor
/* src/styles/global.css */
.prose :is(h2, h3) { position: relative; scroll-margin-top: 5rem; }
.heading-anchor {
margin-left: 0.4em;
color: var(--color-muted, #888);
text-decoration: none;
opacity: 0;
transition: opacity 0.15s;
}
.prose :is(h2, h3):hover .heading-anchor,
.heading-anchor:focus-visible { opacity: 1; }
scroll-margin-top stops a sticky header from covering the heading when someone follows the link. On touch screens there’s no hover, so consider leaving the anchor at a low opacity rather than hidden.
Accessibility choices
The config hides the # from screen readers (ariaHidden) and takes it out of the tab order, because a link announced as “number sign” after every heading is noise. If you want keyboard users to reach anchors, drop those two properties and give the link an accessible name instead, for example content set to a visually hidden span reading “Link to this section”. The wrap behavior avoids the question entirely, since the heading text is the link name. See our Astro accessibility checklist for more.
Adapting a documentation theme? Check whether it already adds anchors before you install the plugin, or you’ll get two.
FAQ
Does Astro add IDs to headings automatically?
Yes. Every heading in a .md or .mdx file gets a slug id based on its text, and render() returns them in the headings array with depth, slug and text.
Why do my rehype-autolink-headings links have no href?
The plugin ran before the headings had IDs. Add rehypeHeadingIds from @astrojs/markdown-remark before it in rehypePlugins, and make sure you’re using the unified() processor.
Can I add heading anchors to headings in .astro components?
The plugin only processes Markdown and MDX. In an .astro component, write the id and the anchor link yourself, for example <h2 id="pricing">Pricing <a href="#pricing">#</a></h2>.