mirror of
				https://github.com/valentineus/popov.link.git
				synced 2025-11-04 06:49:45 +03:00 
			
		
		
		
	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.
This commit is contained in:
		@@ -1,4 +1,5 @@
 | 
				
			|||||||
---
 | 
					---
 | 
				
			||||||
 | 
					import type { CollectionEntry } from "astro:content";
 | 
				
			||||||
import { getCollection } from "astro:content";
 | 
					import { getCollection } from "astro:content";
 | 
				
			||||||
import Layout from "../../layouts/BaseLayout.astro";
 | 
					import Layout from "../../layouts/BaseLayout.astro";
 | 
				
			||||||
import PostElement from "../../components/PostElement.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());
 | 
					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>
 | 
					<Layout>
 | 
				
			||||||
	<section style={{ "margin-top": "3rem" }}>
 | 
						<section style={{ "margin-top": "3rem" }}>
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								years.map((year) => (
 | 
				
			||||||
 | 
									<div>
 | 
				
			||||||
 | 
										<div style={{ "margin-bottom": "1rem" }}>{year}</div>
 | 
				
			||||||
					<ul>
 | 
										<ul>
 | 
				
			||||||
			{posts.map((post) => <PostElement post={post} />)}
 | 
											{postsByYear[year].map((post) => (
 | 
				
			||||||
 | 
												<PostElement post={post} />
 | 
				
			||||||
 | 
											))}
 | 
				
			||||||
					</ul>
 | 
										</ul>
 | 
				
			||||||
 | 
									</div>
 | 
				
			||||||
 | 
								))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	</section>
 | 
						</section>
 | 
				
			||||||
</Layout>
 | 
					</Layout>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user