From 15a3580ffc4ebf5789e21aa2d18d97cb3e932c70 Mon Sep 17 00:00:00 2001 From: Fastman Date: Mon, 15 Apr 2024 23:45:05 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20post=C2=A0=E4=B8=80=E8=A6=A7?= =?UTF-8?q?=E5=8F=96=E5=BE=97=E9=96=A2=E6=95=B0=E3=82=92=E5=88=87=E3=82=8A?= =?UTF-8?q?=E5=87=BA=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/index.astro | 6 ++---- src/utils/getPosts.ts | 9 +++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 src/utils/getPosts.ts diff --git a/src/pages/index.astro b/src/pages/index.astro index 1f8d56d..27c0200 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,10 +1,8 @@ --- -import { getCollection } from "astro:content"; import GlobalLayout from "@/layouts/GlobalLayout.astro"; +import { getPosts } from "@/utils/getPosts"; -const posts = (await getCollection("posts")).sort( - (a, b) => b.data.createdAt.getTime() - a.data.createdAt.getTime(), -); +const posts = await getPosts(); --- diff --git a/src/utils/getPosts.ts b/src/utils/getPosts.ts new file mode 100644 index 0000000..a793833 --- /dev/null +++ b/src/utils/getPosts.ts @@ -0,0 +1,9 @@ +import { getCollection } from "astro:content"; + +export const getPosts = async () => { + const posts = await getCollection("posts"); + + posts.sort((a, b) => b.data.createdAt.getTime() - a.data.createdAt.getTime()); + + return posts; +}; From 7e909e0ebf3f5772964ae60aa8ee7fe1755d748a Mon Sep 17 00:00:00 2001 From: Fastman Date: Mon, 15 Apr 2024 23:45:22 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E5=BD=A2=E3=82=92=20global=20?= =?UTF-8?q?=E3=81=AB=E5=AE=9A=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Post.astro | 4 +--- src/types/index.d.ts | 4 ++++ 2 files changed, 5 insertions(+), 3 deletions(-) create mode 100644 src/types/index.d.ts diff --git a/src/components/Post.astro b/src/components/Post.astro index 8ca0927..28522d6 100644 --- a/src/components/Post.astro +++ b/src/components/Post.astro @@ -1,8 +1,6 @@ --- -import type { CollectionEntry } from "astro:content"; - interface Props { - post: CollectionEntry<"posts">; + post: Post; } const { post } = Astro.props; diff --git a/src/types/index.d.ts b/src/types/index.d.ts new file mode 100644 index 0000000..d9e1160 --- /dev/null +++ b/src/types/index.d.ts @@ -0,0 +1,4 @@ +/// +/// + +type Post = import("astro:content").CollectionEntry<"posts">; From 4d2e7805a9c25403ef15c1cb288f3332db5aa57d Mon Sep 17 00:00:00 2001 From: Fastman Date: Mon, 15 Apr 2024 23:45:35 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=E3=82=BF=E3=82=B0=E4=B8=80?= =?UTF-8?q?=E8=A6=A7=E3=83=9A=E3=83=BC=E3=82=B8=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Header.astro | 7 ++++++- src/pages/tags/index.astro | 16 ++++++++++++++++ src/utils/getTags.ts | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/pages/tags/index.astro create mode 100644 src/utils/getTags.ts diff --git a/src/components/Header.astro b/src/components/Header.astro index 332ab12..f776600 100644 --- a/src/components/Header.astro +++ b/src/components/Header.astro @@ -1,4 +1,9 @@ --- import ThemeIcon from "./ThemeIcon.astro"; --- -
header
+
+ + +
diff --git a/src/pages/tags/index.astro b/src/pages/tags/index.astro new file mode 100644 index 0000000..ba919f8 --- /dev/null +++ b/src/pages/tags/index.astro @@ -0,0 +1,16 @@ +--- +import GlobalLayout from "@/layouts/GlobalLayout.astro"; +import { getTags } from "@/utils/getTags"; + +const tags = await getTags(); +--- + +
    + {Array.from(tags).map(([key, value]) => ( +
  • + {key} + {value.length} posts +
  • + ))} +
+
\ No newline at end of file diff --git a/src/utils/getTags.ts b/src/utils/getTags.ts new file mode 100644 index 0000000..77adec0 --- /dev/null +++ b/src/utils/getTags.ts @@ -0,0 +1,17 @@ +import { getPosts } from "./getPosts"; + +export const getTags = async () => { + const posts = await getPosts(); + + const tags = new Map(); + + for (const post of posts) { + for (const tag of post.data.tags) { + const posts = tags.get(tag) ?? []; + posts.push(post); + tags.set(tag, posts); + } + } + + return tags; +};