#How to grab content from content collection .md file

3 messages · Page 1 of 1 (latest)

oak ploverBOT
#

Hi @ancient matrix. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To grab content from a content collection .md file in Astro, you can use the getEntryBySlug function from the astro:content module. This function allows you to query your content collection and retrieve a specific entry based on its slug.

Here's an example of how you can use it:

---
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 an entry from the docs collection with the slug why-markdoc. The entry.render() function is then used to render the content of the entry, which can be accessed through the Content component.

Please note that the <slot /> component in Astro is used to define where child components should be inserted within a parent component, and it may not be suitable for retrieving content from a markdown file in a content collection.

For more information, you can refer to the Astro Content Collection docs (https://docs.astro.build/en/guides/content-collections/).

Source: Astro Documentation (https://docs.astro.build/en/guides/integrations-guide/markdoc#usage)

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@ancient matrix kapa.ai is still learning and improving, please let us know how it did by reacting below