🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Comparisons••By Komal Nakrani

MDX vs Markdoc in Astro: Which Content Format?

MDX vs Markdoc in Astro compared: syntax, components, safety, CMS editing, validation and setup, with the same content written both ways and a clear rule for choosing.

#Astro#MDX#Markdoc#Content Collections

MDX vs Markdoc in Astro is a choice between power and constraints. MDX lets you import and use any component, write JavaScript expressions and export values right inside Markdown, which suits developer-written content. Markdoc only allows the tags and variables you register in a config file, which keeps content safe, predictable and easy for a CMS or non-developers to edit. Both have official Astro integrations and both work with Content Collections.

Key Takeaways

  • MDX = Markdown plus JSX: imports, components and JavaScript expressions inside content files.
  • Markdoc = Markdown plus a tag syntax you define centrally in markdoc.config.mjs; no arbitrary code in content.
  • MDX is faster for developers; Markdoc is safer when writers, CMS tools or many contributors edit content.
  • Install with npx astro add mdx or npx astro add markdoc; you can use both in one project.
  • Keystatic’s best rich-text editing uses Markdoc, which is a common reason teams pick it.

What MDX is

MDX extends Markdown with JSX. In an .mdx file you can import a component, render it with props, use JavaScript expressions in curly braces, and export variables. Astro compiles each MDX file into a component, so anything you can do in an Astro page you can mostly do inside your content.

---
# src/content/blog/launch.mdx
title: Launch notes
---
import Callout from '../../components/Callout.astro';
export const version = '7.4';

# What changed in {version}

<Callout type="warning" title="Breaking change">
  The old option was removed. Update your config before upgrading.
</Callout>

Regular **Markdown** keeps working around components.

Because it is real JSX, mistakes show up as compile errors. An unclosed tag, a stray < in a sentence, or an unescaped curly brace breaks the build until you fix it. That strictness is a feature for developers and a hazard for writers.

What Markdoc is

Markdoc, created by Stripe for its documentation, extends Markdown with a tag syntax. Tags, variables and functions are declared in a config file. Content can only use what is declared, and attributes are validated against the types you define.

---
# src/content/docs/launch.mdoc
title: Launch notes
---

# What changed in {% $version %}

{% callout type="warning" title="Breaking change" %}
The old option was removed. Update your config before upgrading.
{% /callout %}

Regular **Markdown** keeps working around tags.

There are no imports and no JavaScript in the file. The callout tag maps to an Astro component in configuration, and $version is a variable passed in when the page renders. Markdoc also supports conditionals (if and else tags) and functions such as equals, which helps for docs that vary by product version or audience.

MDX vs Markdoc at a glance

