#Help with accessing frontmatter added with remark

2 messages · Page 1 of 1 (latest)

zenith drumBOT
#

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

#

It seems you're having issues with accessing the frontmatter after migrating to Astro content collections. In Astro v2.0, remark and rehype plugins have access to user frontmatter via frontmatter injection, which allows plugin authors to modify a user's existing frontmatter or compute new properties based on other properties (source (https://docs.astro.build/en/guides/upgrade-to/v2#changed-plugin-access-to-frontmatter)).

To access the modified frontmatter inside a content entry, you can use the remarkPluginFrontmatter property returned from render() (source (https://docs.astro.build/en/guides/content-collections#modifying-frontmatter-with-remark)):

---
import { getEntryBySlug } from 'astro:content';
const blogPost = await getEntryBySlug('blog', 'post-1');
const { remarkPluginFrontmatter } = await blogPost.render();
---
<!-- 
 This example assumes `readingTime` was injected by a remark plugin.
 See our "reading time" example for more details:
 https://docs.astro.build/en/guides/markdown-content/#example-calculate-reading-time
-->
<p>{blogPost.data.title} — {remarkPluginFrontmatter.readingTime}</p>

However, using this approach is not recommended, as it is handled separately from your type-safe schema and will not reflect any changes or defaults applied through Astro. Use it at your own risk (source (https://docs.astro.build/en/guides/content-collections#modifying-frontmatter-with-remark)).

If you still face issues, please double-check your remark plugin implementation and ensure it's correctly modifying the frontmatter as described in the example for calculating reading time (https://docs.astro.build/en/guides/markdown-content#example-calculate-reading-time).

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

Astro Documentation

Learn how to create content using Markdown or MDX with Astro

Astro Documentation

How to upgrade your project to the latest version of Astro.

Astro Documentation

Content collections help organize your Markdown and type-check your frontmatter with schemas.