Markdown is the undisputed champion of writing for the web. Its simplicity has made it the standard for everything from README files to entire blogs. But what happens when static text and images aren't enough? What if you want to embed an interactive chart, a dynamic form, or a custom-styled callout box directly into your content?
Traditionally, this meant ejecting from the comfort of Markdown and wrestling with raw HTML or complex templating languages. But there's a better way.
Enter MDX. It's the superset of Markdown you've been dreaming of, letting you seamlessly blend the simplicity of Markdown with the full power of React components. It's a paradigm shift that enables true CONTENT AS CODE. This guide will show you how to leverage MDX to create rich, interactive experiences and how services like mdx.do make it possible to do this dynamically, without a complex build step.
MDX is an authorable format that lets you write JSX directly inside your Markdown files. Think of it as Markdown with superpowers. You can write standard Markdown as you always have, but now you can also import and render React components right alongside your text.
import { Chart } from './components'
# Q3 Sales Report
Here's a breakdown of our performance this quarter.
<Chart data={...} />
As you can see, growth is strong.
The ability to use components unlocks a world of possibilities:
The power of MDX typically comes with a prerequisite: a build step. Frameworks like Next.js and Gatsby have official integrations that process and compile your .mdx files into web pages when you build your site.
This is perfect for static websites. But what about dynamic content?
What if your blog posts are stored in a headless CMS? Or your documentation is pulled from a database? What if you want to render user-generated content on the fly? In these scenarios, a static build process is a non-starter. You need to compile MDX in real-time.
This is precisely the problem mdx.do solves. It's a simple, powerful API that transforms your MDX content into interactive React components in real-time.
Instead of bundling a compiler into your application or running a complex server-side build process, you can send a raw MDX string to the mdx.do API and get back executable JavaScript code. This decouples your content from your build process, giving you incredible flexibility.
Let's see just how easy it is. Imagine you have MDX content stored in a database or coming from a headless CMS.
import { mdx } from ".do";
const mdxContent = `
# Hello, MDX!
This is a JSX component rendered from a string:
<MyCustomComponent title="Welcome" />
Expressions are easy: {2024 - 1984} years have passed.
`;
// Compile the MDX string into executable code via an API call
const { code, frontmatter } = await mdx.compile(mdxContent);
// `code` can now be safely evaluated and rendered
// in your React application.
Let's break down what's happening:
In your front-end application, you can then use a library like run-mdx or next-mdx-remote to evaluate this code and render it, providing your implementation of MyCustomComponent at render time.
Yes, absolutely! This is the most powerful feature. The magic lies in providing a "scope" of components during the rendering phase.
When mdx.do compiles your content, it doesn't need to know what <MyCustomComponent> is. It simply ensures the compiled code will look for a component with that name.
In your React app, you provide the definition:
// Your React Application
import { MdxComponent } from 'your-mdx-renderer';
import MyCustomComponent from './components/MyCustomComponent';
const components = {
MyCustomComponent, // Map the name to the actual component
};
export default function Page({ compiledCode }) {
return (
<main>
<MdxComponent code={compiledCode} components={components} />
</main>
);
}
Now, whenever the MDX content calls <MyCustomComponent>, your renderer will use the component you provided.
This dynamic approach opens the door for a new class of applications:
Markdown gave us a simple way to write for the web. MDX gives us a way to make that writing come alive. By removing the friction of a local build step, services like mdx.do make this power accessible for any application, static or dynamic.
Ready to transform your content? Turn your Markdown into rich, component-driven experiences and see what you can build.