0
mirror of https://github.com/valentineus/popov.link.git synced 2025-07-04 00:20:26 +03:00
Files
popov.link/src/pages/blog/index.astro

41 lines
1009 B
Plaintext
Raw Normal View History

---
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>