Salta ai contenuti

@astrojs/ markdoc

Questi contenuti non sono ancora disponibili nella tua lingua.

This Astro integration enables the usage of Markdoc to create components, pages, and content collection entries.

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.

Astro includes an astro add command to automate the setup of official integrations. If you prefer, you can install integrations manually instead.

Run one of the following commands in a new terminal window.

Terminal window
npx 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.

First, install the @astrojs/markdoc package:

Terminal window
npm install @astrojs/markdoc

Then, apply the 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()],
});

If you are using VS Code, there is an official Markdoc language extension that includes syntax highlighting and autocomplete for configured tags. See the language server on GitHub for more information.

To set up the extension, create a markdoc.config.json file in the project root with following content:

markdoc.config.json
[
{
"id": "my-site",
"path": "src/content",
"schema": {
"path": "markdoc.config.mjs",
"type": "esm",
"property": "default",
"watch": true
}
}
]

Set markdoc.config.mjs as your configuration file with the schema object, and define where your Markdoc files are stored using the path property. Since Markdoc is specific to content collections, you can use src/content.

Markdoc files can only be used within content collections. Add entries to any content collection using the .mdoc extension:

  • Directorysrc/
    • Directorycontent/
      • Directorydocs/
        • why-markdoc.mdoc
        • quick-start.mdoc

Then, query your collection using the Content Collection APIs:

src/pages/why-markdoc.astro
---
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.

You may need to pass variables to your content. This is useful when passing SSR parameters like A/B tests.

Variables can be passed as props via the Content component:

src/pages/why-markdoc.astro
---
import { getEntryBySlug } from 'astro:content';
const entry = await getEntryBySlug('docs', 'why-markdoc');
const { Content } = await entry.render();
---
<!--Pass the `abTest` param as a variable-->
<Content abTestGroup={Astro.params.abTestGroup} />

Now, abTestGroup is available as a variable in docs/why-markdoc.mdoc:

src/content/docs/why-markdoc.mdoc
{% if $abTestGroup === 'image-optimization-lover' %}
Let me tell you about image optimization...
{% /if %}

To make a variable global to all Markdoc files, you can use the variables attribute from your markdoc.config.mjs|ts:

markdoc.config.mjs
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
variables: {
environment: process.env.IS_PROD ? 'prod' : 'dev',
},
});

Access frontmatter from your Markdoc content

Section titled Access frontmatter from your Markdoc content

To access frontmatter, you can pass the entry data property as a variable where you render your content:

src/pages/why-markdoc.astro
---
import { getEntry } from 'astro:content';
const entry = await getEntry('docs', 'why-markdoc');
const { Content } = await entry.render();
---
<Content frontmatter={entry.data} />

This can now be accessed as $frontmatter in your Markdoc.

@astrojs/markdoc offers configuration options to use all of Markdoc’s features and connect UI components to your content.

Use Astro components as Markdoc tags

Section titled Use Astro components as Markdoc tags

You can configure Markdoc tags that map to .astro components. You can add a new tag by creating a markdoc.config.mjs|ts file at the root of your project and configuring the tag attribute.

This example renders an Aside component, and allows a type prop to be passed as a string:

markdoc.config.mjs
import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
tags: {
aside: {
render: component('./src/components/Aside.astro'),
attributes: {
// Markdoc requires type defs for each attribute.
// These should mirror the `Props` type of the component
// you are rendering.
// See Markdoc's documentation on defining attributes
// https://markdoc.dev/docs/attributes#defining-attributes
type: { type: String },
},
},
},
});

This component can now be used in your Markdoc files with the {% aside %} tag. Children will be passed to your component’s default slot:

# Welcome to Markdoc 👋
{% aside type="tip" %}
Use tags like this fancy "aside" to add some _flair_ to your docs.
{% /aside %}

Tags and nodes are restricted to .astro files. To embed client-side UI components in Markdoc, use a wrapper .astro component that renders a framework component with your desired client: directive.

This example wraps a React Aside.tsx component with a ClientAside.astro component:

src/components/ClientAside.astro
---
import Aside from './Aside';
---
<Aside {...Astro.props} client:load />

This Astro component can now be passed to the render prop for any tag or node in your config:

markdoc.config.mjs
import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
tags: {
aside: {
render: component('./src/components/ClientAside.astro'),
attributes: {
type: { type: String },
},
},
},
});

Use Astro components from npm packages and TypeScript files

Section titled Use Astro components from npm packages and TypeScript files

You may need to use Astro components exposed as named exports from TypeScript or JavaScript files. This is common when using npm packages and design systems.

You can pass the import name as the second argument to the component() function:

markdoc.config.mjs
import { defineMarkdocConfig, component } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
tags: {
tabs: {
render: component('@astrojs/starlight/components', 'Tabs'),
},
},
});

This generates the following import statement internally:

import { Tabs } from '@astrojs/starlight/components';

