The choice between Astro MDX and Markdoc comes down to trust and strictness. MDX treats your Markdown files as JavaScript modules. It allows arbitrary imports and complex inline logic directly in the content file, giving you total freedom at the cost of stability. Markdoc, developed by Stripe for their engineering documentation, does not allow mixing arbitrary code and content. It parses your content into an Abstract Syntax Tree (AST) against a strict, predefined schema.
If you run a personal homelab blog or a small portfolio site where you are the sole author, MDX makes sense. If you are building documentation sites, wikis, or project manuals with multiple contributors, Markdoc is the superior choice. Its strict schema enforcement prevents broken JSX syntax from failing your CI pipelines and breaking the site.
The Core Architectural Difference
When Astro processes an MDX file via the @astrojs/mdx integration, it passes the document through Vite and Rollup. Every .mdx file is compiled into a JSX module. You can write standard JavaScript import statements at the top of the file, bring in React or Svelte components, and drop them directly into the text.
Markdoc operates under a fundamentally different model via the @astrojs/markdoc integration. A .mdoc file does not compile down to a standalone script. It is parsed by Markdoc's custom engine into an AST. You cannot import anything inside a Markdoc file. Instead, you define custom tags centrally in a configuration file and map those tags to your Astro components. Content authors then use a liquid-style syntax, such as {% callout %}, to invoke those components.
MDX: Unrestricted Flexibility
MDX is the default standard for many static site generators. It requires virtually zero configuration. Once you install the integration, you change your file extensions to .mdx and start writing.
This approach excels for highly custom, one-off pages. If you need a unique interactive chart on a single blog post, you just import the charting library directly into that specific .mdx file. You do not have to update a global configuration to support a component you will only use once.
The flexibility of MDX becomes a liability on content-heavy sites. Because MDX evaluates as code, a single syntax error breaks the entire page build. A missing closing bracket on a component, an unescaped curly brace in a paragraph, or a typo in an import path will cause Vite to throw a fatal error. When non-developers write content, they inevitably trigger these build failures.
Furthermore, MDX tightly couples your content to your current component architecture. If you embed import { Notice } from '../../components/Notice.astro' across four hundred files, migrating that component or changing the folder structure requires a massive find-and-replace operation.
Markdoc: Strict Schema Enforcement
Markdoc solves the fragility of MDX by removing logic from the content layer. A .mdoc file only contains Markdown and explicit custom tags.
By defining a schema in your project root, you declare exactly which tags are allowed, what attributes they accept, and what data types those attributes require. If an author forgets a mandatory attribute or passes an integer where a string is expected, Markdoc catches the error during validation. This shifts the failure point from a cryptic Vite build error to a clear, schema-based validation warning.
The trade-off is configuration overhead. You cannot drop a new component into a specific page on a whim. Every interactive element requires registration in the central configuration file.
Variables and Conditional Rendering
MDX handles dynamic content using standard JavaScript. If you want to conditionally render a block of text, you use standard JSX syntax like {featureEnabled && <p>New UI Active</p>}.
Markdoc approaches this using built-in variables. You define global variables in your Astro configuration and reference them in your content with a dollar sign. Markdoc includes native {% if %} tags for conditional logic based on these variables.
{% if $featureFlags.newUI %}
The new interface is now active on your dashboard.
{% /if %}
This variable system is highly effective for documentation versioning. You can pass a version string as a variable and conditionally hide or show API endpoints in the text based on the specific documentation build, all without writing raw JavaScript in the content file.
Content Collections Compatibility
Both formats integrate natively with Astro Content Collections. You define your frontmatter schemas using Zod in src/content.config.ts. The frontmatter validation is identical regardless of whether you author the file in MDX or Markdoc.
Where they differ is how they handle the body content. In an MDX collection entry, you render the body using Astro's `` component, which executes the compiled JSX. In a Markdoc collection entry, Astro maps the AST nodes to the components you defined in your Markdoc configuration before rendering the HTML.
Installation and Configuration
To evaluate Markdoc in an existing Astro environment, use the Astro CLI to install the integration.
`bash
Install the Markdoc integration
npx astro add markdoc
`
This command automatically updates your astro.config.mjs file. Next, create a markdoc.config.mjs file at the root of your project. This is where you map your custom tags to your Astro components.
`javascript
import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
tags: {
banner: {
render: component('./src/components/Banner.astro'),
attributes: {
type: { type: String, default: 'info' },
title: { type: String, required: true }
}
}
}
});
`
Content authors can now use the {% banner %} tag in any .mdoc file within your content collections. They do not need to know the path to the component or understand how it works under the hood.
`markdown
{% banner type="warning" title="Deprecation Notice" %}
This API endpoint is scheduled for removal in the next major release.
{% /banner %}
`
Editor Experience and Tooling
The developer experience in the editor highlights the philosophical divide between the two formats.
With MDX, your editor treats the file as TypeScript or JSX. You get standard autocomplete for React or Astro components, but only if you write the import statement correctly. Spellcheckers and Markdown linters often struggle with MDX because the intertwined code confuses the parsers.
Markdoc offers a dedicated language server and VS Code extension. By creating a markdoc.config.json file in your project root, you point the extension to your markdoc.config.mjs schema.
`json
[
{
"id": "docs-site",
"path": "src/content",
"schema": {
"path": "markdoc.config.mjs",
"type": "esm",
"property": "default",
"watch": true
}
}
]
`
Once configured, the Markdoc extension provides native autocomplete for tags. When an author types {%, a dropdown appears showing all permitted tags. If they select banner, the editor prompts them to fill in the required title attribute. This intelligent tooling bridges the gap between raw text editors and complex headless CMS interfaces, making it drastically easier for non-technical users to contribute structured content safely.
Astro Starlight Integration
Astro's official documentation theme, Starlight, supports both MDX and Markdoc natively. By default, Starlight supports authoring content in Markdown and MDX with no configuration required. If you add the Markdoc integration, Starlight provides a Starlight Markdoc preset. This preset automatically registers Starlight's built-in UI components, such as <Card> and <Tabs>, as Markdoc tags.
You can initialize a Markdoc-enabled Starlight project directly from the Astro CLI:
`bash
npm create astro@latest -- --template starlight/markdoc
`
This creates a ready-to-deploy documentation site where authors can use {% card title="Notice" %} immediately, without writing a custom markdoc.config.mjs file from scratch. Starlight's implementation proves that Markdoc scales to massive documentation hubs while keeping the authoring experience simple.
Hosting Your Built Documentation
Astro compiles both MDX and Markdoc down to static HTML files during the build process. Unless you specifically configure Server-Side Rendering (SSR), the output is entirely static.
For a homelab or self-hosted deployment, serving these static files via a lightweight web server is the most efficient method. After running npm run build, Astro places the generated HTML, CSS, and optimized assets into the dist directory.
You can serve this directory using a standard Nginx container with Docker Compose. This stack uses fewer than 15MB of RAM and handles thousands of concurrent requests.
`yaml
services:
docs-site:
image: nginx:1.25-alpine
container_name: astro-docs
ports:
- "8080:80"
volumes:
- ./dist:/usr/share/nginx/html:ro
restart: unless-stopped
`
Deploying this configuration requires a simple docker compose up -d. The Nginx container will serve your Astro site directly from the mapped volume.
Which Format Fits Your Site?
Pick MDX if you are building a personal site, a developer blog, or a highly interactive presentation. The ability to import arbitrary components into a specific file eliminates the friction of maintaining a global schema. If you are the only person writing the content, you can manage the risk of syntax errors yourself.
Pick Markdoc if you are building an organizational wiki, API documentation, or any project where multiple people contribute text. The separation of logic from content keeps your files clean and readable. The schema validation acts as a guardrail, ensuring authors cannot deploy malformed JSX and break the site build.
Using Markdoc also future-proofs your content. Because your .mdoc files contain no framework-specific import statements, migrating your documentation from Astro to another platform in the future requires significantly less effort than untangling a web of MDX imports.
What's Next
- Astro MD vs MDX: Content Collection Build Performance
- Pagefind Astro Tutorial: Static Site Full Text Search
- Astro Starlight Testing: Test Code Snippets in CI
- Migrating Next.js to Astro: What Changes and Why
FAQ
Can I use both MDX and Markdoc in the same Astro project?
Yes. You can install both @astrojs/mdx and @astrojs/markdoc integrations simultaneously and map them to different content collections.
Does Markdoc support React components in Astro?
Yes. As long as the React component is configured in markdoc.config.mjs, Markdoc renders it via Astro's component framework.
Do I need a Node server to host Astro Markdoc sites?
No. Astro compiles both MDX and Markdoc down to static HTML by default. The output can be hosted on any standard web server like Nginx or Caddy.
Originally published at linuxcore.dev.
Top comments (0)