--- import type { BlogPost } from '@/lib/posts'; interface Props { posts: BlogPost[]; } type ArchiveMonth = { key: string; label: string; posts: BlogPost[]; }; type ArchiveYear = { year: number; count: number; months: ArchiveMonth[]; }; const { posts } = Astro.props; const monthFormatter = new Intl.DateTimeFormat('zh-CN', { month: 'long' }); const dayFormatter = new Intl.DateTimeFormat('zh-CN', { day: '2-digit' }); const archiveYears = posts.reduce((years, post) => { const year = post.date.getFullYear(); const monthKey = `${year}-${String(post.date.getMonth() + 1).padStart(2, '0')}`; let yearGroup = years.find((item) => item.year === year); if (!yearGroup) { yearGroup = { year, count: 0, months: [] }; years.push(yearGroup); } let monthGroup = yearGroup.months.find((item) => item.key === monthKey); if (!monthGroup) { monthGroup = { key: monthKey, label: monthFormatter.format(post.date), posts: [], }; yearGroup.months.push(monthGroup); } yearGroup.count += 1; monthGroup.posts.push(post); return years; }, []); ---
{archiveYears.map((yearGroup) => (

{yearGroup.year}

{yearGroup.count} 篇
{yearGroup.months.map((monthGroup) => (
{monthGroup.label} {monthGroup.posts.length} 篇
{monthGroup.posts.map((post) => ( {post.title} ))}
))}
))}