This Astro integration enables the usage of Markdoc to create components, pages, and content collection entries.
Why Markdoc?Section titled Why Markdoc?
Markdoc allows you to enhance your Markdown with Astro components. If you have existing content authored in Markdoc, this integration allows you to bring those files to your Astro project using content collections.
InstallationSection titled Installation
Quick InstallSection titled Quick Install
astro add command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren’t sure which package manager you’re using, run the first command.) Then, follow the prompts, and type “y” in the terminal (meaning “yes”) for each one.
If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.
Manual InstallSection titled Manual Install
First, install the
@astrojs/markdoc package using your package manager. If you’re using npm or aren’t sure, run this in the terminal:
Then, apply this integration to your
astro.config.* file using the
Editor IntegrationSection titled Editor Integration
VS Code supports Markdown by default. However, for Markdoc editor support, you may wish to add the following setting in your VSCode config. This ensures authoring Markdoc files provides a Markdown-like editor experience.
UsageSection titled Usage
Markdoc files can only be used within content collections. Add entries to any content collection using the
Then, query your collection using the Content Collection APIs:
📚 See the Astro Content Collection docs for more information.
ConfigurationSection titled Configuration
@astrojs/markdoc offers configuration options to use all of Markdoc’s features and connect UI components to your content.
Using componentsSection titled Using components
You can add Astro and UI framework components (React, Vue, Svelte, etc.) to your Markdoc using both Markdoc tags and HTML element nodes.
Render Markdoc tags as Astro componentsSection titled Render Markdoc tags as Astro components
You may configure Markdoc tags that map to components. You can configure a new tag from your
astro.config using the
Then, you can wire this render name (
'Aside') to a component from the
components prop via the
<Content /> component. Note the object key name (
Aside in this case) should match the render name:
Render Markdoc nodes / HTML elements as Astro componentsSection titled Render Markdoc nodes / HTML elements as Astro components
You may also want to map standard HTML elements like headings and paragraphs to components. For this, you can configure a custom Markdoc node. This example overrides Markdoc’s
heading node to render a
Heading component, passing the built-in
level attribute as a prop:
Now, you can map the string passed to render (
'Heading' in this example) to a component import. This is configured from the
<Content /> component used to render your Markdoc using the
Now, all Markdown headings will render with the
Heading.astro component. This example uses a level 3 heading, automatically passing
level: 3 as the component prop:
📚 Find all of Markdoc’s built-in nodes and node attributes on their documentation.
Use client-side UI componentsSection titled Use client-side UI components
components prop does not support the
client: directive for hydrating components. To embed client-side components, create a wrapper
.astro file to import your component and apply a
client: directive manually.
This example wraps a
Aside.tsx component with a
This component can be applied via the
Access frontmatter and content collection information from your templatesSection titled Access frontmatter and content collection information from your templates
You can access content collection information from your Markdoc templates using the
$entry variable. This includes the entry
collection name, and frontmatter
data parsed by your content collection schema (if any). This example renders the
title frontmatter property as a heading:
$entry object matches the
CollectionEntry type, excluding the
Markdoc configSection titled Markdoc config
The Markdoc integration accepts all Markdoc configuration options, including tags and functions.
You can pass these options from the
markdoc() integration in your
astro.config. This example adds a global
Now, you can call this function from any Markdoc content entry:
📚 See the Markdoc documentation for more on using variables or functions in your content.
Define Markdoc configuration at runtimeSection titled Define Markdoc configuration at runtime
You may need to define Markdoc configuration at the component level, rather than the
astro.config.mjs level. This is useful when mapping props and SSR parameters to Markdoc variables.
Astro recommends running the Markdoc transform step manually. This allows you to define your configuration and call Markdoc’s rendering functions in a
.astro file directly, ignoring any Markdoc config in your
You will need to install the
@markdoc/markdoc package into your project first:
Now, you can define Markdoc configuration options using
This example defines an
abTestGroup Markdoc variable based on an SSR param, transforming the raw entry
body. The result is rendered using the
Renderer component provided by
ExamplesSection titled Examples
- The Astro Markdoc starter template shows how to use Markdoc files in your Astro project.
TroubleshootingSection titled Troubleshooting
For help, check out the
#support channel on Discord. Our friendly Support Squad members are here to help!
You can also check our Astro Integration Documentation for more on integrations.
ContributingSection titled Contributing
This package is maintained by Astro’s Core team. You’re welcome to submit an issue or PR!
ChangelogSection titled Changelog
See CHANGELOG.md for a history of changes to this integration.