Skip to Content
rehype-code-group

Astro

Add code groups to Astro, with the Sätteri plugins for Astro 7's default Markdown processor or the rehype plugin for the remark processor.

How you add rehype-code-group depends on which Markdown processor your Astro site uses.

Your setupUse
Astro 7 with its default processor, SätterisatteriCodeGroup from rehype-code-group/satteri
Astro 6 or earlierThe rehype plugin, in markdown.rehypePlugins
Astro 7 with @astrojs/markdown-remark installedThe rehype plugin, in markdown.rehypePlugins

rehypePlugins and Astro 7

Astro 7 renders Markdown with Sätteri by default, and Sätteri doesn't run remark or rehype plugins. On a default Astro 7 site, use the Sätteri plugins below, or install @astrojs/markdown-remark to keep the unified pipeline and markdown.rehypePlugins.

Astro 7 with Sätteri

npm install rehype-code-group
pnpm add rehype-code-group
yarn add rehype-code-group
bun add rehype-code-group
astro.config.mjs
import { satteri } from "@astrojs/markdown-satteri";
import { defineConfig } from "astro/config";
import {
  satteriCodeGroup,
  satteriCodeGroupLabels,
} from "rehype-code-group/satteri";

export default defineConfig({
  markdown: {
    processor: satteri({
      // Optional: take tab labels from `[label]` fence metadata.
      mdastPlugins: [satteriCodeGroupLabels()],
      hastPlugins: [satteriCodeGroup()],
    }),
  },
});

satteriCodeGroup takes the same options as the rehype plugin and writes the same HTML. satteriCodeGroupLabels does the job of the remark companion. See Sätteri for supported versions and diagnostics.

Astro 6, or the remark processor

astro.config.mjs
import { defineConfig } from "astro/config";
import rehypeCodeGroup from "rehype-code-group";
import remarkCodeGroup from "rehype-code-group/remark";

export default defineConfig({
  markdown: {
    // Optional: take tab labels from `[label]` fence metadata.
    remarkPlugins: [remarkCodeGroup],
    rehypePlugins: [rehypeCodeGroup],
  },
});

Astro's Shiki highlighting produces ordinary HTML elements, so the plugin groups highlighted blocks as they are.

Assets

Astro pages are HTML, and scripts in rendered Markdown run normally, so the default inline assets work without setup.

For a stricter setup, or to avoid repeating the assets on every page, switch to assets: "none" and load the client once from a layout:

src/layouts/Docs.astro
---
import "rehype-code-group/styles.css";
---

<slot />

<script>
  import "rehype-code-group/client";
</script>

Astro processes and bundles the <script> and the stylesheet once for the whole site. The client watches for new groups, so content swapped in by client-side routing is set up automatically.

Smart quotes

Astro turns on smart punctuation by default, which curls "npm, classic" into “npm, classic” before rehype-code-group sees it. Quoted labels and attribute values accept the curled forms, so you don't need to turn smart punctuation off.

MDX

For .mdx pages, @astrojs/mdx extends your markdown configuration by default, so the same plugins apply. Remember that { and < are special in MDX, even inside labels.

Edit on GitHub