The {% partial %} tag allows you to render other .mdoc files inside your Markdoc content.

This is useful for reusing content across multiple documents, and allows you to have .mdoc content files that do not follow your collection schema.

This example shows a Markdoc partial for a footer to be used inside blog collection entries:

src/content/blog/_footer.mdoc
Social links:
- [Twitter / X](https://twitter.com/astrodotbuild)
- [Discord](https://astro.build/chat)
- [GitHub](https://github.com/withastro/astro)

Use the {% partial %} tag with to render the footer at the bottom of a blog post entry. Apply the file attribute with the path to the file, using either a relative path or an import alias:

src/content/blog/post.mdoc
# My Blog Post
{% partial file="./_footer.mdoc" %}

@astrojs/markdoc provides Shiki and Prism extensions to highlight your code blocks.

Apply the shiki() extension to your Markdoc config using the extends property. You can optionally pass a shiki configuration object:

markdoc.config.mjs
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
import shiki from '@astrojs/markdoc/shiki';
export default defineMarkdocConfig({
extends: [
shiki({
// Choose from Shiki's built-in themes (or add your own)
// Default: 'github-dark'
// https://shiki.style/themes
theme: 'dracula',
// Enable word wrap to prevent horizontal scrolling
// Default: false
wrap: true,
// Pass custom languages
// Note: Shiki has countless langs built-in, including `.astro`!
// https://shiki.style/languages
langs: [],
}),
],
});

Apply the prism() extension to your Markdoc config using the extends property.

markdoc.config.mjs
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
import prism from '@astrojs/markdoc/prism';
export default defineMarkdocConfig({
extends: [prism()],
});
To learn about configuring Prism stylesheets, see our syntax highlighting guide.

Custom Markdoc nodes / elements

Section titled Custom Markdoc nodes / elements

You may want to render standard Markdown elements, such as paragraphs and bolded text, as Astro components. For this, you can configure a Markdoc node. If a given node receives attributes, they will be available as component props.

This example renders blockquotes with a custom Quote.astro component:

markdoc.config.mjs
import { defineMarkdocConfig, nodes, component } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
nodes: {
blockquote: {
...nodes.blockquote, // Apply Markdoc's defaults for other options
render: component('./src/components/Quote.astro'),
},
},
});
See the Markdoc nodes documentation to learn about all the built-in nodes and attributes.

@astrojs/markdoc automatically adds anchor links to your headings, and generates a list of headings via the content collections API. To further customize how headings are rendered, you can apply an Astro component as a Markdoc node.

This example renders a Heading.astro component using the render property:

markdoc.config.mjs
import { defineMarkdocConfig, nodes, component } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
nodes: {
heading: {
...nodes.heading, // Preserve default anchor link generation
render: component('./src/components/Heading.astro'),
},
},
});

All Markdown headings will render the Heading.astro component and pass the following attributes as component props:

  • level: number The heading level 1 - 6
  • id: string An id generated from the heading’s text contents. This corresponds to the slug generated by the content render() function.

For example, the heading ### Level 3 heading! will pass level: 3 and id: 'level-3-heading' as component props.

Astro’s <Image /> component cannot be used directly in Markdoc. However, you can configure an Astro component to override the default image node every time the native ![]() image syntax is used, or as a custom Markdoc tag to allow you to specify additional image attributes.

Override Markdoc’s default image node

Section titled Override Markdoc’s default image node