MDX Markdoc
Astro integration @astrojs/mdx @astrojs/markdoc
File extension .mdx .mdoc
Components Import any component in the file Map tags to components in markdoc.config.mjs
JavaScript in content Yes: expressions, exports, imports No; only variables and registered functions
Validation Compile errors from the JSX parser Typed tag attributes, validated against config
Error tolerance Strict; stray < or { breaks the build Plain Markdown characters are safe
Variables export const or props Variables passed to <Content />
Conditionals JavaScript expressions Built-in if and else tags
CMS support TinaCMS, Keystatic (fields.mdx), others Keystatic (first-class), others
Editor tooling Wide (VS Code MDX extension, Prettier) Markdoc language server and extension
Best for Developer blogs, interactive posts, demos Docs, large writer teams, CMS-edited content

Setting up each in Astro

Both integrations install with one command:

npx astro add mdx
npx astro add markdoc
// astro.config.mjs
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
import markdoc from '@astrojs/markdoc';

export default defineConfig({
  integrations: [mdx(), markdoc()],
});

Then register your Markdoc tags and node overrides:

// markdoc.config.mjs
import { defineMarkdocConfig, component, nodes } from '@astrojs/markdoc/config';

export default defineMarkdocConfig({
  tags: {
    callout: {
      render: component('./src/components/Callout.astro'),
      attributes: {
        type: { type: String, matches: ['info', 'tip', 'warning'] },
        title: { type: String },
      },
    },
  },
  nodes: {
    heading: {
      ...nodes.heading,
      render: component('./src/components/Heading.astro'),
    },
  },
});

The matches list means a writer who types type="danger" gets a validation error instead of a broken callout. With MDX, the equivalent is TypeScript props on the component, which only help inside an editor that type-checks MDX.

Using both with Content Collections

A single collection can hold .md, .mdx and .mdoc files. The glob() loader picks them up by pattern, and render() returns a Content component for each.

// src/content.config.ts
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { z } from 'astro/zod';

const docs = defineCollection({
  loader: glob({ pattern: '**/*.{md,mdx,mdoc}', base: './src/content/docs' }),
  schema: z.object({ title: z.string() }),
});

export const collections = { docs };
---
// src/pages/docs/[...id].astro
import { getCollection, render } from 'astro:content';
import Callout from '../../components/Callout.astro';

export async function getStaticPaths() {
  const entries = await getCollection('docs');
  return entries.map((entry) => ({ params: { id: entry.id }, props: { entry } }));
}

const { entry } = Astro.props;
const { Content } = await render(entry);
---
<Content components={{ Callout }} version="7.4" />

For MDX files, the components prop lets content use <Callout> without importing it, and overrides HTML elements such as h2 or a. For Markdoc files, extra props such as version become variables, read as $version in content. The Content Collections guide covers loaders and schemas in detail.

Safety and who writes the content

This is the deciding factor.

With MDX, content is code. A content file can import any module, run expressions at build time and render any component. That is fine when the authors are developers reviewing each other’s pull requests. It is risky when content comes from a CMS, external contributors or AI-assisted drafts, because a malformed file can break the build or include markup you didn’t intend.

With Markdoc, content is data. Writers can only use the tags you registered, with attributes you validated. A stray < or { in a sentence is just text. Changing how every callout renders means editing one component, not hundreds of files. For a docs site with dozens of writers, that central control is worth the extra config.

Developer experience and ecosystem

MDX has the larger ecosystem. Most Markdown tooling supports it: syntax highlighting, Prettier, the VS Code extension, remark and rehype plugins, and many Astro themes and starters. Astro’s MDX integration supports the same remark and rehype plugins as Markdown, configured in markdown or per-integration options.

Markdoc’s ecosystem is smaller but focused on documentation. It has a language server for editor support and its own transform pipeline. Astro’s Markdoc integration renders Shiki or Prism code blocks and supports custom nodes, and you can enable allowHTML if you need raw HTML in content, although that removes part of the safety benefit.

Performance is the same for visitors. Both compile to static HTML at build time, and both only ship JavaScript if a component you render is a hydrated island.

Choose MDX or Markdoc

Choose MDX if developers write the content, posts include interactive demos or one-off components, you want to import components per file, or you rely on the remark and rehype plugin ecosystem. Most Astro blogs use MDX for these reasons.

Choose Markdoc if writers or a CMS edit content, you run a documentation site with many contributors, you want validated, centrally defined components, or you use Keystatic as your CMS. The Keystatic vs TinaCMS vs Decap comparison shows how Markdoc fits a git-based editing workflow.

Choose plain Markdown if content needs no components at all. It is the simplest and most portable format, and Astro supports it with no integration.

Komal themes use MDX for blog content, so you can drop components into posts from day one; the Content Collections docs show where each theme keeps its content. For blog-focused designs, see the blog and editorial themes, and for the official option reference, read the Astro Markdoc integration docs.

FAQ

What is the difference between MDX and Markdoc?

MDX is Markdown with JSX, so content files can import components and run JavaScript expressions. Markdoc is Markdown with a custom tag syntax, where every tag and attribute is declared and validated in a config file, and content cannot run code. MDX gives authors more power; Markdoc gives maintainers more control.

Can I use MDX and Markdoc together in Astro?

Yes. Install both @astrojs/mdx and @astrojs/markdoc, and a Content Collection with a glob() pattern such as **/*.{md,mdx,mdoc} loads all three formats. Each file renders through its own integration.

Is Markdoc better for documentation?

Markdoc was built for documentation and suits it well: validated tags, variables, conditionals for versioned content and no code in content files. Many docs sites still use MDX, including Starlight sites, so it is a good fit rather than a requirement.

Does MDX slow down an Astro site?

No. MDX compiles to static HTML at build time, so visitors get the same output as with Markdown. Builds can be slightly slower with many large MDX files, and any component you hydrate with a client:* directive adds JavaScript.

How do I use components in Markdoc with Astro?

Register a tag in markdoc.config.mjs with render: component('./src/components/YourComponent.astro') and declare its attributes. Writers then use the tag syntax with those attributes in .mdoc files, and Astro renders your component in its place.