Skip to content

Commit a6dae47

Browse files
committed
refactor: combine blog formats into one collection
1 parent 59ce538 commit a6dae47

File tree

6 files changed

+18
-45
lines changed

6 files changed

+18
-45
lines changed

src/content.config.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ import { defineCollection, z } from "astro:content"
22
import { glob } from "astro/loaders"
33

44
const posts_path = import.meta.env.PROD ? "./posts" : "./src/test/posts"
5-
const schema = z.object({
6-
title: z.string().min(1, "Title cannot be empty"),
7-
createdAt: z.date(),
8-
updatedAt: z.date().optional(),
9-
})
105

116
export const collections = {
127
blog: defineCollection({
13-
loader: glob({ pattern: "**/*.md", base: posts_path }),
14-
schema,
15-
}),
16-
blog_mdx: defineCollection({
17-
loader: glob({ pattern: "**/*.mdx", base: posts_path }),
18-
schema,
8+
loader: glob({ pattern: "**/*.{md,mdx}", base: posts_path }),
9+
schema: z.object({
10+
title: z.string().min(1, "Title cannot be empty"),
11+
createdAt: z.date(),
12+
updatedAt: z.date().optional(),
13+
})
1914
}),
2015
}

src/pages/api/hits.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import { getCollection } from "astro:content"
22
import type { APIRoute } from "astro"
33

44
const ResCode = (status: number) => new Response(null, { status })
5-
const collections = await Promise.all([
6-
getCollection("blog"),
7-
getCollection("blog_mdx"),
8-
])
9-
const ids = collections.flat().map(post => post.id)
5+
const ids = await getCollection("blog").then(c => c.map(post => post.id))
106

117
export const prerender = false
128
export const POST: APIRoute = async ({ locals, request }) => {

src/pages/blog.astro

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import Footer from "../components/footer.astro"
44
import Navbar from "../components/navbar.astro"
55
import Layout from "../layouts/posinega.astro"
66
7-
const collections = await Promise.all([
8-
getCollection("blog"),
9-
getCollection("blog_mdx"),
10-
])
7+
const collections = await getCollection("blog")
118
12-
const posts = collections
13-
.flat()
14-
.sort((a, b) => b.data.createdAt.getTime() - a.data.createdAt.getTime())
9+
const posts = collections.sort((a, b) => {
10+
return b.data.createdAt.getTime() - a.data.createdAt.getTime()
11+
})
1512
1613
const style = `
1714
display: inline-block;

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,8 @@ import "../../styles/blog.css"
1111
1212
const iso_date = (date: Date) => date.toISOString().slice(0, 10)
1313
export const getStaticPaths = (async () => {
14-
const [md_collection, mdx_collection] = await Promise.all([
15-
getCollection("blog"),
16-
getCollection("blog_mdx"),
17-
])
18-
const all_posts = [
19-
...md_collection.map(post => ({ ...post, type: "md" })),
20-
...mdx_collection.map(post => ({ ...post, type: "mdx" })),
21-
]
22-
23-
return all_posts.map(post => ({
14+
const collection = await getCollection("blog")
15+
return collection.map(post => ({
2416
params: { slug: post.id },
2517
props: { post, fm: post.data },
2618
}))

src/pages/index.astro

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ const social_links = [
1111
{ href: "mailto:hi+web@azrd.dev", text: "email" },
1212
]
1313
14-
const collections = await Promise.all([
15-
getCollection("blog"),
16-
getCollection("blog_mdx"),
17-
])
14+
const collections = await getCollection("blog")
1815
1916
const recent_posts = collections
20-
.flat()
2117
.sort((a, b) => b.data.createdAt.getTime() - a.data.createdAt.getTime())
2218
.slice(0, 3)
2319

src/pages/rss.xml.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1+
import type { APIRoute } from "astro"
12
import { getCollection } from "astro:content"
23
import rss from "@astrojs/rss"
3-
import type { APIRoute } from "astro"
44

5-
export const GET: APIRoute = async ctx => {
6-
const posts = await Promise.all([
7-
getCollection("blog"),
8-
getCollection("blog_mdx"),
9-
])
5+
export const GET: APIRoute = async (ctx) => {
6+
const posts = await getCollection("blog")
107

118
return rss({
129
title: "azrd",
1310
description: "My personal site blog",
1411
site: import.meta.env.DEV
1512
? `http://localhost:${ctx.url.port}`
1613
: import.meta.env.SITE,
17-
items: posts.flat().map(post => ({
14+
items: posts.map(post => ({
1815
title: post.data.title,
1916
pubDate: post.data.createdAt,
2017
link: `/blog/${post.id}`,

0 commit comments

Comments
 (0)