Skip to content

Commit 63191df

Browse files
committed
refac: centralize getCollection query
1 parent 3fb98bd commit 63191df

File tree

12 files changed

+97
-68
lines changed

12 files changed

+97
-68
lines changed

contents/project-kinds.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ export const kinds = [
33
frontmatter: "long-term",
44
path: undefined,
55
title: "長期プロジェクト",
6+
description:
7+
"ut.code(); で長期間にわたって開発を行っているプロジェクトです",
68
tabTitle: "長期プロジェクト",
79
},
810
{
911
frontmatter: "festival",
1012
path: "festival",
1113
title: "学園祭プロジェクト",
14+
description:
15+
"ut.code(); が毎年参加している、学園祭に展示するプロジェクトです",
1216
tabTitle: "学園祭",
1317
},
1418
{
1519
frontmatter: "hackathon",
1620
path: "hackathon",
1721
title: "ハッカソンプロジェクト",
22+
description: "ut.code(); で短期間で開発したハッカソンのプロジェクトです",
1823
tabTitle: "ハッカソン",
1924
},
2025
] as const;

contents/projects/README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
# プロジェクトページ
22

3-
[`project.tsx`](/src/templates/project.tsx) によってレンダリングされるプロジェクト詳細ページです。
3+
`src/pages/projects/[...id].astro` によってレンダリングされるプロジェクト詳細ページです。
4+
5+
プロジェクトには 3 種類あります。
6+
7+
- 長期プロジェクト
8+
- 学園祭プロジェクト
9+
- ハッカソン
10+
11+
歴史的な経緯で長期プロジェクトは `contents/projects` 直下にありますが、学園祭プロジェクトとハッカソンは `contents/projects/{festival,hackathon}`にいれるようにしています。
412

513
## frontmatter
614

715
| キー | 必須 | 説明 |
816
| ------------- | ---- | ------------------------------------------------------------------------------ |
917
| `title` || プロジェクト名 |
10-
| `slug` || ウェブサイト上の URL で使用される文字列 (重複不可) |
1118
| `order` | | 表示順。指定されなかった場合は `date` 降順でソートされます。 |
1219
| `date` || プロジェクトの開始日。現状ソートのみで利用しています。 |
13-
| `image` || イメージ画像。 |
20+
| `image` || イメージ画像に関するデータ。 |
21+
| `image.src` || イメージファイルへの markdown からの相対パス。 |
22+
| `image.fit` | | イメージのクロップ方法。 default = "cover"。 |
23+
| `image.bg` | | イメージの背景色。ロード中と `crop` = "contain" のときの背景に使われています。 |
1424
| `description` || 短い説明。 |
15-
| `tags` | | 使用されている技術など。現状タグごとのフィルタリング機能等は提供していません。 |
25+
| `tags` | | 使用されている技術など。現状タグごとのフィルタリング機能等は提供していません。 |
1626
| `github` | | プロジェクトの GitHub 上での URL。 |
1727
| `youtube` | | プロジェクトの YouTube 上での URL。 |
1828
| `website` | | プロジェクトのウェブサイトの URL。 |
1929

20-
- URL とアイコンを追加したい場合は、 [`project.tsx`](/src/templates/project.tsx) のアイコン表示部分と、末尾にある query を編集する必要があります
30+
## body について
31+
32+
長期プロジェクトの body は各長期プロジェクトのページに使用されています。
33+
それ以外は必要ありません。

src/content.config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ const members = defineCollection({
1919
schema: CreateMemberSchema,
2020
});
2121
const projects = defineCollection({
22-
loader: glob({ base: "./contents/projects", pattern: "*/index.{md,mdx}" }),
22+
loader: glob({ base: "./contents/projects", pattern: "**/index.{md,mdx}" }),
2323
schema: CreateProjectSchema,
2424
});
2525

26-
export const collections = { articles, members, projects };
26+
export const collections = {
27+
articles,
28+
members,
29+
projects,
30+
};

