|
| 1 | +--- |
| 2 | +import { getCollection } from "astro:content"; |
| 3 | +import Card from "@components/Card"; |
| 4 | +import Footer from "@components/Footer.astro"; |
| 5 | +import Header from "@components/Header.astro"; |
| 6 | +import { SITE } from "@config"; |
| 7 | +import Layout from "@layouts/Layout.astro"; |
| 8 | +import Main from "@layouts/Main.astro"; |
| 9 | +import getPostsByGroupCondition from "@utils/getPostsByGroupCondition"; |
| 10 | +
|
| 11 | +// Redirect to 404 page if `showArchives` config is false |
| 12 | +if (!SITE.showArchives) { |
| 13 | + return Astro.redirect("/404"); |
| 14 | +} |
| 15 | +
|
| 16 | +const posts = await getCollection("blog", ({ data }) => !data.draft); |
| 17 | +
|
| 18 | +const MonthMap: Record<string, string> = { |
| 19 | + "1": "January", |
| 20 | + "2": "February", |
| 21 | + "3": "March", |
| 22 | + "4": "April", |
| 23 | + "5": "May", |
| 24 | + "6": "June", |
| 25 | + "7": "July", |
| 26 | + "8": "August", |
| 27 | + "9": "September", |
| 28 | + "10": "October", |
| 29 | + "11": "November", |
| 30 | + "12": "December", |
| 31 | +}; |
| 32 | +--- |
| 33 | + |
| 34 | +<Layout title={`Archives | ${SITE.title}`}> |
| 35 | + <Header activeNav="archives" /> |
| 36 | + <Main pageTitle="Archives" pageDesc="All the articles I've archived."> |
| 37 | + { |
| 38 | + Object.entries( |
| 39 | + getPostsByGroupCondition(posts, post => |
| 40 | + post.data.pubDatetime.getFullYear() |
| 41 | + ) |
| 42 | + ) |
| 43 | + .sort(([yearA], [yearB]) => Number(yearB) - Number(yearA)) |
| 44 | + .map(([year, yearGroup]) => ( |
| 45 | + <div> |
| 46 | + <span class="text-2xl font-bold">{year}</span> |
| 47 | + <sup class="text-sm">{yearGroup.length}</sup> |
| 48 | + {Object.entries( |
| 49 | + getPostsByGroupCondition( |
| 50 | + yearGroup, |
| 51 | + post => post.data.pubDatetime.getMonth() + 1 |
| 52 | + ) |
| 53 | + ) |
| 54 | + .sort(([monthA], [monthB]) => Number(monthB) - Number(monthA)) |
| 55 | + .map(([month, monthGroup]) => ( |
| 56 | + <div class="flex flex-col sm:flex-row"> |
| 57 | + <div class="mt-6 min-w-36 text-lg sm:my-6"> |
| 58 | + <span class="font-bold">{MonthMap[month]}</span> |
| 59 | + <sup class="text-xs">{monthGroup.length}</sup> |
| 60 | + </div> |
| 61 | + <ul> |
| 62 | + {monthGroup.map(({ data, slug }) => ( |
| 63 | + <Card href={`/posts/${slug}`} frontmatter={data} /> |
| 64 | + ))} |
| 65 | + </ul> |
| 66 | + </div> |
| 67 | + ))} |
| 68 | + </div> |
| 69 | + )) |
| 70 | + } |
| 71 | + </Main> |
| 72 | + |
| 73 | + <Footer /> |
| 74 | +</Layout> |
0 commit comments