rehype-raw

Turns raw HTML strings embedded in Markdown into real hast nodes, so other plugins can see and process them.

Category
Interop
Maintained by
rehype team
License
MIT
Module format
ESM, default export
Install
npm install rehype-raw

The problem it solves

Markdown allows raw HTML, such as <kbd>Ctrl</kbd>. When remark-rehype converts Markdown to hast, it cannot treat those snippets as elements because Markdown only knows them as strings. With allowDangerousHtml: true they are carried along as raw nodes, but plugins such as rehype-sanitize or rehype-slug cannot see inside a string. rehype-raw parses the whole tree again, turning those raw strings into proper element nodes.

Example: raw HTML, then sanitize

raw-html-in-markdown.mjs
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeRaw from 'rehype-raw'
import rehypeSanitize from 'rehype-sanitize'
import rehypeStringify from 'rehype-stringify'

const markdown = 'Press <kbd>Ctrl</kbd> + <kbd>C</kbd>. <span onclick="x()">Hi</span>'

const file = await unified()
  .use(remarkParse)
  .use(remarkRehype, {allowDangerousHtml: true})
  .use(rehypeRaw) // turn the raw HTML strings into real hast nodes
  .use(rehypeSanitize) // then make the result safe
  .use(rehypeStringify)
  .process(markdown)

console.log(String(file))
Output node raw-html-in-markdown.mjs
<p>Press <kbd>Ctrl</kbd> + <kbd>C</kbd>. <span>Hi</span></p>

The <kbd> elements survive because the sanitize schema allows them; the onclick attribute is removed.

Security: allowing raw HTML in Markdown means allowing whatever HTML the author writes. If the Markdown is untrusted, always run rehype-sanitize after rehype-raw.

When you do not need it

If your final output is an HTML string and you trust the content, you can instead pass allowDangerousHtml: true to both remark-rehype and rehype-stringify; the raw strings are then copied into the output untouched. You need rehype-raw when later plugins must operate on that HTML, or when the output is not a string (React, MDX).

Options

  • passThrough: custom node types to keep untouched while re-parsing (advanced; see hast-util-raw).

Official references

Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.

Use ↑ ↓ to move, Enter to open.