From 7573617e798642da0a70bea54845df01fcb9802f Mon Sep 17 00:00:00 2001 From: Patrick Wilson Date: Mon, 14 Oct 2024 19:49:40 +0800 Subject: [PATCH] feat: add archives page with configurable menu (#386) * feat: add archives page with configurable menu * optimize: compat mobile layout * fix: update archive menu layout * fix: hide archives page from URL if showArchives is false * docs: add `showArchives` option in docs Resolves #361 --------- Co-authored-by: satnaing --- src/components/Header.astro | 39 +++++++++- src/config.ts | 1 + .../blog/how-to-configure-astropaper-theme.md | 4 +- src/pages/archives/index.astro | 74 +++++++++++++++++++ src/types.ts | 1 + src/utils/getPostsByGroupCondition.ts | 25 +++++++ tailwind.config.cjs | 5 ++ 7 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 src/pages/archives/index.astro create mode 100644 src/utils/getPostsByGroupCondition.ts diff --git a/src/components/Header.astro b/src/components/Header.astro index 43a4a7168..e0b1144fb 100644 --- a/src/components/Header.astro +++ b/src/components/Header.astro @@ -4,7 +4,7 @@ import Hr from "./Hr.astro"; import LinkButton from "./LinkButton.astro"; export interface Props { - activeNav?: "posts" | "tags" | "about" | "search"; + activeNav?: "posts" | "archives" | "tags" | "about" | "search"; } const { activeNav } = Astro.props; @@ -70,6 +70,41 @@ const { activeNav } = Astro.props; About + { + SITE.showArchives && ( +
  • + + + <> + + + + + + + + Archives + + +
  • + ) + }
  • !data.draft); + +const MonthMap: Record = { + "1": "January", + "2": "February", + "3": "March", + "4": "April", + "5": "May", + "6": "June", + "7": "July", + "8": "August", + "9": "September", + "10": "October", + "11": "November", + "12": "December", +}; +--- + + +
    +
    + { + Object.entries( + getPostsByGroupCondition(posts, post => + post.data.pubDatetime.getFullYear() + ) + ) + .sort(([yearA], [yearB]) => Number(yearB) - Number(yearA)) + .map(([year, yearGroup]) => ( +
    + {year} + {yearGroup.length} + {Object.entries( + getPostsByGroupCondition( + yearGroup, + post => post.data.pubDatetime.getMonth() + 1 + ) + ) + .sort(([monthA], [monthB]) => Number(monthB) - Number(monthA)) + .map(([month, monthGroup]) => ( +
    +
    + {MonthMap[month]} + {monthGroup.length} +
    +
      + {monthGroup.map(({ data, slug }) => ( + + ))} +
    +
    + ))} +
    + )) + } +
    + +