🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Write a Custom Astro Remark Plugin in 20 Lines

Write a custom Astro remark plugin: visit the Markdown tree with unist-util-visit, change nodes, add frontmatter, and register it with the unified() processor.

#Astro#Remark Plugin#Markdown#MDX

An Astro remark plugin is a function that returns a transformer, which receives the Markdown syntax tree (mdast) for each file and changes it before it becomes HTML. In Astro 7 you register it with markdown.processor: unified({ remarkPlugins: [yourPlugin] }) from @astrojs/markdown-remark, and MDX files inherit it automatically.

Key Takeaways

  • A remark plugin is () => (tree, file) => { ... }; walk the tree with unist-util-visit.
  • Astro 7’s default processor is Sätteri, so remark plugins need the unified() processor.
  • Write to file.data.astro.frontmatter to add computed frontmatter, such as a word count.
  • The deprecated markdown.remarkPlugins option still works, but move plugins into the processor.

Set up the processor

npm install @astrojs/markdown-remark unist-util-visit
// astro.config.mjs
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import { unified } from '@astrojs/markdown-remark';
import { remarkDemoteH1, remarkWordCount } from './src/markdown/remark-plugins.mjs';

export default defineConfig({
  integrations: [mdx()],
  markdown: {
    processor: unified({
      remarkPlugins: [remarkDemoteH1, remarkWordCount],
    }),
  },
});

Plugins run in array order on every .md and .mdx file at build time (and on each change in dev). The Astro Markdown pipeline explainer shows where remark sits relative to rehype, Shiki and image handling.

Example 1: change nodes

Many layouts render the post title as the page’s <h1>, so a # Heading inside the Markdown creates a second one. This plugin demotes every heading one level when the file contains an H1:

// src/markdown/remark-plugins.mjs
import { visit } from 'unist-util-visit';

export function remarkDemoteH1() {
  return (tree) => {
    let hasH1 = false;
    visit(tree, 'heading', (node) => {
      if (node.depth === 1) hasH1 = true;
    });
    if (!hasH1) return;
    visit(tree, 'heading', (node) => {
      node.depth = Math.min(node.depth + 1, 6);
    });
  };
}

visit(tree, 'heading', fn) calls fn for every node of type heading. Mutating the node in place is all a remark plugin needs to do. Other common node types are paragraph, link, image, code, text and blockquote; the mdast spec lists every type and its fields.

Example 2: add computed frontmatter

Astro exposes file.data.astro.frontmatter to remark plugins, and anything you add there shows up in the page’s frontmatter:

// src/markdown/remark-plugins.mjs (continued)
import { toString } from 'mdast-util-to-string';

export function remarkWordCount() {
  return (tree, file) => {
    const words = toString(tree).split(/\s+/).filter(Boolean).length;
    file.data.astro.frontmatter.wordCount = words;
  };
}

Install mdast-util-to-string too. In a content collection, read the value from remarkPluginFrontmatter, which render() returns:

---
// src/pages/blog/[id].astro (excerpt)
import { render } from 'astro:content';
const { post } = Astro.props;
const { Content, remarkPluginFrontmatter } = await render(post);
---
<p>{remarkPluginFrontmatter.wordCount} words</p>
<Content />

This is how the reading time tip works too. Values added this way aren’t validated by your collection schema, so treat them as optional in templates.

Plugin options and debugging

To accept options, take them as the outer function’s argument and register the plugin as a tuple:

export function remarkCallouts(options = { className: 'callout' }) {
  return (tree) => { /* use options.className */ };
}
// in unified(): remarkPlugins: [[remarkCallouts, { className: 'note' }]]

When a plugin seems to do nothing, log tree once with console.dir(tree, { depth: 4 }) and check the node types you’re visiting. Edits to the plugin file need a dev server restart, because the config isn’t reloaded by HMR.

If you’d rather keep the faster Sätteri default, port the same logic to a Sätteri mdast plugin with defineMdastPlugin from satteri; the tree shapes match. For plugins you can install instead of write, see our remark and rehype plugins guide. If you’re adapting a blog theme, add plugins in its astro.config.mjs the same way.

FAQ

What is the difference between a remark and a rehype plugin in Astro?

Remark plugins work on the Markdown tree (mdast) before it’s converted to HTML, so they suit syntax-level changes like headings or custom shortcodes. Rehype plugins work on the HTML tree (hast) afterwards and suit output changes like attributes on links or wrappers around tables.

Do remark plugins work in MDX files?

Yes. MDX inherits the plugins configured in markdown.processor, so one unified({ remarkPlugins }) setting covers both .md and .mdx. Passing remarkPlugins to mdx() directly is deprecated in Astro 7.

Why is my remark plugin not running in Astro 7?

Most often because the default Sätteri processor is active, which doesn’t run remark plugins. Set markdown.processor to unified() from @astrojs/markdown-remark and pass the plugin there, then restart the dev server.