Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 7

Introduction to MDX: Markdown & JSX Combined

Recommended resources

Hello and welcome to the second module of our course!

In the first module, we built a solid foundation in Astro. We learned how it uses a Static Site Generation (SSG) approach for performance, how to create pages and reusable components, and, most importantly, how Astro uses the "islands architecture" to add pockets of interactivity to otherwise static pages.

Now, we shift our focus from the structure of the site to its content. Your goal is to build a content-driven accounting website with blogs, service pages, and case studies. To do this effectively, we need a powerful and flexible way to write that content. This is where MDX comes in.

This lesson kicks off our new module, "Content Authoring with MDX and Collections." Our objective is to explain what MDX is and how it combines Markdown with JSX. Since you're already familiar with Markdown for documentation and JSX from your work with React, you're in a great position to understand the synergy between them.

1. From Markdown to MDX

Let's start with what you know. Markdown is a lightweight markup language designed for one thing: writing formatted text in a simple, human-readable way. You write **bold** instead of <strong>bold</strong>, and # Heading instead of <h1>Heading</h1>. A compiler then turns this into HTML for the browser.

This is fantastic for static content like articles or README files. But it has a major limitation: you're restricted to the standard set of HTML elements that Markdown supports. What if you wanted to embed an interactive chart, a dynamic form, or a custom-styled call-to-action button directly within your blog post?

This is the problem that MDX solves. MDX is a superset of Markdown that allows you to write JSX directly inside your content files. You get the simple authoring experience of Markdown, supercharged with the full power of components.


2. The Power of Combining Markdown and JSX

To see what this means in practice, let's look at an article by developer Josh W. Comeau, who explains this concept beautifully.

How I Built my Blog using MDX, Next.js, and React

This article, "How I Built my Blog using MDX, Next.js, and React", clearly explains the core value proposition of MDX from a developer's perspective. While he uses Next.js, the principles of MDX are universal and apply directly to our work in Astro.

Please read the section titled "MDX, The secret ingredient". Pay close attention to the example where a <PieChart> React component is embedded directly within the Markdown text. This is the central idea of MDX.

As you saw in the article, you can seamlessly switch from writing standard Markdown to embedding a component with its own props and logic:

import PieChart from '../components/PieChart';

# Quarterly Expense Report

Here is a summary of our expenses for Q1. As you can see, marketing remains our largest expenditure.

<PieChart
  title="Q1 Expenses"
  data={[
    { label: 'Marketing', value: '45%' },
    { label: 'Salaries', value: '35%' },
    { label: 'Operations', value: '20%' },
  ]}
/>

Next quarter, we plan to optimize our marketing spend...

This is a game-changer for content. Imagine a blog post on your accounting site explaining tax deductions. Instead of just writing about it, you could embed a small, interactive "Deduction Calculator" React component right in the middle of the article. This creates a much richer and more engaging user experience.

MDX and Next.js Tutorial Web Page Example
This image illustrates the final rendered output. You can see standard Markdown formatting (bold text, a list) alongside the output of a React component, all on the same page.

3. Why Not Just Use React for Everything?

Given your background, a natural question arises: "If I'm using React components, why not just build the whole page in React?"

This is a valid point, and Josh Comeau's article addresses it well. While you could create a separate React component for each blog post, using MDX offers two significant advantages for a content-heavy site:

  1. A Better Authoring Experience: Writing prose is much more natural and efficient in Markdown. You don't have to wrap every paragraph in <p> tags or manually handle lists and links. This speeds up content creation for you or any non-developer who might contribute to the site in the future.
  2. Content as Data: MDX files are structured data. They can contain frontmatter (metadata at the top of the file, like title, author, publishDate). As we'll see later in the course, Astro can programmatically read this metadata from all your files. This allows you to automatically generate a blog listing page, sort posts by date, create archive pages for categories, and much more—a process that is much harder if each post is a self-contained React component.

MDX gives us the perfect middle ground: the simple authoring of a CMS with the full technical power of a component-based framework.


4. MDX in the Astro Ecosystem

Astro has first-class support for MDX through an official integration. This allows you to create pages and content files using the .mdx extension.

The key thing to know is that within an Astro project, you can import and use both Astro components (.astro) and UI framework components (.js/.jsx) inside your MDX files.

Let's look at the official documentation to see how this works.

astrojs/mdx - Astro Docs

The official Astro documentation for the @astrojs/mdx integration provides specific examples of how to use MDX within an Astro project.

Please read the following sections: "Why MDX?": A quick summary of the benefits. "MDX in Astro": This explains how Astro enhances standard MDX. "Using Components in MDX": This is the most important part. Look at the code example showing how a ReactCounter component is imported and used with a client:load directive. This directly connects to our last lesson on Astro Islands!

As the documentation shows, using a React component in an MDX file in Astro looks like this:

---
title: My Interactive Post
---
import ReactCounter from '../components/ReactCounter.jsx';

# Welcome to my interactive post!

This content is written in Markdown. But here is a fully interactive React component, hydrated using an Astro client directive:

<ReactCounter client:load />

Isn't that powerful?

Notice the client:load directive. This should be familiar from our previous lesson. This is how you tell Astro that this specific component is an "island of interactivity" that needs its JavaScript sent to the browser. You get to combine the content-authoring benefits of MDX with the performance benefits of the Islands Architecture.

Test your understanding!

Imagine you're creating a "Services" page for the accounting website. The page will be an .mdx file. It needs to describe the "Tax Planning" service.

The content requires:

  1. A main heading: "Tax Planning Services".
  2. An introductory paragraph explaining the service.
  3. A custom-styled, interactive "Request a Consultation" button component (let's call it <ConsultButton>) that you've built with React.
  4. A bulleted list of benefits.

How would you structure the .mdx file to achieve this? Write a short pseudo-code or outline of the file's content.

Show answer

Your .mdx file would look something like this:

---
title: "Tax Planning"
description: "Proactive tax planning strategies to minimize your liabilities and maximize savings."
---
import ConsultButton from '../components/ConsultButton.jsx';

# Tax Planning Services

Our expert tax planning service helps individuals and businesses develop proactive strategies to minimize tax liabilities. We work with you year-round, not just at tax time.

<ConsultButton client:visible />

Key benefits include:
*   Identifying tax-saving opportunities.
*   Ensuring compliance with all regulations.
*   Long-term financial strategy alignment.

This demonstrates the key concept: you use Markdown for the heading (#), paragraph, and bulleted list (*), and you import and use the JSX <ConsultButton /> component for the interactive element, applying a client directive to turn it into an island.


Conclusion

In this lesson, we've unpacked the "what" and "why" of MDX. It's a foundational technology for our goal of building a dynamic, content-driven website.

Here are the key takeaways:

  • MDX = Markdown + JSX: It lets you write content using the simple syntax of Markdown and embed powerful, interactive components directly where you need them.
  • Best of Both Worlds: You get a superior authoring experience for prose-heavy content while retaining the ability to create rich, application-like experiences.
  • Perfect for Astro: MDX integrates seamlessly with Astro's Islands Architecture, allowing you to create interactive content islands (<MyComponent client:visible />) within your Markdown.

Up Next: We've covered the theory. In the next lesson, we'll get our hands dirty. We will set up the MDX integration in our Astro project and introduce Content Collections, Astro's built-in system for organizing and validating our content with type-safety. This is where the foundation for our blog and services section truly begins.