Astro Content

Astro Content Collections

ComeOnOliver Updated 61 repo stars

File contents

Astro Content Collections

Define Collection

// src/content/config.ts
import { defineCollection, z } from 'astro:content';
const blog = defineCollection({
    type: 'content',
    schema: z.object({ title: z.string(), date: z.date(), tags: z.array(z.string()).optional() }),
});
export const collections = { blog };

Content Files

---
title: My First Post
date: 2024-01-15
tags: [typescript, astro]
---
# Hello World
Content here...

Query

---
import { getCollection } from 'astro:content';
const posts = await getCollection('blog');
const sorted = posts.sort((a, b) => b.data.date - a.data.date);
---
{sorted.map(post => <a href={`/blog/${post.slug}`}>{post.data.title}</a>)}

ComeOnOliver/skillshub/tree/main/skills/skillshub-team/catalog-batch5/astro-content commit 9f49f18b7c

Frequently asked questions

npx skillmds@latest add comeonoliver/astro-content