What is @mdx-js/react?
The @mdx-js/react package allows you to use MDX (Markdown for the component era) with React. MDX is a syntax that lets you seamlessly use JSX in your markdown documents. You can import components, such as interactive charts or alerts, and export metadata. This makes writing long-form content with React components a breeze.
What are @mdx-js/react's main functionalities?
Embedding JSX in Markdown
Allows embedding JSX directly in your markdown files, enabling the inclusion of React components within the content.
<MDXProvider><MyDocument /></MDXProvider>
Importing and using components in MDX
Enables the import and use of React components directly in MDX files, making it easy to integrate interactive or custom elements in markdown content.
import { Chart } from './components/Chart'
# My MDX document
Here's a chart component in my MDX document:
<Chart />
Using MDXProvider to customize components
Allows customization of standard markdown elements (like headings, paragraphs) with your own React components, providing a way to apply consistent styling or behavior across all markdown content.
import { MDXProvider } from '@mdx-js/react'
import { MyHeading, MyParagraph } from './MyComponents'
const components = {
h1: MyHeading,
p: MyParagraph
}
<MDXProvider components={components}>
<MyDocument />
</MDXProvider>
Other packages similar to @mdx-js/react
remark-react
Similar to @mdx-js/react, remark-react allows you to render markdown as React components. However, it uses the remark parsing library and does not support the direct use of JSX within markdown.
react-markdown
react-markdown is another library that lets you render markdown content as React components. It supports plugins to extend its functionality but, unlike @mdx-js/react, does not allow for the embedding of JSX components directly within the markdown.
markdown-to-jsx
markdown-to-jsx compiles markdown into JSX but is more focused on simplicity and performance. It doesn't offer the same level of integration with React components or the ability to import and use components directly in markdown files as @mdx-js/react does.
@mdx-js/react
Map components to HTML elements based on the Markdown syntax.
Serves as the React implementation for MDX.
Installation
npm:
npm install --save @mdx-js/react
Usage
import React from 'react'
import {renderToString} from 'react-dom/server'
import {MDXProvider, mdx} from '@mdx/react'
const H1 = props => <h1 style={{color: 'tomato'}} {...props} />
console.log(
renderToString(
<MDXProvider components={{ h1: H1 }}>
<h1>Hello, world!</h1>
</MDXProvider>
)
)
Yields:
<h1 style="color:tomato">Hello, world!</h1>
Contribute
See contributing.md
in mdx-js/mdx
for ways to get started.
This organisation has a Code of Conduct.
By interacting with this repository, organisation, or community you agree to
abide by its terms.
License
MIT © Compositor and ZEIT