Skip to Content
rehype-code-group

Getting started

Install rehype-code-group, add it to a unified pipeline, and render your first group of tabs.

Requirements

  • Node.js 22.12 or newer.
  • An ESM project. The package has no CommonJS build, so load it with import.
  • A Markdown or HTML pipeline that accepts rehype plugins. This page uses unified directly; see Integrations for Astro, Next.js, MDX and Sätteri.

Install

npm install rehype-code-group
pnpm add rehype-code-group
yarn add rehype-code-group
bun add rehype-code-group

The example below also uses the standard unified packages for Markdown:

npm install unified remark-parse remark-rehype rehype-stringify
pnpm add unified remark-parse remark-rehype rehype-stringify
yarn add unified remark-parse remark-rehype rehype-stringify
bun add unified remark-parse remark-rehype rehype-stringify

Render your first group

Add the plugin

rehype-code-group is a rehype plugin, so it goes after remark-rehype, once the Markdown has become HTML.

render.js
import rehypeCodeGroup from "rehype-code-group";
import rehypeStringify from "rehype-stringify";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";

export async function render(markdown) {
  const file = await unified()
    .use(remarkParse)
    .use(remarkRehype)
    .use(rehypeCodeGroup)
    .use(rehypeStringify)
    .process(markdown);

  return String(file);
}

Write a group

Open a group with ::: code-group, list one label per tab, add one block per tab, and close it with :::.

install.md
::: code-group labels=[npm, pnpm]

```sh
npm install rehype-code-group
```

```sh
pnpm add rehype-code-group
```

:::

Open the page

With the default options, the plugin also adds a <style> and a <script> to any document that contains a group, so the tabs work without any other setup. The result looks like this:

npm install rehype-code-group
pnpm add rehype-code-group

Using React, a bundler, or a strict CSP?

Inline assets are the simplest way to start, but they are not always the best way to ship. If your pages are rendered by React, or you already have an application bundle, use assets: "none" and import the client yourself. See Asset delivery and CSP.

Where to go next

Edit on GitHub