Skip to content

Commit d79d1fa

Browse files
committed
refactor: move blog posts to content collection
1 parent 913944d commit d79d1fa

File tree

8 files changed

+1823
-1427
lines changed

8 files changed

+1823
-1427
lines changed

package-lock.json

+1,770-1,416
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
"astro": "astro"
1111
},
1212
"dependencies": {
13-
"@astrojs/react": "^3.0.2",
14-
"@astrojs/tailwind": "^5.0.0",
13+
"@astrojs/react": "^3.0.10",
14+
"@astrojs/tailwind": "^5.1.0",
1515
"@types/react": "^18.2.21",
1616
"@types/react-dom": "^18.2.7",
17-
"astro": "^3.1.0",
17+
"astro": "^4.4.6",
1818
"astro-icon": "^0.8.1",
1919
"react": "^18.2.0",
2020
"react-dom": "^18.2.0",
21-
"tailwindcss": "^3.3.3"
21+
"tailwindcss": "^3.4.1"
2222
},
2323
"devDependencies": {
2424
"@tailwindcss/typography": "^0.5.10",

src/content/config.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { z, defineCollection } from "astro:content";
2+
3+
const posts = defineCollection({
4+
type: "content",
5+
schema: z.object({
6+
title: z.string(),
7+
pubDate: z.date(),
8+
description: z.string(),
9+
author: z.string(),
10+
image: z
11+
.object({
12+
url: z.string(),
13+
alt: z.string(),
14+
})
15+
.optional(),
16+
tags: z.array(z.string()),
17+
}),
18+
});
19+
20+
export const collections = {
21+
posts,
22+
};

src/pages/blog/why-astro.md src/content/posts/why-astro.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
2-
layout: "../../layouts/BlogLayout.astro"
32
title: "Why Astro?"
4-
pubDate: "2023/12/21"
3+
pubDate: 2023-12-21
54
description: "My first blog post, which explains my reasoning for choosing Astro to build my personal website"
65
author: "Lucas Machado"
76
tags: ["astro", "blogging", "learning in public"]

src/env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/// <reference path="../.astro/types.d.ts" />
12
/// <reference types="astro/client" />

src/layouts/BlogLayout.astro

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const formatDate = (date: Date): string => {
2222
day: "numeric",
2323
month: "long",
2424
year: "numeric",
25-
}).formatToParts(date.setHours(0, 0, 0, 0));
25+
}).formatToParts(date);
2626
2727
return parts
2828
.map((part) => {
@@ -42,7 +42,7 @@ const formatDate = (date: Date): string => {
4242
<h1 class="leading-tight font-bold text-3xl text-slate-200">
4343
{frontmatter.title}
4444
</h1>
45-
<p class="pt-2 pb-8">{formatDate(new Date(frontmatter.pubDate))}</p>
45+
<p class="pt-2 pb-8">{formatDate(frontmatter.pubDate)}</p>
4646
<slot />
4747
<p class="pt-8 font-medium text-slate-200">
4848
Written by {frontmatter.author}

src/pages/blog.astro

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
---
2+
import { getCollection } from "astro:content";
23
import Layout from "../layouts/Layout.astro";
34
45
const metadata = {
56
title: "The Lucas Machado Blog",
67
description: "Lucas Machado's personal blog.",
78
};
89
9-
const postList = await Astro.glob("../pages/blog/*.md");
10+
const postCollection = await getCollection("posts");
1011
---
1112

1213
<Layout {...metadata}>
@@ -23,9 +24,9 @@ const postList = await Astro.glob("../pages/blog/*.md");
2324
</h2>
2425
<ul>
2526
{
26-
postList.map((post) => (
27+
postCollection.map((post) => (
2728
<li>
28-
<a href={post.url}>{post.frontmatter.title}</a>
29+
<a href={`/posts/${post.slug}`}>{post.data.title}</a>
2930
</li>
3031
))
3132
}

src/pages/posts/[...slug].astro

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
import { getCollection } from "astro:content";
3+
import BlogLayout from "../../layouts/BlogLayout.astro";
4+
5+
export async function getStaticPaths() {
6+
const blogPosts = await getCollection("posts");
7+
return blogPosts.map((post) => ({
8+
params: { slug: post.slug },
9+
props: { post },
10+
}));
11+
}
12+
13+
const { post } = Astro.props;
14+
const { Content } = await post.render();
15+
---
16+
17+
<BlogLayout frontmatter={post.data}>
18+
<Content />
19+
</BlogLayout>

0 commit comments

Comments
 (0)