OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

Render your Astro markdown content for your overviews

Astro Markdown
post

This post is over a year old, some of this information may be out of date.

I wanted to create a roll-up of the latest news articles on our new BIWUG community site (which still needs to be released).

Show image BIWUG News Rollup
BIWUG News Rollup

When I retrieved the news collection data, I only got the front matter data and the markdown content.

Looking at the Astro documentation, I found several ways to get the HTML. The first one was to use the marked dependency, although I found an easier and better way to utilize the Astro.glob() functionality.

The Astro.glob() function is a way to retrieve files from a directory. It takes a glob pattern as its input, and it returns an array of the files that match the pattern.

For example, the following code would retrieve all of the news articles in the content/news directory:

const allNewsPosts = await Astro.glob(../content/news/*.md);

The nice part about the Astro.glob() function is that it returns the front matter data and the Astro Content component. This means that you can use the Astro Content component to render the HTML.

On the BIWUG website, I used it as follows:

---
const allNewsPosts = await Astro.glob(`../content/news/*.md`)
const newsPosts = allNewsPosts.map(n => ({
title: n.frontmatter.title,
Content: n.Content
}));
---
<section class="news__section mt-16">
<h2>Stay Up to Date</h2>
<h3>Latest News and Exciting Events</h3>
<div class="mx-auto mt-8 grid max-w-max grid-cols-1 place-content-center gap-x-16 gap-y-12 md:grid-cols-2">
{
newsPosts.map(({ title, Content }) => (
<article class="bg-white space-y-4 p-8 shadow-md rounded-md border border-gray-200">
<h2>{title}</h2>
<Content />
</article>
))
}
</ul>
</section>

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code