diff --git a/src/env.d.ts b/src/env.d.ts index 1cca6a7d..0f9a0a77 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -12,6 +12,7 @@ interface Post { created: number; modified: number; slug: string; + commentsNum: number; type: string; digest: string; permalink: string; diff --git a/src/pages/blog/archives.astro b/src/pages/blog/archives.astro index 81ff7531..e49e24cb 100644 --- a/src/pages/blog/archives.astro +++ b/src/pages/blog/archives.astro @@ -8,15 +8,37 @@ url.search = new URLSearchParams({ const response = await fetch(url); const data = await response.json(); const posts = data.data.dataSet; +const groupedPosts = groupByYear(posts); import Layout from "../../layouts/Layout.astro"; function formatTimestamp(timestamp: number) { const date = new Date(timestamp * 1000); - const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, "0"); const day = String(date.getDate()).padStart(2, "0"); - return `${year}-${month}-${day}`; + return `${month}-${day}`; +} + +interface GroupedPosts { + [year: number]: Post[]; +} + +function groupByYear(posts: Post[]): GroupedPosts { + function getYear(timestamp: number) { + const date = new Date(timestamp * 1000); + return date.getFullYear(); + } + + const ret: GroupedPosts = {}; + posts.forEach((post: Post) => { + const year = getYear(post.created); + if (ret[year] === undefined) { + ret[year] = []; + } + ret[year].push(post); + }); + + return ret; } --- @@ -33,25 +55,38 @@ function formatTimestamp(timestamp: number) {

共发布 {posts.length} 篇文章。


- + { + Object.entries(groupedPosts) + .sort((a, b) => parseInt(b[0]) - parseInt(a[0])) + .map((entry) => ( + <> +

{entry[0]}

+ + + )) + }