-
Notifications
You must be signed in to change notification settings - Fork 12
/
gatsby-node.ts
185 lines (172 loc) · 4.88 KB
/
gatsby-node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import path from "path";
import { GatsbyNode } from "gatsby";
export const createPages: GatsbyNode["createPages"] = async ({
actions,
graphql,
}) => {
const { createPage } = actions;
await pagination(createPage, graphql);
await detailPage(createPage, graphql);
await tagsPage(createPage, graphql);
};
type NextEdge =
Queries.NextPrevQueryQuery["allMarkdownRemark"]["edges"][number]["next"];
type PrevEdge =
Queries.NextPrevQueryQuery["allMarkdownRemark"]["edges"][number]["previous"];
export interface DetailPageContext {
next: NextEdge;
prev: PrevEdge;
id: string;
tags: string[];
}
const pagination = async (
createPage: Parameters<
NonNullable<GatsbyNode["createPages"]>
>["0"]["actions"]["createPage"],
graphql: Parameters<NonNullable<GatsbyNode["createPages"]>>["0"]["graphql"]
) => {
const paginationIndexPageResult =
await graphql<Queries.PaginationQueryQuery>(`
query PaginationQuery {
allMarkdownRemark(
sort: { frontmatter: { created: DESC } }
limit: 1000
) {
nodes {
frontmatter {
path
}
}
}
}
`);
if (!paginationIndexPageResult.data || paginationIndexPageResult.errors) {
throw new Error("pagination 用のデータ取得に失敗しました。");
}
const posts = paginationIndexPageResult.data.allMarkdownRemark.nodes;
if (posts === undefined) {
throw new Error("pagination 用のデータが見つかりませんでした。");
}
const postsPerPage = 50;
const numPages = Math.ceil(posts.length / postsPerPage);
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/` : `/posts/${i + 1}`,
component: path.resolve("./src/templates/root-page.tsx"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
},
});
});
};
const detailPage = async (
createPage: Parameters<
NonNullable<GatsbyNode["createPages"]>
>["0"]["actions"]["createPage"],
graphql: Parameters<NonNullable<GatsbyNode["createPages"]>>["0"]["graphql"]
) => {
const getNextPrevsResult = await graphql<Queries.NextPrevQueryQuery>(`
query NextPrevQuery {
allMarkdownRemark(sort: { frontmatter: { created: DESC } }) {
edges {
next {
frontmatter {
path
title
visual {
childImageSharp {
gatsbyImageData(width: 120, height: 90)
}
}
}
timeToRead
excerpt(pruneLength: 40)
}
previous {
frontmatter {
path
title
visual {
childImageSharp {
gatsbyImageData(width: 120, height: 90)
}
}
}
timeToRead
excerpt(pruneLength: 40)
}
node {
frontmatter {
path
tags
}
id
}
}
}
}
`);
if (!getNextPrevsResult.data || getNextPrevsResult.errors) {
throw new Error("全ページURLのデータ取得に失敗しました。");
}
getNextPrevsResult.data.allMarkdownRemark.edges.forEach((edge) => {
if (!edge.node.frontmatter || !edge.node.frontmatter.tags) {
throw new Error("data should be");
}
const context: DetailPageContext = {
id: edge.node.id,
next: edge.next,
prev: edge.previous,
tags: edge.node.frontmatter.tags.filter((t) => Boolean(t)) as string[],
};
if (!edge.node.frontmatter?.path) {
throw new Error("path 情報がありません");
}
createPage({
path: `${edge.node.frontmatter.path}`,
component: path.resolve("./src/templates/detail-page.tsx"),
context,
});
});
};
export interface TagPageContext {
tag: string;
}
const tagsPage = async (
createPage: Parameters<
NonNullable<GatsbyNode["createPages"]>
>["0"]["actions"]["createPage"],
graphql: Parameters<NonNullable<GatsbyNode["createPages"]>>["0"]["graphql"]
) => {
const getTagsResult = await graphql<Queries.AllTagsQuery>(`
query AllTags {
tags: allMarkdownRemark {
group(field: { frontmatter: { tags: SELECT } }) {
tag: fieldValue
totalCount
}
}
}
`);
if (!getTagsResult.data || getTagsResult.errors) {
throw new Error("全tagのデータ取得に失敗しました。");
}
// create each page
getTagsResult.data.tags.group.forEach((tag) => {
if (tag.tag === null) {
throw new Error("tag should be there");
}
const context: TagPageContext = {
// This is needed for query by tag in tag page.
tag: tag.tag,
};
createPage({
path: `/tags/${tag.tag}`,
component: path.resolve("./src/templates/tag-page.tsx"),
context,
});
});
};