Astro remark plugins and rehype plugins change how Markdown becomes HTML: remark plugins work on the Markdown syntax tree, rehype plugins on the HTML tree. The most useful ones for an Astro site add anchor links to headings (rehype-autolink-headings), mark external links (rehype-external-links), render math (remark-math with rehype-katex), add a table of contents (remark-toc) and compute reading time with a few lines of custom code. In Astro 7 you pass them to unified() from @astrojs/markdown-remark in the markdown.processor option.
This list covers the plugins worth knowing, what each one does, and the exact config, plus which features Sätteri, Astro 7’s default processor, already handles without plugins.
Key Takeaways
- Astro 7’s default Markdown processor is Sätteri. To use remark or rehype plugins, switch to
processor: unified({ ... }).- The old top-level
markdown.remarkPluginsandrehypePluginsstill work but log a deprecation warning.- GitHub-flavored Markdown, smart punctuation, heading IDs and Shiki highlighting are built in; don’t add plugins for them.
- Put Astro’s
rehypeHeadingIdsbeforerehype-autolink-headingsso the anchors have IDs to link to.- MDX inherits
markdown.processorby default, so one config covers.mdand.mdx.
How to add remark and rehype plugins in Astro 7
Install the processor package and your plugins, then configure markdown.processor:
# terminal
npm install @astrojs/markdown-remark rehype-external-links
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { unified } from '@astrojs/markdown-remark';
import rehypeExternalLinks from 'rehype-external-links';
export default defineConfig({
markdown: {
processor: unified({
rehypePlugins: [[rehypeExternalLinks, { target: '_blank', rel: ['noopener', 'noreferrer'] }]],
}),
},
});
A plugin is either the function itself or a [plugin, options] tuple. Remark plugins go in remarkPlugins, rehype plugins in rehypePlugins. unified() also accepts gfm, smartypants and remarkRehype options.
If you use @astrojs/mdx, it reuses this processor and its plugins by default (extendMarkdownConfig is on), so .mdx files get the same treatment.
What’s already built in
Before installing anything, check what Astro does on its own:
| Feature | Built in? | Notes |
|---|---|---|
| GitHub-flavored Markdown (tables, task lists, footnotes, strikethrough) | Yes | On by default |
| Smart quotes and dashes | Yes | On by default |
Heading id attributes |
Yes | Exposed through headings from render() |
| Syntax highlighting | Yes | Shiki, configured with markdown.shikiConfig |
| Math, directives, heading attributes, definition lists | Sätteri features | Off by default; enable with satteri({ features }) |
| Heading anchor links | No | rehype-autolink-headings |
| External link attributes | No | rehype-external-links |
| Reading time | No | Small custom remark plugin |
When Sätteri features are enough
If you only need math, ::: container directives, {#id} heading attributes, superscript, subscript, wikilinks or definition lists, you don’t need remark at all. Sätteri can turn them on:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { satteri } from '@astrojs/markdown-satteri';
export default defineConfig({
markdown: {
processor: satteri({
features: { math: true, directive: true, headingAttributes: true },
}),
},
});
Use remark/rehype when you need a specific plugin’s output or a custom transform.
Headings and navigation plugins
rehype-autolink-headings
Adds a link inside or around each heading so readers can copy a URL to a section. Astro adds heading IDs itself, but after user plugins run, so add rehypeHeadingIds first:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { unified, rehypeHeadingIds } from '@astrojs/markdown-remark';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
export default defineConfig({
markdown: {
processor: unified({
rehypePlugins: [
rehypeHeadingIds,
[rehypeAutolinkHeadings, { behavior: 'wrap' }],
],
}),
},
});
behavior: 'wrap' wraps the heading text in the link; 'append' adds a separate icon link you can style.
remark-toc
Generates a table of contents wherever a heading named “Contents” (or a custom match) appears in the Markdown. It’s handy for long Markdown-only pages. In most Astro layouts, though, building the table of contents from the headings array that render() returns gives you more control. The table of contents tip shows that approach.
rehype-slug
Adds IDs to headings. Astro already does this, so you only need it if another plugin expects rehype-slug’s exact output.
Link plugins
rehype-external-links
Adds target, rel and optionally an icon to links that point off-site. Configure rel: ['noopener', 'noreferrer'] or ['nofollow'] depending on your policy. Shown in the first config above.
Rewriting internal links
There’s no single standard plugin for this, which is why many sites write a small rehype plugin that adds trailing slashes to internal links or flags broken ones. If you’ve set trailingSlash: 'always', enforcing it in Markdown links avoids redirects; see the trailing slash tip.
Math and diagrams
remark-math and rehype-katex
For $inline$ and $$block$$ math rendered at build time:
# terminal
npm install remark-math rehype-katex katex
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { unified } from '@astrojs/markdown-remark';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
export default defineConfig({
markdown: {
processor: unified({
remarkPlugins: [remarkMath],
rehypePlugins: [rehypeKatex],
}),
},
});
Import the KaTeX stylesheet (katex/dist/katex.min.css) in the layout that renders math. Output is plain HTML and CSS, with no client JavaScript.
rehype-mermaid
Renders Mermaid code blocks to SVG at build time. It uses a headless browser (Playwright) during the build, which adds install weight and build time, so check your CI and host support it. The alternative is rendering Mermaid in the browser, which ships a large script.
Callouts and custom blocks
remark-directive
Parses :::note style container directives into nodes that a second plugin turns into HTML. You write the second part yourself, a few lines that map note, tip and warning to <aside> elements with classes. Sätteri’s directive feature covers the parsing if you don’t need remark.
remark-github-blockquote-alert
Turns GitHub’s alert syntax (> [!NOTE], > [!WARNING]) into styled blocks, so the same Markdown renders well on GitHub and on your site.
In MDX files you can skip both and use a component instead, which is usually clearer for authors.
A custom remark plugin for reading time
Many useful plugins are ten lines you write yourself. This one adds minutesRead to each post’s frontmatter:
# terminal
npm install reading-time mdast-util-to-string
// src/plugins/remark-reading-time.mjs
import getReadingTime from 'reading-time';
import { toString } from 'mdast-util-to-string';
export function remarkReadingTime() {
return function (tree, { data }) {
const text = toString(tree);
data.astro.frontmatter.minutesRead = getReadingTime(text).text;
};
}
// astro.config.mjs
import { defineConfig } from 'astro/config';
import { unified } from '@astrojs/markdown-remark';
import { remarkReadingTime } from './src/plugins/remark-reading-time.mjs';
export default defineConfig({
markdown: {
processor: unified({ remarkPlugins: [remarkReadingTime] }),
},
});
Read it from remarkPluginFrontmatter, returned by render(entry) alongside Content. The reading time tip has the layout code.
Code block plugins
Shiki is built in and handles most needs. For frames, titles, line highlighting and diffs, Expressive Code (astro-expressive-code) is an integration rather than a plain rehype plugin, and it’s the more common choice on Astro sites. rehype-pretty-code is a rehype-based alternative; if you use it, set syntaxHighlight: false so the two highlighters don’t both run. The copy code button article shows a lightweight add-on for the built-in output.
Choosing plugins without slowing your build
- Prefer built-ins and Sätteri features. They’re faster than a JavaScript plugin chain.
- Each plugin runs on every Markdown file at build time. Ten plugins on a 2,000-post site add up.
- Plugins that launch a browser (Mermaid) or fetch remote data cost the most. Cache or pre-render their output if builds get slow.
- Pin plugin versions. The unified ecosystem moves in major versions that sometimes change ESM exports.
Komal blog themes use Content Collections with the default Markdown setup, so adding plugins is a config change rather than a refactor; you can browse blog and editorial themes or read the content docs. The official Markdown guide documents processors and plugins.
FAQ
How do I add remark plugins to Astro?
Install @astrojs/markdown-remark and the plugin, then set markdown.processor to unified({ remarkPlugins: [yourPlugin] }) in astro.config.mjs. The old markdown.remarkPlugins option still works in Astro 7 but is deprecated.
What is the difference between remark and rehype plugins?
Remark plugins transform the Markdown syntax tree before it becomes HTML, so they see headings, lists and links as Markdown nodes. Rehype plugins transform the HTML tree afterward, which is where you add attributes, wrap elements or highlight code.
Do remark plugins work with MDX in Astro?
Yes. @astrojs/mdx inherits the plugins from markdown.processor by default. You can also pass a different processor or plugin set to the MDX integration if MDX files need different treatment.
Does Astro add IDs to headings automatically?
Yes. Astro adds an id to every Markdown heading and returns the list in headings from render(). When a rehype plugin needs those IDs, add rehypeHeadingIds from @astrojs/markdown-remark before it in rehypePlugins.
Can I use remark plugins with Sätteri?
No. Sätteri has its own mdast and hast plugin format and built-in feature flags. To use remark or rehype plugins from the unified ecosystem, switch markdown.processor to unified().