Skip to content

docs: Add comprehensive rehype-raw plugin documentation with TypeScript #914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ React component to render markdown.
* [Compatibility](#compatibility)
* [Architecture](#architecture)
* [Appendix A: HTML in markdown](#appendix-a-html-in-markdown)
* [Using rehype-raw with TypeScript](#using-rehype-raw-with-typescript)
* [Security considerations with rehype-raw](#security-considerations-with-rehype-raw)
* [Common mistakes](#common-mistakes)
* [Appendix B: Components](#appendix-b-components)
* [Appendix C: line endings in markdown (and JSX)](#appendix-c-line-endings-in-markdown-and-jsx)
* [Security](#security)
Expand Down Expand Up @@ -693,6 +696,73 @@ CommonMark][commonmark-html].
Make sure to use blank lines around block-level HTML that again contains
markdown!

### Using rehype-raw with TypeScript

If you encounter TypeScript errors when using `rehype-raw`, you may need to
use a type assertion:

```tsx
import React from 'react'
import Markdown from 'react-markdown'
import rehypeRaw from 'rehype-raw'

function App() {
return (
<Markdown
rehypePlugins={[rehypeRaw as any]}
>
{`# Hello, <em>world</em>!`}
</Markdown>
)
}
```

### Security considerations with rehype-raw

⚠️ **Warning**: Using `rehype-raw` allows arbitrary HTML in markdown which can
be a security risk.
Always use it with [`rehype-sanitize`][github-rehype-sanitize] when processing
untrusted content:

```js
import React from 'react'
import Markdown from 'react-markdown'
import rehypeRaw from 'rehype-raw'
import rehypeSanitize from 'rehype-sanitize'

function App() {
return (
<Markdown
rehypePlugins={[rehypeRaw, rehypeSanitize]}
>
{userGeneratedContent}
</Markdown>
)
}
```

### Common mistakes

❌ **Don’t** use rehype plugins in `remarkPlugins`:

```js
// Wrong - this will not work
<Markdown remarkPlugins={[rehypeRaw]}>
```

✅ **Do** use rehype plugins in `rehypePlugins`:

```js
// Correct
<Markdown rehypePlugins={[rehypeRaw]}>
```

This is a common mistake because both remark and rehype are part of the
unified ecosystem, but they process different stages of the content:

* **remark plugins** process markdown (mdast)
* **rehype plugins** process HTML (hast)

## Appendix B: Components

You can also change the things that come from markdown:
Expand Down