-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.ts
201 lines (177 loc) · 5.11 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 { CreatePagesArgs, GatsbyNode } from 'gatsby';
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/config/constants.ts';
import { getPageMetadata } from './src/managers/config.ts';
import {
getAuthorBioHtml,
getProjectsForPage,
} from './src/managers/content/projects.ts';
import { transformGithubDataNode } from './src/node/github-data-node-transformer.ts';
import { schema } from './src/node/graphql.ts';
import { setReporter, warn } from './src/node/logger.ts';
import {
createPage,
createRedirect,
deletePage,
setGatsbyNodeHelpers,
} from './src/node/utils.ts';
import type { PageMetadata } from './src/types/other.ts';
import type {
IndexPageContext,
ProjectPageContext,
ResumePageContext,
} from './src/types/page-context.ts';
import type { AbsolutePathString } from './src/types/strings.ts';
import type { EmptyObject } from './src/types/utils.ts';
// 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
async function createIndexPage(graphql: CreatePagesArgs['graphql']) {
const projects = await getProjectsForPage(graphql, INDEX_PATH);
const authorBioHtml = await getAuthorBioHtml(graphql);
const context: IndexPageContext = {
projects,
authorBioHtml,
};
createPage({
path: INDEX_PATH,
component: INDEX_PAGE_TEMPLATE,
socialImageComponent: INDEX_OG_IMAGE_TEMPLATE,
context: context,
});
}
// Create the resume page
async function createResumePage(graphql: CreatePagesArgs['graphql']) {
const projects = await getProjectsForPage(graphql, RESUME_PATH);
const pageMetadata: PageMetadata | EmptyObject =
getPageMetadata(RESUME_PATH) || {};
const context: ResumePageContext = {
pageMetadata,
projects,
};
createPage({
path: RESUME_PATH,
component: RESUME_PAGE_TEMPLATE,
socialImageComponent: OTHER_OG_IMAGE_TEMPLATE,
context: context,
});
}
// Create project pages
async function createProjectPages(graphql: CreatePagesArgs['graphql']) {
const projects = await getProjectsForPage(graphql, PROJECTS_PATH);
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 }) => {
await createProjectPages(graphql);
await createIndexPage(graphql);
await createResumePage(graphql);
createRedirects();
};