-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.ts
224 lines (197 loc) · 5.67 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import path from "path";
import { formatInTimeZone } from "date-fns-tz";
import { isDefined } from "./src/utils/typeguard";
import type {
Search,
OnCreatePagesStatefullyQuery,
} from "./src/generated/graphqlTypes";
import type { GatsbyNode } from "gatsby";
/*
Add import alias
*/
export const onCreateWebpackConfig: GatsbyNode["onCreateWebpackConfig"] = ({
actions,
}) => {
actions.setWebpackConfig({
resolve: {
alias: {
// see tsconfig.json
"@/constants": path.resolve("src/constants"),
"@/content": path.resolve("content"),
"@/components": path.resolve("src/components"),
"@/features": path.resolve("src/features"),
"@/generated": path.resolve("src/generated"),
"@/utils": path.resolve("src/utils"),
"@/layouts": path.resolve("src/layouts"),
},
},
});
};
/**
* Add Search nodes
*/
export const createPagesStatefully: GatsbyNode["createPagesStatefully"] =
async ({ graphql, reporter, actions, createNodeId, createContentDigest }) => {
const { createNode } = actions;
/**
* Add Search nodes
*/
const result = await graphql<OnCreatePagesStatefullyQuery>(/* GraphQL */ `
query OnCreatePagesStatefully {
timelineItems: allTimeline {
nodes {
__typename
title
date
... on ArticlesYaml {
url
}
... on SlidesYaml {
url
}
... on NotesYaml {
url
}
... on PresentationsYaml {
url
}
... on OthersYaml {
url
}
}
}
qiitaItems: allQiitaJson {
nodes {
title
url
}
}
}
`);
if (isDefined(result.errors)) throw result.errors;
const timelineList: Array<
Pick<Search, "title" | "slug" | "url" | "excerpt">
> = [];
result.data?.timelineItems.nodes.forEach((node) => {
timelineList.push({
title: node.title,
slug: "",
url: node.url,
excerpt: "",
});
});
result.data?.qiitaItems.nodes.forEach((node) => {
timelineList.push({
title: node.title,
slug: "",
url: node.url,
excerpt: "",
});
});
if (!isDefined(timelineList)) throw new Error("timelineList is undefined");
await Promise.all(
timelineList.map(async (timeline) => {
const node = {
...timeline,
id: createNodeId(timeline.title),
parent: null,
children: [],
internal: {
type: "Search",
contentDigest: createContentDigest(timeline),
},
};
await createNode(node);
}),
);
reporter.success(
`onCreatePagesStatefully: Created ${timelineList.length} search nodes`,
);
};
/**
* GraphQL Schema
* see https://www.gatsbyjs.com/docs/reference/graphql-data-layer/schema-customization/
*/
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] =
({ actions }) => {
const { createTypes } = actions;
const typeDefs = /* GraphQL */ `
type TimelineFields {
dateYear: Int!
endDateYear: Int
}
interface Timeline implements Node @dontInfer {
id: ID!
title: String!
date: Date! @dateformat
fields: TimelineFields!
}
interface Output implements Node & Timeline @dontInfer {
id: ID!
title: String!
date: Date! @dateformat
fields: TimelineFields!
}
type ArticlesYaml implements Node & Timeline & Output @dontInfer {
title: String!
date: Date! @dateformat
url: String!
fields: TimelineFields!
}
type SlidesYaml implements Node & Timeline & Output @dontInfer {
title: String!
date: Date! @dateformat
url: String!
fields: TimelineFields!
}
type NotesYaml implements Node & Timeline & Output @dontInfer {
title: String!
date: Date! @dateformat
url: String!
fields: TimelineFields!
}
type PresentationsYaml implements Node & Timeline & Output @dontInfer {
title: String!
date: Date! @dateformat
url: String!
fields: TimelineFields!
}
type OthersYaml implements Node & Timeline & Output @dontInfer {
title: String!
date: Date! @dateformat
url: String!
fields: TimelineFields!
}
type Search implements Node @dontInfer {
id: ID!
title: String!
excerpt: String!
url: String!
slug: String
}
`;
createTypes(typeDefs);
};
/**
* Add `dateYear` field to nodes
* Add `endDateYear` field to nodes
*/
export const onCreateNode: GatsbyNode<{
frontmatter: Record<string, unknown>;
}>["onCreateNode"] = ({ node, actions }) => {
const { createNodeField } = actions;
const date =
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- date is nullable
node?.date ?? node?.frontmatter?.date;
const endDate =
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- endDate is nullable
node?.endDate ?? node?.frontmatter?.endDate;
if (typeof date === "string" || date instanceof Date) {
const dateYear = formatInTimeZone(date, "Asia/Tokyo", "yyyy");
createNodeField({ node, name: "dateYear", value: Number(dateYear) });
}
if (typeof endDate === "string" || endDate instanceof Date) {
const endDateYear = formatInTimeZone(endDate, "Asia/Tokyo", "yyyy");
createNodeField({ node, name: "endDateYear", value: Number(endDateYear) });
}
};