š MDX Tutorial ā Write JSX Inside Markdown
š What is MDX?
MDX is an extension of Markdown that allows you to useJSX components directly inside your Markdown files.
It combines the simplicity of Markdown with the power of React components, making it perfect for documentation websites, blogs, portfolios, and learning platforms.
>>"MDX = Markdown + JSX"
⨠Why Use MDX?
- š Write content using Markdown.
- āļø Embed React components directly.
- š Add charts, alerts, tabs, and custom UI.
- š Perfect for documentation.
- š Used by Docusaurus, Next.js, Astro, Gatsby, and many more.
š¦ Installing MDX
For a React Project
Install MDX
npm install @mdx-js/react @mdx-js/loader @mdx-js/mdxš Your First MDX File
Create a file named:
hello.mdxhello.mdx
# Hello World
This is **Markdown**.
<button>Click Me</button>Everything above is valid MDX.
š Markdown Features
Headings
Headings
# Heading 1
## Heading 2
### Heading 3Bold & Italic
Formatting
**Bold**
*Italic*
~~Strikethrough~~Lists
Lists
- Apple
- Mango
- Orange
1. Learn
2. Build
3. DeployLinks
Links
[OpenAI](https://openai.com)Images
Images
āļø Using React Components
The biggest feature of MDX is embedding React components.
Alert.jsx
export default function Alert({children}) {
return (
<div style={{background:"#ffeeba",padding:20}}>
{children}
</div>
);
}Using Alert
import Alert from "./Alert"
# Welcome
<Alert>
This is an alert box.
</Alert>š„ Passing Props
Props Example
<Button
color="blue"
text="Click Here"
/>šÆ JavaScript Expressions
You can use JavaScript inside curly braces.
Expressions
export const name = "John"
# Hello {name}
The value is {10 + 5}.š¦ Importing Components
Import Example
import Card from "./Card"
<Card title="MDX">
Learning MDX is fun.
</Card>š Typical Project Structure
Folder Structure
project/
āāā components/
ā āāā Alert.jsx
ā āāā Card.jsx
ā
āāā docs/
ā āāā intro.mdx
ā āāā guide.mdx
ā
āāā package.jsonš MDX in Next.js
Install
npm install @next/mdxnext.config.js
const withMDX = require('@next/mdx')()
module.exports = withMDX({
pageExtensions: ['js','jsx','md','mdx'],
})š MDX in Docusaurus
Docusaurus uses MDX by default. Simply create:
docs/tutorial.mdxand start writing Markdown mixed with React components.
š„ MDX Components Example
Multiple Components
import Card from "./Card"
import Alert from "./Alert"
# Dashboard
<Alert>
Important update!
</Alert>
<Card title="Users">
1500 Active Users
</Card>š” Advantages
- ā” Easy to write.
- āļø Full React support.
- š Great for documentation.
- šØ Beautiful UI using components.
- š Reusable content blocks.
ā ļø Limitations
- Requires a build setup.
- Needs JSX knowledge.
- Can become harder to read if overloaded with components.
Note
š” Use Markdown for content and React components only when interactive or reusable UI is needed. This keeps your MDX files clean and maintainable.
šÆ Best Practices
- Keep Markdown simple.
- Reuse components instead of repeating markup.
- Organize components in a dedicated folder.
- Prefer small, focused components.
- Use syntax highlighting for code blocks.
š Quick Cheat Sheet
| Feature | Syntax |
|---|---|
| Heading | # Hello |
| Bold | **Bold** |
| Italic | *Italic* |
| List | - Item |
| Link | [Text](URL) |
| Image |  |
| Component | <Alert /> |
| Import | import Card from "./Card" |
| JavaScript | {2 + 2} |
š Learn More
For the official documentation, visit:
MDX Official Documentation>>š MDX empowers developers to create rich, interactive documentation by blending the readability of Markdown with the flexibility of React components.