To override the default image node, you can configure an .astro component to be rendered in place of a standard <img>.

  1. Build a custom MarkdocImage.astro component to pass the required src and alt properties from your image to the <Image /> component:

    src/components/MarkdocImage.astro
    ---
    import { Image } from "astro:assets";
    interface Props {
    src: ImageMetadata;
    alt: string;
    }
    const { src, alt } = Astro.props;
    ---
    <Image src={src} alt={alt} />
  2. The <Image /> component requires a width and height for remote images which cannot be provided using the ![]() syntax. To avoid errors when using remote images, update your component to render a standard HTML <img> tag when a remote URL src is found:

    src/components/MarkdocImage.astro
    ---
    import { Image } from "astro:assets";
    interface Props {
    src: ImageMetadata | string;
    alt: string;
    }
    const { src, alt } = Astro.props;
    ---
    <Image src={src} alt={alt} />
    {
    typeof src === 'string' ? <img src={src} alt={alt} /> : <Image src={src} alt={alt} />
    }
  3. Configure Markdoc to override the default image node and render MarkdocImage.astro:

    markdoc.config.mjs
    import { defineMarkdocConfig, nodes, component } from '@astrojs/markdoc/config';
    export default defineMarkdocConfig({
    nodes: {
    image: {
    ...nodes.image, // Apply Markdoc's defaults for other options
    render: component('./src/components/MarkdocImage.astro'),
    },
    },
    });
  4. The native image syntax in any .mdoc file will now use the <Image /> component to optimize your local images. Remote images may still be used, but will not be rendered by Astro’s <Image /> component.

    src/content/blog/post.mdoc
    <!-- Optimized by <Image /> -->
    ![A picture of a cat](/cat.jpg)
    <!-- Unoptimized <img> -->
    ![A picture of a dog](https://example.com/dog.jpg)

Create a custom Markdoc image tag

Section titled Create a custom Markdoc image tag

A Markdoc image tag allows you to set additional attributes on your image that are not possible with the ![]() syntax. For example, custom image tags allow you to use Astro’s <Image /> component for remote images that require a width and height.

The following steps will create a custom Markdoc image tag to display a <figure> element with a caption, using the Astro <Image /> component to optimize the image.

  1. Create a MarkdocFigure.astro component to receive the necessary props and render an image with a caption:

    src/components/MarkdocFigure.astro
    ---
    // src/components/MarkdocFigure.astro
    import { Image } from "astro:assets";
    interface Props {
    src: ImageMetadata | string;
    alt: string;
    width: number;
    height: number;
    caption: string;
    }
    const { src, alt, width, height, caption } = Astro.props;
    ---
    <figure>
    <Image {src} {alt} {width} {height} />
    {caption && <figcaption>{caption}</figcaption>}
    </figure>
  2. Configure your custom image tag to render your Astro component:

    markdoc.config.mjs
    import { component, defineMarkdocConfig, nodes } from '@astrojs/markdoc/config';
    export default defineMarkdocConfig({
    tags: {
    image: {
    attributes: nodes.image.attributes,
    render: component('./src/components/MarkdocFigure.astro'),
    },
    },
    });
  3. Use the image tag in Markdoc files to display a figure with caption, providing all the necessary attributes for your component:

    {% image src="./astro-logo.png" alt="Astro Logo" width="100" height="100" caption="a caption!" %}

Advanced Markdoc configuration

Section titled Advanced Markdoc configuration

The markdoc.config.mjs|ts file accepts all Markdoc configuration options, including tags and functions.

You can pass these options from the default export in your markdoc.config.mjs|ts file:

markdoc.config.mjs
import { defineMarkdocConfig } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
functions: {
getCountryEmoji: {
transform(parameters) {
const [country] = Object.values(parameters);
const countryToEmojiMap = {
japan: '🇯🇵',
spain: '🇪🇸',
france: '🇫🇷',
};
return countryToEmojiMap[country] ?? '🏳';
},
},
},
});

Now, you can call this function from any Markdoc content entry:

¡Hola {% getCountryEmoji("spain") %}!
See the Markdoc documentation for more on using variables or functions in your content.

Markdoc wraps documents with an <article> tag by default. This can be changed from the document Markdoc node. This accepts an HTML element name or null if you prefer to remove the wrapper element:

markdoc.config.mjs
import { defineMarkdocConfig, nodes } from '@astrojs/markdoc/config';
export default defineMarkdocConfig({
nodes: {
document: {
...nodes.document, // Apply defaults for other options
render: null, // default 'article'
},
},
});

The Astro Markdoc integration handles configuring Markdoc options and capabilities that are not available through the markdoc.config.js file.

Enables writing HTML markup alongside Markdoc tags and nodes.

By default, Markdoc will not recognize HTML markup as semantic content.

To achieve a more Markdown-like experience, where HTML elements can be included alongside your content, set allowHTML:true as a markdoc integration option. This will enable HTML parsing in Markdoc markup.

astro.config.mjs
import { defineConfig } from 'astro/config';
import markdoc from '@astrojs/markdoc';
export default defineConfig({
// ...
integrations: [markdoc({ allowHTML: true })],
});

By default, any content that is indented by four spaces is treated as a code block. Unfortunately, this behavior makes it difficult to use arbitrary levels of indentation to improve the readability of documents with complex structure.

When using nested tags in Markdoc, it can be helpful to indent the content inside of tags so that the level of depth is clear. To support arbitrary indentation, we have to disable the indent-based code blocks and modify several other markdown-it parsing rules that account for indent-based code blocks. These changes can be applied by enabling the ignoreIndentation option.

astro.config.mjs
import { defineConfig } from 'astro/config';
import markdoc from '@astrojs/markdoc';
export default defineConfig({
// ...
integrations: [markdoc({ ignoreIndentation: true })],
});
# Welcome to Markdoc with indented tags 👋
# Note: Can use either spaces or tabs for indentation
{% custom-tag %}
{% custom-tag %} ### Tags can be indented for better readability
{% another-custom-tag %}
This is easier to follow when there is a lot of nesting
{% /another-custom-tag %}
{% /custom-tag %}
{% /custom-tag %}

Più integrazioni

Framework UI

Adattatori SSR

Altre Integrazioni

Contribuisci

A cosa stai pensando?

Crea una Issue su GitHub

Il modo più rapido per segnalare un problema al nostro team.

Comunità