src/pages/articles/[...id].astro

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---
22
import { Picture } from "astro:assets";
3-
import { getCollection, getEntry, render } from "astro:content";
3+
import { render } from "astro:content";
44
import JoinUsCTA from "+/components/JoinUsCTA.astro";
55
import GlobalLayout from "+/layouts/GlobalLayout.astro";
6-
import { Focus } from "+/schema";
6+
import { getArticles, getMember } from "+/query.ts";
7+
import { Focus } from "+/schema.ts";
78
import { format } from "date-fns";
89
910
export async function getStaticPaths() {
10-
const articles = await getCollection("articles");
11+
const articles = await getArticles();
1112
return articles.map((article) => ({
1213
params: { id: article.id },
1314
props: { article },
@@ -16,12 +17,7 @@ export async function getStaticPaths() {
1617
1718
const { article } = Astro.props;
1819
const { Content } = await render(article);
19-
const author =
20-
article.data.author && (await getEntry("members", article.data.author.id));
21-
if (article.data.author && !author)
22-
throw new Error(
23-
`Author not found for article ${article.id}, searched for author ${article.data.author.id}`,
24-
);
20+
const author = article.data.author && (await getMember(article.data.author.id));
2521
---
2622

2723
<GlobalLayout

src/pages/articles/[...page].astro

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
---
2-
import { getCollection } from "astro:content";
32
import ArticleList from "+/components/ArticleList.astro";
43
import GlobalLayout from "+/layouts/GlobalLayout.astro";
4+
import { getArticles } from "+/query";
55
import { Focus } from "+/schema";
66
import type { GetStaticPaths } from "astro";
77
88
export const getStaticPaths = (async ({ paginate }) => {
9-
const articles = (await getCollection("articles")).sort(
10-
(a, b) => b.data.date.getTime() - a.data.date.getTime(),
11-
);
9+
const articles = await getArticles();
1210
return paginate(articles, { pageSize: 18 });
1311
}) satisfies GetStaticPaths;
1412

src/pages/index.astro

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
import { Picture } from "astro:assets";
3-
import { getCollection } from "astro:content";
43
import { Icon } from "astro-icon/components";
54
65
import GlobalLayout from "+/layouts/GlobalLayout.astro";
@@ -9,25 +8,17 @@ import ActionButton from "+/components/ActionButton.astro";
98
import ArticleList from "+/components/ArticleList.astro";
109
import ProjectList from "+/components/ProjectList.astro";
1110
import SectionHeader from "+/components/per-page/SectionHeader.astro";
11+
import { getArticles, getProjects } from "+/query.ts";
1212
import { Focus } from "+/schema.ts";
1313
1414
import LogoGMOMedia from "+/images/donators/gmo-media.png";
1515
import Hero from "+/images/headers/index.jpg";
1616
import LabCafe from "+/images/lab-cafe.jpg";
1717
18-
const projects = (await getCollection("projects")).sort((a, b) => {
19-
const a_order = a.data.order ?? Number.POSITIVE_INFINITY;
20-
const b_order = b.data.order ?? Number.POSITIVE_INFINITY;
21-
if (a_order !== b_order) {
22-
return a_order - b_order;
23-
}
24-
return b.data.date.getTime() - a.data.date.getTime();
25-
});
18+
const projects = await getProjects("long-term");
2619
projects.splice(6);
2720
28-
const articles = (await getCollection("articles")).sort(
29-
(a, b) => b.data.date.getTime() - a.data.date.getTime(),
30-
);
21+
const articles = await getArticles();
3122
articles.splice(3);
3223
---
3324

src/pages/members.astro

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
---
22
import { Picture } from "astro:assets";
3-
import { getCollection } from "astro:content";
43
import JoinUsCTA from "+/components/JoinUsCTA.astro";
54
import Header from "+/images/headers/members.jpg";
65
import GlobalLayout from "+/layouts/GlobalLayout.astro";
7-
import { Focus } from "+/schema";
6+
import { getMembers } from "+/query.ts";
7+
import { Focus } from "+/schema.ts";
88
9-
const members = (await getCollection("members")).sort(
10-
(a, b) => b.data.joinYear - a.data.joinYear,
11-
);
9+
const members = await getMembers();
1210
---
1311

1412
<GlobalLayout

src/pages/members/[member].astro

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
---
22
import { Picture } from "astro:assets";
3-
import { getCollection, render } from "astro:content";
3+
import { render } from "astro:content";
44
import ArticleList from "+/components/ArticleList.astro";
55
import JoinUsCTA from "+/components/JoinUsCTA.astro";
66
import GlobalLayout from "+/layouts/GlobalLayout.astro";
7+
import { getArticles, getMembers } from "+/query";
78
import { Focus } from "+/schema";
89
import { Icon } from "astro-icon/components";
910
1011
export async function getStaticPaths() {
11-
const members = await getCollection("members");
12+
const members = await getMembers();
1213
return members.map((member) => ({
1314
params: { member: member.id },
1415
props: { member },
@@ -18,11 +19,9 @@ export async function getStaticPaths() {
1819
const { member } = Astro.props;
1920
const { Content } = await render(member);
2021
21-
const articles = await getCollection(
22-
"articles",
23-
(article) => article.data.author?.id === member.id,
24-
);
25-
articles.sort((a, b) => b.data.date.getTime() - a.data.date.getTime());
22+
const articles = await getArticles({
23+
where: (article) => article.data.author?.id === member.id,
24+
});
2625
---
2726

2827
<GlobalLayout
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
---
22
import { Picture } from "astro:assets";
3-
import { getCollection } from "astro:content";
43
import { render } from "astro:content";
54
import ActionButton from "+/components/ActionButton.astro";
65
import JoinUsCTA from "+/components/JoinUsCTA.astro";
76
import GlobalLayout from "+/layouts/GlobalLayout.astro";
8-
import { Focus, type Project } from "+/schema";
7+
import { getProjects } from "+/query";
8+
import { Focus } from "+/schema.ts";
99
import type { GetStaticPaths } from "astro";
1010
import { Icon } from "astro-icon/components";
1111
1212
export const getStaticPaths = (async () => {
13-
const projects = await getCollection("projects");
13+
const projects = await getProjects("all");
1414
return projects.map((project) => ({
15-
params: { project: project.id },
15+
params: { id: project.id },
1616
props: { project },
1717
}));
1818
}) satisfies GetStaticPaths;

src/pages/projects/[...kind].astro

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
import { getCollection } from "astro:content";
32
import ProjectList from "+/components/ProjectList.astro";
43
import GlobalLayout from "+/layouts/GlobalLayout.astro";
4+
import { getProjects } from "+/query";
55
import { Focus } from "+/schema";
66
import { kinds } from "+contents/project-kinds.ts";
77
@@ -14,23 +14,12 @@ export function getStaticPaths() {
1414
1515
const kind = Astro.props.kind;
1616
17-
const projects = (await getCollection("projects"))
18-
.filter((project) => {
19-
return project.data.kind === kind.frontmatter;
20-
})
21-
.sort((a, b) => {
22-
const a_order = a.data.order ?? Number.POSITIVE_INFINITY;
23-
const b_order = b.data.order ?? Number.POSITIVE_INFINITY;
24-
if (a_order !== b_order) {
25-
return a_order - b_order;
26-
}
27-
return b.data.date.getTime() - a.data.date.getTime();
28-
});
17+
const projects = await getProjects(kind.frontmatter);
2918
---
3019

3120
<GlobalLayout
32-
title="長期プロジェクト"
33-
description="ut.code(); で長期間にわたって開発を行っているプロジェクトです。"
21+
title={kind.title}
22+
description={kind.description}
3423
focus={Focus.projects}
3524
image={null}
3625
>
@@ -42,10 +31,7 @@ const projects = (await getCollection("projects"))
4231
<a>
4332
<a
4433
role="tab"
45-
class:list={[
46-
"tab",
47-
{ "tab-active": kind.frontmatter === tab.frontmatter },
48-
]}
34+
class:list={["tab", { "tab-active": kind.path === tab.path }]}
4935
href={tab.path ? `/projects/${tab.path}` : "/projects"}
5036
>
5137
{tab.tabTitle}

0 commit comments

Comments
 (0)