diff options
author | Valentin Popov <valentin@popov.link> | 2025-06-11 19:05:50 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2025-06-11 19:05:50 +0300 |
commit | 3d6aedd272a2c73d65ce79036d63e6916e8f71b5 (patch) | |
tree | d04397a3eeb10007ac88b956c0ce611556f6d0e9 /src/pages/blog | |
parent | 6fe5df4e32ce7e7e4a2709bef4c7c881f1cc717e (diff) | |
download | popov.link-3d6aedd272a2c73d65ce79036d63e6916e8f71b5.tar.xz popov.link-3d6aedd272a2c73d65ce79036d63e6916e8f71b5.zip |
feat: group blog posts by year in index page
- Implemented functionality to categorize blog posts by publication year.
- Updated the blog index page to display posts organized under their respective years for improved navigation.
Diffstat (limited to 'src/pages/blog')
-rw-r--r-- | src/pages/blog/index.astro | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/pages/blog/index.astro b/src/pages/blog/index.astro index 48d33e8..0c33b66 100644 --- a/src/pages/blog/index.astro +++ b/src/pages/blog/index.astro @@ -1,4 +1,5 @@ --- +import type { CollectionEntry } from "astro:content"; import { getCollection } from "astro:content"; import Layout from "../../layouts/BaseLayout.astro"; import PostElement from "../../components/PostElement.astro"; @@ -8,12 +9,32 @@ const posts = await getCollection("blog", ({ data }) => { }); 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" }}> - <ul> - {posts.map((post) => <PostElement post={post} />)} - </ul> + { + years.map((year) => ( + <div> + <div style={{ "margin-bottom": "1rem" }}>{year}</div> + <ul> + {postsByYear[year].map((post) => ( + <PostElement post={post} /> + ))} + </ul> + </div> + )) + } </section> </Layout> |