Skip to content
rehype-code-group

Getting started

rehype-code-group turns neighboring code blocks—or arbitrary HTML content—into an accessible tab interface. It runs in the rehype phase, so it stays independent of your syntax highlighter and frontend framework.

Build your first group

Install the package

Source

terminal
$ npm install rehype-code-group

Live preview

npm install rehype-code-group
pnpm add rehype-code-group
yarn add rehype-code-group
bun add rehype-code-group
Add the plugin to your unified pipeline

Add the plugin after Markdown has been converted to HAST. It stays independent of your syntax highlighter.

build-docs.ts
import rehypeStringify from "rehype-stringify";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeCodeGroup from "rehype-code-group";
import { unified } from "unified";
 
const file = await unified()
  .use(remarkParse)
  .use(remarkRehype) 
  .use(rehypeCodeGroup) 
  .use(rehypeStringify)
  .process(markdown);
Author a compact code group

Give the group one label per neighboring code block.

README.md
::: code-group labels=[npm, pnpm]
 
```sh
npm install package
```
 
```sh
pnpm add package
```
 
:::

Continue learning