#Astro.glob() not working as expected

3 messages · Page 1 of 1 (latest)

quick bridge
#

I'm fetching my Markdown file as per the docs:

const allPosts = await Astro.glob('./posts/*.md');

However when I JSON.stringify() the result, it doesn't contain the information I was expcting:

{"images":{},"frontmatter":{},"file":"/home/user/folder/src/pages/posts/my-article.md","url":"/posts/my-article"}

This is from logging out the result of allPosts.map(post => JSON.stringify(post))

I've been using Astro for ages, but never with Markdown before!

wanton archBOT
#

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

#

Astro provides built-in support for Markdown files and allows you to access their content and other properties such as frontmatter values. When you import Markdown files in an Astro component, you get an object containing their exported properties.

From the information you provided, it seems like the frontmatter in your Markdown file might be empty or not properly formatted. The frontmatter is a section at the top of the file that is used to store metadata about the file. It should be written in YAML and enclosed by triple-dashed lines. Here's an example:

---
title: 'The greatest post of all time'
author: 'Ben'
---
Here is my _great_ post!

In this example, title and author are frontmatter properties. You can access these properties in your Astro file like this:

<p>{greatPost.frontmatter.title}</p>
<p>Written by: {greatPost.frontmatter.author}</p>

If your Markdown file's frontmatter is correctly formatted but you're still not seeing the expected properties, it might be a good idea to double-check your file path in the Astro.glob() function. Make sure it correctly points to your Markdown files.

For more information, you can refer to the Astro documentation on Markdown content (https://docs.astro.build/en/guides/markdown-content/).

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