-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.ts
201 lines (176 loc) · 5.17 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
import { join, resolve } from 'node:path';
import type { GatsbyNode } from 'gatsby';
import { getPageMetadata } from './src/common/config-manager';
import {
ABOUT_PATH,
CONTACT_PATH,
EXPERIENCE_PATH,
INDEX_PATH,
PAGE_TEMPLATES_DIR,
PROJECTS_PATH,
PROJECTS_PATH_SHORT,
RESUME_PATH,
SOCIAL_IMAGES_PATH,
SOCIAL_IMAGE_TEMPLATES_DIR,
} from './src/common/constants';
import { transformGithubDataNode } from './src/node/github-data-node-transformer';
import { schema } from './src/node/graphql';
import { setReporter, warn } from './src/node/logger';
import { getProjects, getSubsetOfProjects } from './src/node/projects-manager';
import {
createPage,
createRedirect,
deletePage,
setGatsbyNodeHelpers,
} from './src/node/utils';
import { EntryPage, type PageMetadata } from './src/types/other';
import type {
IndexPageContext,
ProjectPageContext,
ResumePageContext,
} from './src/types/page-context';
import type { Project } from './src/types/projects';
import type { AbsolutePathString } from './src/types/strings';
import type { EmptyObject } from './src/types/utils';
// Constants
const INDEX_PAGE_TEMPLATE = resolve(PAGE_TEMPLATES_DIR, 'index.tsx');
const PROJECT_PAGE_TEMPLATE = resolve(PAGE_TEMPLATES_DIR, 'project.tsx');
const INDEX_OG_IMAGE_TEMPLATE = resolve(
SOCIAL_IMAGE_TEMPLATES_DIR,
'index.tsx',
);
const RESUME_PAGE_TEMPLATE = resolve(PAGE_TEMPLATES_DIR, 'resume.tsx');
const PROJECT_OG_IMAGE_TEMPLATE = resolve(
SOCIAL_IMAGE_TEMPLATES_DIR,
'project.tsx',
);
const OTHER_OG_IMAGE_TEMPLATE = resolve(
SOCIAL_IMAGE_TEMPLATES_DIR,
'other.tsx',
);
// Functions
// Create the landing page
function createIndexPage(projects: Project[], authorBioHtml: string) {
const context: IndexPageContext = {
projects: getSubsetOfProjects(projects, EntryPage.Index),
authorBioHtml,
};
createPage({
path: INDEX_PATH,
component: INDEX_PAGE_TEMPLATE,
socialImageComponent: INDEX_OG_IMAGE_TEMPLATE,
context: context,
});
}
// Sort GitHub repos by creation date
function sortByCreatedAt(a: Project, b: Project) {
return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
}
// Create the resume page
function createResumePage(projects: Project[]) {
const pageMetadata: PageMetadata | EmptyObject =
getPageMetadata(RESUME_PATH) || {};
const context: ResumePageContext = {
pageMetadata,
projects: getSubsetOfProjects(projects, EntryPage.Resume, sortByCreatedAt),
};
createPage({
path: RESUME_PATH,
component: RESUME_PAGE_TEMPLATE,
socialImageComponent: OTHER_OG_IMAGE_TEMPLATE,
context: context,
});
}
// Create project pages
function createProjectPages(projects: Project[]) {
for (const project of projects) {
const path: AbsolutePathString = join(
PROJECTS_PATH,
project.slug,
) as AbsolutePathString;
const shortPath: AbsolutePathString = join(
PROJECTS_PATH_SHORT,
project.slug,
) as AbsolutePathString;
const context: ProjectPageContext = {
project,
};
// Create project pages
createPage({
path,
component: PROJECT_PAGE_TEMPLATE,
socialImageComponent: PROJECT_OG_IMAGE_TEMPLATE,
context,
});
createRedirect(shortPath, path);
}
}
// Create client-side redirects
function createRedirects() {
const redirects = [
[ABOUT_PATH, '/#about'],
[PROJECTS_PATH, '/#projects'],
[EXPERIENCE_PATH, '/#experience'],
[CONTACT_PATH, '/#contact'],
];
for (const [fromPath, toPath] of redirects) {
createRedirect(fromPath, toPath);
}
}
// Save Gatsby Node API Helpers for later use
export const onPluginInit: GatsbyNode['onPluginInit'] = ({
reporter,
actions: { createPage, deletePage, createRedirect },
}) => {
setReporter(reporter);
setGatsbyNodeHelpers(createPage, deletePage, createRedirect);
};
// Add custom types to the GraphQL schema
export const createSchemaCustomization: GatsbyNode['createSchemaCustomization'] =
({ actions: { createTypes } }) => {
// Overwrite fields that we know are non-null (as per the GitHub GraphQL schema)
createTypes(schema);
};
// Transform source nodes into a more usable format
export const onCreateNode: GatsbyNode<Queries.GithubData>['onCreateNode'] = ({
node,
actions: { createNode },
createContentDigest,
}) => {
if (node.internal.type === 'GithubData') {
transformGithubDataNode(node, createNode, createContentDigest);
}
};
// Add metadata to automatically generated pages and generate the associated Open Graph images
export const onCreatePage: GatsbyNode['onCreatePage'] = ({ page }) => {
if (!page.path) {
return;
}
// Skip social images
if (page.path.startsWith(SOCIAL_IMAGES_PATH)) {
return;
}
const pageMetadata = getPageMetadata(page.path);
if (!pageMetadata) {
warn(`Skipped adding metadata to ${page.path}`);
return;
}
deletePage(page);
createPage({
...page,
path: page.path as AbsolutePathString,
socialImageComponent: OTHER_OG_IMAGE_TEMPLATE,
context: {
...page.context,
pageMetadata: pageMetadata,
},
});
};
// Manually create pages and generate the associated Open Graph images
export const createPages: GatsbyNode['createPages'] = async ({ graphql }) => {
const { projects, authorBioHtml } = await getProjects(graphql);
createProjectPages(projects);
createIndexPage(projects, authorBioHtml);
createResumePage(projects);
createRedirects();
};