What Is Rehype? A Practical Introduction
BeginnerWhat rehype is, the problems it solves, how it fits into unified, and when it is the right tool.
Focused, task-based lessons. Each one builds a working pipeline, and every code sample was executed with Node.js to capture the output shown.
What rehype is, the problems it solves, how it fits into unified, and when it is the right tool.
rehype handles HTML, remark handles Markdown. How they differ, and how to move content between them in one pipeline.
The parser and serializer at the heart of rehype: fragments, parse errors, and the options that change the output.
How to find and evaluate plugins, install and configure them, and order them so they do not undo each other.
Use rehype-sanitize to neutralize XSS in user content: schemas, plugin order, DOM clobbering and the mistakes to avoid.
Highlight code at build time from HTML or Markdown, style it with a theme, keep bundles small and stay safe with sanitizing.
Generate stable heading ids, add accessible anchor links, handle duplicates and prefixes, and collect a table of contents.
Pretty-print HTML with rehype-format for humans and minify it with rehype-preset-minify for production, in code or from the CLI.
Write two real plugins: one that changes the tree (lazy images) and one that reports problems (missing alt text).
Pass rehype plugins to MDX and to Next.js through the rehypePlugins option, including the Turbopack-compatible string syntax.
Prefer opinion and background? Read the blog.