Markdown & MDX
Markdown is commonly used to author text-heavy content like blog posts and documentation. Astro includes built-in support for standard Markdown files.
With the @astrojs/mdx integration installed, Astro also supports MDX (.mdx
) files which bring added features like support for JavaScript expressions and components in your Markdown content.
Use either or both types of files to write your Markdown content!
Markdown and MDX Pages
Section titled Markdown and MDX PagesFile-based Routing
Section titled File-based RoutingAstro treats any .md
(or alternative supported extension) or .mdx
file inside of the /src/pages/
directory as a page.
Placing a file in this directory, or any sub-directory, will automatically build a page route using the pathname of the file.
📚 Read more about Astro’s file-based routing or options for creating dynamic routes.
Draft Pages
Section titled Draft Pagesdraft: true
is an optional frontmatter value that will mark an individual Markdown or MDX page or post as “unpublished.” By default, this page will be:
- excluded from the site build (no page will be built)
- returned by
Astro.glob()
(visible in lists of posts)
To exclude draft posts from being included in a post archive, or list of most recent posts, you can filter the results returned by Astro.glob()
:
Enable building draft pages
Section titled Enable building draft pagesTo enable building draft pages by default, update astro.config.mjs
by adding drafts: true
to markdown
or to the mdx
integration:
Markdown Features
Section titled Markdown FeaturesAstro provides some extra, built-in Markdown features available when using Markdown and MDX files.
Frontmatter layout
Section titled Frontmatter layoutAstro provides Markdown and MDX pages with a special frontmatter layout
property that can specify a relative path (or alias) to an Astro layout component.
Frontmatter properties are then available to the layout through Astro.props
:
📚 Learn more about Markdown Layouts.
Heading IDs
Section titled Heading IDsUsing headings in Markdown and MDX will automatically give you anchor links so you can link directly to certain sections of your page.
Escaping special characters
Section titled Escaping special charactersCertain characters have a special meaning in Markdown and MDX. You may need to use a different syntax if you want to display them. To do this, you can use HTML entities for these characters instead.
For example, to prevent <
being interpreted as the beginning of an HTML element, write <
. Or, to prevent {
being interpreted as the beginning of a JavaScript expression in MDX, write {
.
MDX-only Features
Section titled MDX-only FeaturesAdding the Astro MDX integration enhances your Markdown authoring with JSX variables, expressions and components.
It also adds extra features to standard MDX, including support for Markdown-style frontmatter in MDX. This allows you to use most of Astro’s built-in Markdown features like a frontmatter layout
property and a setting for draft pages.
.mdx
files must be written in MDX syntax rather than Astro’s HTML-like syntax.
Using Exported Variables in MDX
Section titled Using Exported Variables in MDXMDX supports using export
statements to add variables to your MDX content. These variables are accessible both in the template itself and as named properties when importing the the file somewhere else.
For example, you can export a title
field from an MDX page or component to use as a heading with {JSX expressions}
:
Using Frontmatter Variables in MDX
Section titled Using Frontmatter Variables in MDXThe Astro MDX integration includes support for using frontmatter in MDX by default. Add frontmatter properties just as you would in Markdown files, and these variables are accessible to use in the template, in its layout
component, and as named properties when importing the the file somewhere else.
Using Components in MDX
Section titled Using Components in MDXAfter installing the MDX integration, you can import and use both Astro components and UI framework components in MDX (.mdx
) files just as you would use them in any other Astro component.
Don’t forget to include a client:directive
on your UI framework components, if necessary!
See more examples of using import and export statements in the MDX docs.
Assigning Custom Components to HTML elements
Section titled Assigning Custom Components to HTML elementsWith MDX, you can map Markdown syntax to custom components instead of their standard HTML elements. This allows you to write in standard Markdown syntax, but apply special component styling to selected elements.
Import your custom component into your .mdx
file, then export a components
object that maps the standard HTML element to your custom component:
Visit the MDX website for a full list of HTML elements that can be overwritten as custom components.
Importing Markdown
Section titled Importing MarkdownYou can import Markdown and MDX files directly into your Astro files. This gives you access to their Markdown content, as well as other properties such as frontmatter values that can be used within Astro’s JSX-like expressions.
You can import one specific page with an import
statement, or multiple pages with Astro.glob()
.
When you import Markdown and MDX files in an Astro component, you get an object containing their exported properties.
In MDX files, you can access properties from both frontmatter and export
statements:
You can optionally provide a type for the frontmatter
variable using a TypeScript generic:
Exported Properties
Section titled Exported PropertiesThe following properties are available to a .astro
component when using an import
statement or Astro.glob()
:
file
- The absolute file path (e.g./home/user/projects/.../file.md
).url
- If it’s a page, the URL of the page (e.g./en/guides/markdown-content
).frontmatter
- Contains any data specified in the file’s YAML frontmatter.getHeadings
- An async function that returns an array of all headings (i.e.h1 -> h6
elements) in the file. Each heading’sslug
corresponds to the generated ID for a given heading and can be used for anchor links. This list follows the type:{ depth: number; slug: string; text: string }[]
.Content
- A component that returns the full, rendered contents of the file.- (Markdown only)
rawContent()
- A function that returns the raw Markdown document as a string. - (Markdown only)
compiledContent()
- A function that returns the Markdown document compiled to an HTML string. Note this does not include layouts configured in your frontmatter! Only the markdown document itself will be returned as HTML. - (MDX only) - MDX files can also export data with an
export
statement.
The Content
Component
Section titled The Content ComponentImport Content
to render a component that returns the full rendered contents of a Markdown or MDX file:
Example: Dynamic page routing
Section titled Example: Dynamic page routingInstead of putting your Markdown/MDX files in the src/pages/
directory to create page routes, you can generate pages dynamically.
To access your Markdown content, pass the <Content/>
component through the Astro page’s props
. You can then retrieve the component from Astro.props
and render it in your page template.
MDX-only Exports
Section titled MDX-only ExportsMDX files can also export data with an export
statement.
For example, you can export a title
field from an MDX page or component.
This title
will be accessible from import
and Astro.glob() statements:
Custom components with imported MDX
Section titled Custom components with imported MDXWhen rendering imported MDX content, custom components can be passed via the components
prop.
Configuring Markdown and MDX
Section titled Configuring Markdown and MDXMarkdown support in Astro is powered by remark, a powerful parsing and processing tool with an active ecosystem. Other Markdown parsers like Pandoc and markdown-it are not currently supported.
Astro applies the GitHub-flavored Markdown and Smartypants plugins by default. This brings some niceties like generating clickable links from text and formatting quotes for readability.
You can customize how remark parses your Markdown in astro.config.mjs
. See the full list of Markdown configuration options.
Markdown Plugins
Section titled Markdown PluginsAstro supports adding third-party remark and rehype plugins for Markdown and MDX. These plugins allow you to extend your Markdown with new capabilities, like auto-generating a table of contents, applying accessible emoji labels, and more.
We encourage you to browse awesome-remark and awesome-rehype for popular plugins! See each plugin’s own README for specific installation instructions.
By default, Astro’s MDX integration inherits all remark and rehype plugins from your Astro config markdown
options. To change this behavior, configure extendPlugins
in your mdx
integration.
Any additional plugins you apply in your MDX config will be applied after your configured Markdown plugins, and will apply only to .mdx
files.
This example applies remark-toc
to Markdown and MDX, and rehype-accessible-emojis
to MDX only, while preserving Astro’s default plugins:
Example: Injecting frontmatter
Section titled Example: Injecting frontmatterYou can add frontmatter properties to all of your Markdown and MDX files by using a remark or rehype plugin.
-
Append a
customProperty
to thedata.astro.frontmatter
property from your plugin’sfile
argument: -
Apply this plugin to your
markdown
ormdx
integration config:or
Now, every Markdown or MDX file will have customProperty
in its frontmatter, making it available when importing your markdown and from the Astro.props.frontmatter
property in your layouts.
Example: calculate reading time
Section titled Example: calculate reading timeYou can use a remark plugin to add a reading time to your frontmatter. We recommend two helper packages:
reading-time
to calculate minutes readmdast-util-to-string
to extract all text from your markdown
We can apply these packages to a remark plugin like so:
Once you apply this plugin to your config:
…all Markdown documents will have a calculated minutesRead
. You can use this to include an “X min read” banner in a markdown layout, for instance:
Syntax Highlighting
Section titled Syntax HighlightingAstro comes with built-in support for Shiki and Prism. This provides syntax highlighting for:
- all code fences (```) used in a Markdown or MDX file.
- content within the built-in
<Code />
component (powered by Shiki). - content within the
<Prism />
component (powered by Prism).
Shiki is enabled by default, preconfigured with the github-dark
theme. The compiled output will be limited to inline style
s without any extraneous CSS classes, stylesheets, or client-side JS.
Shiki configuration
Section titled Shiki configurationShiki is our default syntax highlighter. You can configure all options via the shikiConfig
object like so:
Adding your own theme
Section titled Adding your own themeInstead of using one of Shiki’s predefined themes, you can import a custom theme from a local file.
We also suggest reading Shiki’s own theme documentation to explore more about themes, light vs dark mode toggles, or styling via CSS variables.
Change Default Syntax Highlighting Mode
Section titled Change Default Syntax Highlighting ModeIf you’d like to switch to 'prism'
by default, or disable syntax highlighting entirely, you can use the markdown.syntaxHighlighting
config object:
Prism configuration
Section titled Prism configurationIf you opt to use Prism, Astro will apply Prism’s CSS classes instead. Note that you need to bring your own CSS stylesheet for syntax highlighting to appear!
- Choose a premade stylesheet from the available Prism Themes.
- Add this stylesheet to your project’s
public/
directory. - Load this into your page’s
<head>
in a layout component via a<link>
tag. (See Prism basic usage.)
You can also visit the list of languages supported by Prism for options and usage.
Fetching Remote Markdown
Section titled Fetching Remote MarkdownAstro was primarily designed for local Markdown files that could be saved inside of your project directory. However, there may be certain cases where you need to fetch Markdown from a remote source. For example, you may need to fetch and render Markdown from a remote API when you build your website (or when a user makes a request to your website, when using SSR).
Astro does not include built-in support for remote Markdown! To fetch remote Markdown and render it to HTML, you will need to install and configure your own Markdown parser from npm. This will not inherit from any of Astro’s built-in Markdown and MDX settings that you have configured. Be sure that you understand these limitations before implementing this in your project.