Skip to main content
Astro has built-in support for Markdown and MDX files, making it easy to write content-focused pages. You can use standard Markdown syntax, add frontmatter for metadata, and even use components within your content.

Markdown Pages

Create Markdown pages by adding .md files to your src/pages/ directory. Each file becomes a route based on its file path.
src/pages/blog/post.md

Accessing Frontmatter

In your layout, access frontmatter data through the frontmatter prop:
src/layouts/BlogPost.astro

MDX Support

MDX extends Markdown by allowing you to import and use components directly in your content. First, add the MDX integration:

Using Components in MDX

Once installed, you can use components in .mdx files:
src/pages/interactive.mdx
MDX is perfect for documentation, blogs, and any content that benefits from interactive components.

Markdown Features

Syntax Highlighting

Code blocks support syntax highlighting out of the box:
You can customize the theme in your astro.config.mjs:
astro.config.mjs

GitHub-Flavored Markdown

Astro supports GitHub-Flavored Markdown (GFM) including:
  • Tables
  • Task lists
  • Strikethrough
  • Autolinks

Custom Components

Replace HTML elements with custom components using the components prop in MDX:
src/pages/styled.mdx

Layouts for Markdown

Specify a layout in frontmatter to wrap your Markdown content:
src/pages/article.md
The layout receives all frontmatter properties plus the rendered content:
src/layouts/Article.astro

Programmatic Access

Import Markdown files in Astro components to access their content and frontmatter:
src/pages/index.astro

Dynamic Imports

Load Markdown files dynamically:
src/pages/blog/[slug].astro

Configuration Options

Customize Markdown processing in astro.config.mjs:
astro.config.mjs
Remark plugins process Markdown AST before HTML conversion:
Rehype plugins process HTML after Markdown conversion:

Best Practices

Content Collections

Use Content Collections for type-safe content management

Layouts

Create reusable layouts for consistent styling
For complex content sites, use Content Collections instead of accessing Markdown files directly. They provide type safety, validation, and better performance.

MDX-Specific Features

Export Variables

Export variables from MDX to use in your content:

JavaScript Expressions

Use JavaScript expressions inline:

Component Props

Pass props to components:

Common Use Cases

src/pages/blog/first-post.md

Content Collections

Type-safe content management

Layouts

Wrap content with layouts

Components

Build reusable components

Assets

Optimize images in Markdown