aboutsummaryrefslogtreecommitdiff
path: root/src/pages/blog/index.astro
blob: 0c33b660255d87fd003f7f0e7f92f4a737d239c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
---
import type { CollectionEntry } from "astro:content";
import { getCollection } from "astro:content";
import Layout from "../../layouts/BaseLayout.astro";
import PostElement from "../../components/PostElement.astro";

const posts = await getCollection("blog", ({ data }) => {
	return data.draft !== true;
});

posts.sort((a, b) => b.data.pubDate.getTime() - a.data.pubDate.getTime());

const postsByYear = posts.reduce<Record<string, CollectionEntry<"blog">[]>>((acc, post) => {
	const year = post.data.pubDate.getFullYear().toString();
	if (!acc[year]) {
		acc[year] = [];
	}
	acc[year].push(post);
	return acc;
}, {});

const years = Object.keys(postsByYear).sort((a, b) => Number(b) - Number(a));
---

<Layout>
	<section style={{ "margin-top": "3rem" }}>
		{
			years.map((year) => (
				<div>
					<div style={{ "margin-bottom": "1rem" }}>{year}</div>
					<ul>
						{postsByYear[year].map((post) => (
							<PostElement post={post} />
						))}
					</ul>
				</div>
			))
		}
	</section>
</Layout>