This repository has been archived by the owner on Jun 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.ts
151 lines (129 loc) · 3.44 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
import {GatsbyNode} from "gatsby";
import readingTime from 'reading-time';
import path from "path";
import axios from 'axios';
import crypto from 'crypto';
import {toKebabCase} from "./src/lib/toKebabCase";
interface BlogPostInterface {
id: string;
internal: {
contentFilePath: string;
}
fields: {
slug: string;
}
}
interface TagGroupsInterface {
fieldValue: string;
}
export const createPages: GatsbyNode['createPages'] = async ({ actions, graphql }) => {
const { createPage } = actions;
const blogPostTemplate = path.resolve('./src/templates/BlogPostTemplate.tsx');
const queryResponse = await graphql(`
{
posts: allMdx(
limit: 1000
filter: { internal: {contentFilePath: {regex: "/posts/"}}}
) {
nodes {
id
internal {
contentFilePath
}
fields {
slug
}
}
},
tagsGroup: allMdx(
limit: 1000,
filter: { internal: {contentFilePath: {regex: "/posts/"}}}
) {
group(field: { frontmatter: { tags: SELECT } }) {
fieldValue
}
}
}
`);
const posts = queryResponse?.data?.posts?.nodes;
if(posts) {
posts.forEach((post: BlogPostInterface) => {
createPage({
path: `/posts/${post.fields.slug}`,
component: `${blogPostTemplate}?__contentFilePath=${post.internal.contentFilePath}`,
context: {
...post,
},
});
});
}
const { createRedirect } = actions;
createRedirect({
fromPath: '/cv',
toPath: '/resume',
isPermanent: true,
});
/**
* Create Tag Archive and singles
*/
const tagTemplate = path.resolve('./src/templates/Tags.tsx');
const tags = queryResponse?.data?.tagsGroup?.group;
tags.forEach((tag: TagGroupsInterface) => {
createPage({
path: `posts/-/tags/${toKebabCase(tag.fieldValue)}`,
component: tagTemplate,
context: {
tag: tag.fieldValue,
},
});
});
};
export const onCreateNode: GatsbyNode['onCreateNode'] = async ({ node, actions }) => {
const {createNodeField} = actions;
if (node.internal.type === `Mdx`) {
if(!node.internal.contentFilePath) {
console.warn(`MDX Node missing contentFilePath unable to generate slug`);
return;
}
createNodeField({
node,
name: `slug`,
value: `${node.internal.contentFilePath.split('/posts/')[1].split('.')[0].split('/')[1]}`
});
if(node.body) {
createNodeField({
node,
name: `timeToRead`,
value: readingTime(node.body.toString())
})
}
}
}
export const sourceNodes: GatsbyNode["sourceNodes"] = async ({ actions }) => {
const { createNode } = actions;
// Create carbonFootprint
const carbonRequest = 'https://api.websitecarbon.com/site?url=https://jamesrwilliams.ca';
const carbonFootprintResponse = await axios.get(carbonRequest);
if(carbonFootprintResponse.data) {
const carbonFootprintData = carbonFootprintResponse.data;
const carbonNode = {
id: 'carbonFootprint',
parent: '__SOURCE__',
internal: {
type: 'CarbonFootprint',
},
children: [],
...carbonFootprintData
};
carbonNode.internal.contentDigest = crypto
.createHash('md5')
.update(JSON.stringify(carbonNode))
.digest('hex');
createNode(carbonNode);
}
};