### Install react-markdown via package managers and ESM
Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md
Instructions for installing the react-markdown package using npm for Node.js or importing via ESM URLs for Deno and browser environments.
```sh
npm install react-markdown
```
```js
import Markdown from 'https://esm.sh/react-markdown@10'
```
```html
```
--------------------------------
### Use remark-math and rehype-katex plugins with react-markdown
Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md
Demonstrates how to integrate remark-math for parsing math expressions in markdown and rehype-katex for rendering them using KaTeX. This requires installing 'react-markdown', 'remark-math', 'rehype-katex', and 'katex'. The CSS for KaTeX must be imported separately.
```javascript
import React from 'react'
import {createRoot} from 'react-dom/client'
import Markdown from 'react-markdown'
import rehypeKatex from 'rehype-katex'
import remarkMath from 'remark-math'
import 'katex/dist/katex.min.css' // `rehype-katex` does not import the CSS for you
const markdown = `The lift coefficient ($C_L$) is a dimensionless coefficient.`
createRoot(document.body).render(
{markdown}
)
```
--------------------------------
### Use remark-gfm plugin with options in react-markdown
Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md
Shows how to configure remark plugins with options, specifically for remark-gfm to control strikethrough behavior. The plugin and its options are passed as an array to the remarkPlugins prop. This example disables single tilde strikethrough.
```javascript
import React from 'react'
import {createRoot} from 'react-dom/client'
import Markdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
const markdown = 'This ~is not~ strikethrough, but ~~this is~~!'
createRoot(document.body).render(
{markdown}
)
```
--------------------------------
### Use remark-gfm plugin with react-markdown
Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md
Demonstrates how to use the remark-gfm plugin to enable GitHub Flavored Markdown features such as strikethrough, tables, tasklists, and direct URL support within react-markdown. This requires installing 'react-markdown' and 'remark-gfm'.
```javascript
import React from 'react'
import {createRoot} from 'react-dom/client'
import Markdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
const markdown = `A paragraph with *emphasis* and **strong importance**.
> A block quote with ~strikethrough~ and a URL: https://reactjs.org.
* Lists
* [ ] todo
* [x] done
A table:
| a | b |
| - | - |
`
createRoot(document.body).render(
{markdown}
)
```
--------------------------------
### Render basic markdown with react-markdown
Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md
A basic implementation showing how to import the Markdown component and render a markdown string within a React root.
```js
import React from 'react'
import {createRoot} from 'react-dom/client'
import Markdown from 'react-markdown'
const markdown = '# Hi, *Pluto*!'
createRoot(document.body).render({markdown})
```
--------------------------------
### Extend react-markdown with plugins
Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md
Demonstrates how to use remark plugins, such as remark-gfm, to add support for extended markdown features like tables, tasklists, and auto-linked URLs.
```js
import React from 'react'
import {createRoot} from 'react-dom/client'
import Markdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
const markdown = `Just a link: www.nasa.gov.`
createRoot(document.body).render(
{markdown}
)
```
--------------------------------
### Configure remarkPlugins for Markdown AST Transformation
Source: https://context7.com/remarkjs/react-markdown/llms.txt
Use the remarkPlugins option to apply remark plugins for transforming the markdown Abstract Syntax Tree (AST) before it's converted to HTML. Common plugins like remark-gfm for GitHub Flavored Markdown and remark-toc for table of contents generation can be included. Plugins can also accept options for fine-grained control.
```jsx
import Markdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
import remarkToc from 'remark-toc'
// GitHub Flavored Markdown (tables, strikethrough, task lists, autolinks)
const gfmMarkdown = `
| Feature | Supported |
| ------- | --------- |
| Tables | ✓ |
~~strikethrough~~ and https://example.com
- [x] Task done
- [ ] Task pending
`
{gfmMarkdown}
// Output includes:
// Custom component function
{children}
},
a({node, href, children, ...props}) {
return {children}
}
}}
>
{'*emphasized* and [link](https://example.com)'}
// Syntax highlighting with custom code component
{String(children).replace(/\n$/, '')}
) : (
{children}
)
}
}}
>
{'```javascript\nconst greeting = "Hello";\n```'}
// Access node information (position, properties)
{children}
}
}}
>
{'Paragraph text'}
```
--------------------------------
### Filter elements with allowElement
Source: https://context7.com/remarkjs/react-markdown/llms.txt
The allowElement prop provides a function to programmatically include or exclude specific markdown elements based on their properties or tags. It is useful for security filtering, such as blocking external links or specific media types.
```jsx
import Markdown from 'react-markdown'
// Filter based on element properties
{
// Block images
if (element.tagName === 'img') return false
// Block external links
if (element.tagName === 'a' && element.properties.href?.startsWith('http')) {
return false
}
return true
}}
>
{'\n\n[External](https://example.com)\n\n[Internal](/page)'}
// Combined with allowedElements
{
// Additional filtering on allowed elements
if (element.tagName === 'a') {
return element.properties.href !== '/blocked'
}
return true
}}
>
{'[OK](/page) and [Blocked](/blocked)'}
```
--------------------------------
### Define Components Mapping for react-markdown
Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md
The Components type allows mapping HTML tag names to custom React components. It accepts standard element properties along with extra properties provided by the library.
```typescript
import type {ExtraProps} from 'react-markdown'
import type {ComponentProps, ElementType} from 'react'
type Components = {
[Key in Extract]?: ElementType & ExtraProps>
}
```
--------------------------------
### Transform URLs with urlTransform
Source: https://context7.com/remarkjs/react-markdown/llms.txt
The urlTransform prop allows modification of all URLs within the markdown output. It is commonly used for prefixing paths, proxying assets, or stripping specific attributes for security.
```jsx
import Markdown from 'react-markdown'
// Prefix all URLs with a base path
{
if (url.startsWith('/')) {
return `/docs${url}`
}
return url
}}
>
{'[Link](/page) and '}
// Proxy images through a service
{
if (key === 'src' && node.tagName === 'img') {
return `https://proxy.example.com/?url=${encodeURIComponent(url)}`
}
return url
}}
>
{''}
// Remove all images by returning null for src
{
if (key === 'src') return null
return url
}}
>
{'\n\n[Kept](link)'}
```
--------------------------------
### Sanitize URLs with defaultUrlTransform
Source: https://context7.com/remarkjs/react-markdown/llms.txt
A utility function that enforces security by blocking dangerous protocols like 'javascript:' while allowing safe web and mail protocols.
```javascript
import Markdown, {defaultUrlTransform} from 'react-markdown'
// Default behavior - unsafe URLs are blocked
{'[Click me](javascript:alert(1))'}{'[Safe link](https://example.com)'}
// Using defaultUrlTransform directly
defaultUrlTransform('https://example.com') // Returns: 'https://example.com'
defaultUrlTransform('javascript:alert(1)') // Returns: ''
defaultUrlTransform('/relative/path') // Returns: '/relative/path'
defaultUrlTransform('mailto:user@example.com') // Returns: 'mailto:user@example.com'
```
--------------------------------
### Filter HTML Elements with allowedElements / disallowedElements
Source: https://context7.com/remarkjs/react-markdown/llms.txt
Control which HTML elements are included in the output using the allowedElements and disallowedElements options. Only one of these options can be used at a time. The unwrapDisallowed option can be used in conjunction to keep the content of disallowed elements.
```jsx
import Markdown from 'react-markdown'
// Only allow specific elements (others are removed)
{'# Heading\n\n**Bold** and *italic* in paragraph'}
// Output:
Bold and italic in paragraph
// Note: h1 is dropped entirely
// Disallow specific elements
{'**Bold** and *italic* text'}
// Output:
and text
// Unwrap disallowed elements (keep children)
{'# *Title*'}
// Output:
Title
// Note: em is removed but "Title" text is preserved
// Using allowedElements with unwrapDisallowed
{'# *Styled* heading'}
// Output:
Styled heading
```
--------------------------------
### Handle HTML with skipHtml
Source: https://context7.com/remarkjs/react-markdown/llms.txt
The skipHtml boolean prop controls whether raw HTML found in markdown is escaped or removed entirely. This is essential for preventing XSS attacks when rendering untrusted content.
```jsx
import Markdown from 'react-markdown'
import rehypeRaw from 'rehype-raw'
// Default behavior - HTML is escaped
{'
HTML content
'}
// With skipHtml - HTML is removed entirely
{'Text with inline HTML'}
// Comparison with rehype-raw (allows HTML)
{'
Allowed
'}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.