Skip to content

Commit

Permalink
Merge pull request #22 from hayato-osh/feature/tag-index
Browse files Browse the repository at this point in the history
タグ一覧ページを作成
  • Loading branch information
hayato-osh authored Apr 15, 2024
2 parents 302d753 + 4d2e780 commit fc456c7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/components/Header.astro
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---
import ThemeIcon from "./ThemeIcon.astro";
---
<header class="h-8 flex items-center justify-between">header<ThemeIcon /></header>
<header class="h-8 flex items-center justify-between">
<nav>
<a href="/tags">tags</a>
</nav>
<ThemeIcon />
</header>
4 changes: 1 addition & 3 deletions src/components/Post.astro
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
import type { CollectionEntry } from "astro:content";
interface Props {
post: CollectionEntry<"posts">;
post: Post;
}
const { post } = Astro.props;
Expand Down
6 changes: 2 additions & 4 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -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();
---

<GlobalLayout>
Expand Down
16 changes: 16 additions & 0 deletions src/pages/tags/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
import GlobalLayout from "@/layouts/GlobalLayout.astro";
import { getTags } from "@/utils/getTags";
const tags = await getTags();
---
<GlobalLayout>
<ul class="grid gap-2">
{Array.from(tags).map(([key, value]) => (
<li class="grid gap-1">
<a href={`/tags/${key}`} class="underline text-xl">{key}</a>
<span class="text-sm">{value.length} posts</span>
</li>
))}
</ul>
</GlobalLayout>
4 changes: 4 additions & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// <reference types="../../.astro/types" />
/// <reference types="astro" />

type Post = import("astro:content").CollectionEntry<"posts">;
9 changes: 9 additions & 0 deletions src/utils/getPosts.ts
Original file line number Diff line number Diff line change
@@ -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;
};
17 changes: 17 additions & 0 deletions src/utils/getTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { getPosts } from "./getPosts";

export const getTags = async () => {
const posts = await getPosts();

const tags = new Map<string, Post[]>();

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;
};

0 comments on commit fc456c7

Please sign in to comment.