@astrojs/ markdoc
هذا المحتوى لا يتوفر بلغتك بعد.
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.
Installation
Section titled InstallationAstro 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.
If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.
Manual Install
Section titled Manual InstallFirst, install the @astrojs/markdoc
package:
Then, apply the integration to your astro.config.*
file using the integrations
property:
Editor Integration
Section titled Editor IntegrationVS 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.
Usage
Section titled UsageMarkdoc 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:
Markdoc config
Section titled Markdoc config@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 tagsYou 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:
This component can now be used in your Markdoc files with the {% aside %}
tag. Children will be passed to your component’s default slot:
Use Astro components from npm packages and TypeScript files
Section titled Use Astro components from npm packages and TypeScript filesYou 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:
This generates the following import statement internally:
Custom headings
Section titled Custom headings@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:
All Markdown headings will render the Heading.astro
component and pass the following attributes
as component props:
level: number
The heading level 1 - 6id: string
Anid
generated from the heading’s text contents. This corresponds to theslug
generated by the contentrender()
function.
For example, the heading ### Level 3 heading!
will pass level: 3
and id: 'level-3-heading'
as component props.
Syntax highlighting
Section titled Syntax highlighting@astrojs/markdoc
provides Shiki and Prism extensions to highlight your code blocks.
Shiki
Section titled ShikiApply the shiki()
extension to your Markdoc config using the extends
property. You can optionally pass a shiki configuration object:
Prism
Section titled PrismApply the prism()
extension to your Markdoc config using the extends
property.
Set the root HTML element
Section titled Set the root HTML elementMarkdoc 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:
Custom Markdoc nodes / elements
Section titled Custom Markdoc nodes / elementsYou 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:
Use client-side UI components
Section titled Use client-side UI componentsTags 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:
This Astro component can now be passed to the render
prop for any tag or node in your config:
Markdoc config
Section titled Markdoc configThe 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:
Now, you can call this function from any Markdoc content entry:
Markdoc Language Server
Section titled Markdoc Language ServerIf 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 into the project root with following content:
The schema
property contains all information to configure the language server for Astro content collections. It accepts following properties:
path
: The path to the configuration file.type
: The type of module your configuration file uses (esm
allowsimport
syntax).property
: The exported property name that contains the configuration object.watch
: Tell the server to watch for changes in the configuration.
The top-level path
property tells the server where content is located. Since Markdoc is specific to content collections, you can use src/content
.
Pass Markdoc variables
Section titled Pass Markdoc variablesYou 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:
Now, abTestGroup
is available as a variable in docs/why-markdoc.mdoc
:
To make a variable global to all Markdoc files, you can use the variables
attribute from your markdoc.config.mjs|ts
:
Access frontmatter from your Markdoc content
Section titled Access frontmatter from your Markdoc contentTo access frontmatter, you can pass the entry data
property as a variable where you render your content:
This can now be accessed as $frontmatter
in your Markdoc.
Integration config options
Section titled Integration config optionsThe Astro Markdoc integration handles configuring Markdoc options and capabilities that are not available through the markdoc.config.js
file.
allowHTML
Section titled allowHTMLEnables 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.
When allowHTML
is enabled, HTML markup inside Markdoc documents will be rendered as actual HTML elements (including <script>
), making attack vectors like XSS possible. Ensure that any HTML markup comes from trusted sources.
ignoreIndentation
Section titled ignoreIndentationBy 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.
Examples
Section titled Examples- The Astro Markdoc starter template shows how to use Markdoc files in your Astro project.