#Markdoc nodes - get content collection entry
4 messages · Page 1 of 1 (latest)
Hi @hushed lodge. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, you can use the getEntry() or getEntryBySlug() functions to retrieve a single collection entry by collection name and either the entry id or slug. These functions are part of the astro:content API and can be used within an Astro component, including those used within a Markdoc file.
Here is an example of how you can use getEntryBySlug():
---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 />
In this example, getEntryBySlug() is used to retrieve a specific entry from the 'docs' collection. The entry.render() function is then called to get the Content component for use in the .astro file template.
However, there is no global context that you can access within Markdoc components used inside a content entry. If you need to pass variables to your content, you can do so as props via the Content component. For example:
---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} />
In this example, the abTestGroup parameter is passed as a variable to the Content component. This variable is then available within the docs/why-markdoc.mdoc file.
For more information, you can refer to the Astro Content Collection docs (https://docs.astro.build/en/guides/content-collections/) and the Astro API Reference (https://docs.astro.build/en/reference/api-reference#content-).
Sources:
- Astro Content Collection docs (https://docs.astro.build/en/guides/content-collections/)
- Astro API Reference (https://docs.astro.build/en/reference/api-reference#content-)
- Astro Markdoc Integration Guide (https://docs.astro.build/en/guides/integrations-guide/markdoc#usage)
- Astro Markdoc Variable Passing (https://docs.astro.build/en/guides/integrations-guide/markdoc#pass-markdoc-variables)