.. | ||
components | ||
src | ||
template | ||
test | ||
CHANGELOG.md | ||
package.json | ||
README.md | ||
tsconfig.json |
@astrojs/markdoc (experimental) 📝
This Astro integration enables the usage of Markdoc to create components, pages, and content collection entries.
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.
Installation
Quick Install
The 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.
# Using NPM
npx astro add markdoc
# Using Yarn
yarn astro add markdoc
# Using PNPM
pnpm astro add markdoc
If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.
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:
npm install @astrojs/markdoc
Then, apply this integration to your astro.config.*
file using the integrations
property:
astro.config.mjs
import { defineConfig } from 'astro/config';
import markdoc from '@astrojs/markdoc';
export default defineConfig({
// ...
integrations: [markdoc()],
});
Usage
Markdoc files can only be used within content collections. Add entries to any content collection using the .mdoc
extension:
src/content/docs/
why-markdoc.mdoc
quick-start.mdoc
Then, query your collection using the Content Collection APIs:
---
import { getEntryBySlug } from 'astro:content';
const entry = await getEntryBySlug('docs', 'why-markdoc');
const { Content } = await entry.render();
---
<!--Access frontmatter properties with `data`-->
<h1>{entry.data.title}</h1>
<!--Render Markdoc contents with the Content component-->
<Content />
📚 See the Astro Content Collection docs for more information.
Configuration
Once the Markdoc integration is installed, no configuration is necessary to use .mdoc
files in your Content Collections.
Markdoc config
The Markdoc integration accepts all Markdoc configuration options, including tags and variables.
You can pass these options from the markdoc()
integration in your astro.config
. This example declares a version
variable and an aside
tag for use across all Markdoc Content Collection entries:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import markdoc from '@astrojs/markdoc';
// https://astro.build/config
export default defineConfig({
integrations: [
markdoc({
variables: {
version: '0.0.1',
},
tags: {
aside: {
// See "Content `components` prop section
// for more on rendering components via tags
render: 'Aside',
attributes: {
type: { type: String },
title: { type: String },
},
},
},
}),
],
});
:::note These options will be applied during the Markdoc "transform" phase. This is run at build time (rather than server request time) both for static and SSR Astro projects. ::
Content components
prop
The Content
component accepts a components
prop, which defines mappings from Markdoc tags and HTML element names to Astro or UI framework components (React, Vue, Svelte, etc).
Render HTML elements as Astro components
You may want to map standard HTML elements like headings and paragraphs to components. This example renders all h1
headings using a Title
component:
---
import { getEntryBySlug } from 'astro:content';
import Title from '../components/Title.astro';
const entry = await getEntryBySlug('docs', 'why-markdoc');
const { Content } = await entry.render();
---
<Content
components={{
h1: Title,
}}
/>
Render Markdoc tags as Astro components
You may also configure Markdoc tags that map to components. You can configure a new tag from your astro.config
like so, passiing the uppercase component name as the render
prop:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import markdoc from '@astrojs/markdoc';
// https://astro.build/config
export default defineConfig({
integrations: [
markdoc({
tags: {
aside: {
render: 'Aside',
},
},
}),
],
});
Then, you can wire this render name ('Aside'
) to a component from the components
prop:
---
import { getEntryBySlug } from 'astro:content';
import Aside from '../components/Aside.astro';
const entry = await getEntryBySlug('docs', 'why-markdoc');
const { Content } = await entry.render();
---
<Content
components={{
Aside: Aside,
}}
/>
Use client-side UI components
Today, the 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 ClientAside.astro
wrapper:
---
// src/components/ClientAside.astro
import Aside from './Aside';
---
<Aside client:load />
This component can be applied via the components
prop:
---
// src/pages/why-markdoc.astro
import { getEntryBySlug } from 'astro:content';
import ClientAside from '../components/ClientAside.astro';
const entry = await getEntryBySlug('docs', 'why-markdoc');
const { Content } = await entry.render();
---
<Content
components={{
aside: ClientAside,
}}
/>
Examples
- The Astro Markdoc starter template shows how to use Markdoc files in your Astro project.
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.
Contributing
This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
Changelog
See CHANGELOG.md for a history of changes to this integration.