### 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: , , , // Footnotes with GFM {'Here is a footnote[^1]\n\n[^1]: Footnote content'} // Plugin with options (single tilde disabled for strikethrough) {'~not strikethrough~ but ~~this is~~'} // Output:

~not strikethrough~ but this is

// Table of contents generation {'# Doc\n## Contents\n## Section 1\n## Section 2'} ``` -------------------------------- ### Enable HTML in Markdown with rehype-raw Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md Demonstrates how to use the rehype-raw plugin to safely render HTML tags within markdown content. This requires the rehype-raw dependency and should only be used in trusted environments due to security implications. ```javascript import React from 'react' import {createRoot} from 'react-dom/client' import Markdown from 'react-markdown' import rehypeRaw from 'rehype-raw' const markdown = `
Some *emphasis* and strong!
` createRoot(document.body).render( {markdown} ) ``` -------------------------------- ### Configure AST transformation with remarkRehypeOptions Source: https://context7.com/remarkjs/react-markdown/llms.txt The remarkRehypeOptions prop allows passing configuration to the underlying remark-rehype processor. This is used to customize footnote IDs, label tags, and other AST-to-HTML conversion behaviors. ```jsx import Markdown from 'react-markdown' import remarkGfm from 'remark-gfm' // Customize footnote ID prefix {'Text[^1]\n\n[^1]: Footnote'} // Configure how handlers work {'Content with footnote[^ref]\n\n[^ref]: Note text'} ``` -------------------------------- ### Render Markdown Synchronously with react-markdown Source: https://context7.com/remarkjs/react-markdown/llms.txt The primary component for rendering markdown content synchronously. It supports standard CommonMark syntax including headings, code blocks, lists, and blockquotes. ```jsx import React from 'react' import {createRoot} from 'react-dom/client' import Markdown from 'react-markdown' // Basic usage const basicMarkdown = '# Hello World\n\nThis is **bold** and *italic* text.' createRoot(document.getElementById('root')).render( {basicMarkdown} ) // With children prop // Code blocks with language const codeExample = '```javascript\nconst x = 42;\nconsole.log(x);\n```' {codeExample} // Lists and blockquotes const listMarkdown = ` > A blockquote - Item 1 - Item 2 - Nested item 1. Ordered item 2. Another item ` {listMarkdown} ``` -------------------------------- ### Use custom components for syntax highlighting in react-markdown Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md Illustrates overriding default element rendering in react-markdown to implement custom behavior, such as syntax highlighting for code blocks using react-syntax-highlighter. This involves providing a 'components' prop with a custom 'code' component. ```javascript import React from 'react' import {createRoot} from 'react-dom/client' import Markdown from 'react-markdown' import {Prism as SyntaxHighlighter} from 'react-syntax-highlighter' import {dark} from 'react-syntax-highlighter/dist/esm/styles/prism' // Did you know you can use tildes instead of backticks for code in markdown? ✨ const markdown = `Here is some JavaScript code: ~~~js console.log('It works!') ~~~` createRoot(document.body).render( ) : ( {children} ) } }} /> ) ``` -------------------------------- ### Configure rehypePlugins for HTML AST Transformation Source: https://context7.com/remarkjs/react-markdown/llms.txt The rehypePlugins option allows you to apply rehype plugins to transform the HTML Abstract Syntax Tree (AST) after markdown conversion. This is useful for tasks like syntax highlighting, sanitizing HTML output for security, or enabling raw HTML support. Multiple plugins can be combined. ```jsx import Markdown from 'react-markdown' import rehypeRaw from 'rehype-raw' import rehypeSanitize from 'rehype-sanitize' import rehypeKatex from 'rehype-katex' import remarkMath from 'remark-math' // Allow raw HTML in markdown (use with caution) {'
**Bold inside div**
'}
// Output:
Bold inside div
// Sanitize output for extra security {'# Safe content'} // Math rendering with KaTeX {'The equation $E = mc^2$ is famous.'} // Multiple plugins combined {complexMarkdownWithMathAndTables} ``` -------------------------------- ### Customize react-markdown Components Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md Demonstrates how to customize the HTML elements rendered by react-markdown. You can map markdown elements like 'h1' to different HTML tags or provide custom React components with specific props. This allows for fine-grained control over the rendering of markdown content. ```javascript } }} /> ``` -------------------------------- ### Render Markdown with Hooks using MarkdownHooks Source: https://context7.com/remarkjs/react-markdown/llms.txt A client-side component that handles asynchronous plugins with React hooks. It supports a fallback prop to display content while plugins are processing. ```jsx import {MarkdownHooks} from 'react-markdown' import rehypeStarryNight from 'rehype-starry-night' function CodeViewer({code}) { return ( Loading syntax highlighting...} rehypePlugins={[rehypeStarryNight]} > {code} ) } {'# Title\n\nContent here'} ``` -------------------------------- ### Render Markdown Asynchronously with MarkdownAsync Source: https://context7.com/remarkjs/react-markdown/llms.txt A component designed for server-side rendering that supports asynchronous plugins. It is ideal for tasks like server-side syntax highlighting. ```jsx import {MarkdownAsync} from 'react-markdown' import {renderToPipeableStream} from 'react-dom/server' import rehypeStarryNight from 'rehype-starry-night' const code = '```js\nconsole.log("Hello")\n```' renderToPipeableStream( {code} ).pipe(response) const markdown = '# Async Rendered\n\nThis content is rendered asynchronously.' {markdown} ``` -------------------------------- ### Handle Line Endings in react-markdown Source: https://github.com/remarkjs/react-markdown/blob/main/readme.md Explains the correct way to handle line endings when passing markdown strings to react-markdown, especially within JSX. It recommends using template literals and passing the markdown as a single child expression to avoid issues with whitespace collapsing. ```javascript // If you write actual markdown in your code, put your markdown in a variable; // **do not indent markdown**: const markdown = ` # This is perfect! ` // Pass the value as an expression as an only child: const result = {markdown} ``` ```javascript {` # Hi This is a paragraph. `} ``` -------------------------------- ### Customize Rendering with the components Option Source: https://context7.com/remarkjs/react-markdown/llms.txt The components option in react-markdown allows you to map HTML tag names to custom React components or functions. This provides complete control over how markdown elements are rendered, enabling simple tag replacements, complex custom logic, or integration with libraries like syntax highlighters. ```jsx import Markdown from 'react-markdown' import {Prism as SyntaxHighlighter} from 'react-syntax-highlighter' import {dark} from 'react-syntax-highlighter/dist/esm/styles/prism' // Simple tag replacement {'# This becomes h2'} // Output:

This becomes h2

// 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 }} > {'![Image](pic.jpg)\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 ![Image](/img.png)'} // Proxy images through a service { if (key === 'src' && node.tagName === 'img') { return `https://proxy.example.com/?url=${encodeURIComponent(url)}` } return url }} > {'![Photo](https://external.com/photo.jpg)'} // Remove all images by returning null for src { if (key === 'src') return null return url }} > {'![Removed](photo.jpg)\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.