A rehype plugin that transforms <pre> elements containing <code> blocks into precomputed data for the CodeHighlighter component. This plugin extracts source code from HTML, processes it through syntax highlighting and transformations, then stores the results for efficient client-side rendering.
This plugin is typically used in the second stage of a markdown-to-HTML processing pipeline, after transformMarkdownCodeVariants has converted markdown code blocks into HTML structures.
class="language-*" attributesdata-variant attribute for explicit namingdata-filename attribute for specific file extensionsdata-transform="true"import { unified } from 'unified';
import rehypeParse from 'rehype-parse';
import transformHtmlCodePrecomputed from '@mui/internal-docs-infra/pipeline/transformHtmlCodePrecomputed';
const processor = unified()
.use(rehypeParse)
.use(transformHtmlCodePrecomputed) // No configuration needed
.use(rehypeStringify);
// next.config.js
const withMDX = require('@next/mdx')({
options: {
remarkPlugins: [transformMarkdownCodeVariants],
rehypePlugins: [transformHtmlCodePrecomputed],
},
});
module.exports = withMDX({
// your Next.js config
});
<pre><code class="language-typescript">
const greeting: string = "Hello, world!";
console.log(greeting);
</code></pre>
<pre>
<code class="language-javascript">console.log("Hello, world!");</code>
<code class="language-typescript">console.log("Hello, world!" as string);</code>
</pre>
<pre>
<code class="language-javascript" data-variant="Client">console.log("Running on client");</code>
<code class="language-javascript" data-variant="Server">console.log("Running on server");</code>
</pre>
<pre><code class="language-javascript" data-filename="app.jsx">
const App = () => <div>Hello React!</div>;
export default App;
</code></pre>
By default, code blocks are processed for syntax highlighting only. To enable transformations (like TypeScript-to-JavaScript conversion), explicitly mark the code block:
<pre><code class="language-typescript" data-transform="true">
const greeting: string = "Hello, world!";
console.log(greeting);
</code></pre>
This will generate both the original TypeScript and the transformed JavaScript versions.
After processing, the plugin:
data-precompute attribute as JSONdata-name and data-slug attributesdata-content-props attribute as JSON<pre
data-precompute='{"Default":{"fileName":"index.ts","source":"...","transforms":{...}}}'
data-name="My Demo"
data-slug="my-demo"
data-content-props='{"title":"Example Code"}'
>
Error: expected pre tag to be handled by CodeHighlighter
</pre>
The following data-* attributes are supported on code elements:
| Attribute | Purpose |
|---|---|
data-filename | Sets the filename for the code variant |
data-variant | Sets the variant name |
data-transform | Enables TypeScript→JavaScript transformation when "true" |
data-name | Demo name, passed through to the output <pre> element |
data-slug | URL slug for the demo, passed through to the output <pre> element |
Any other data-* attributes on the code element are serialized as JSON in the data-content-props attribute on the output <pre> element. These can be used by your content component for custom features.
Example:
<pre><code class="language-typescript" data-title="Example">
const x = 1;
const y = 2;
</code></pre>
The output <pre> element will include:
<pre data-content-props='{"title":"Example"}' ...></pre>
| Scenario | Variant Names |
|---|---|
| Single code block | "Default" |
| Different languages | "Js", "Ts", "Html", etc. |
| Same language | "Variant 1", "Variant 2", etc. |
Custom data-variant | Uses the specified name |
class="language-*" | Extension | Notes |
|---|---|---|
javascript | .js | Also accepts js (normalized) |
typescript | .ts | Also accepts ts; includes TS→JS transform when data-transform="true" |
tsx | .tsx | React TypeScript |
jsx | .jsx | React JavaScript |
json | .json | |
html | .html | |
css | .css | |
shell | .sh | Also accepts bash, sh |
yaml | .yaml | Also accepts yml |
markdown | .md | Also accepts md |
| Others | .txt | Fallback |
These plugins work together in a processing pipeline:
Markdown Input:
npm
```bash variant-group=install
npm install package
```
pnpm
```bash variant-group=install
pnpm install package
```
After transformMarkdownCodeVariants:
<pre>
<code class="language-shell" data-variant="npm">npm install package</code>
<code class="language-shell" data-variant="pnpm">pnpm install package</code>
</pre>
After transformHtmlCode:
<pre data-precompute='{"npm":{"fileName":"index.sh","source":"npm install package"...}}'>
Error: expected pre tag to be handled by CodeHighlighter
</pre>