-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
162 lines (145 loc) · 4.58 KB
/
gatsby-node.js
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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require(`path`);
const readingTime = require(`reading-time`);
const {buildReadingList, validateNode} = require('./gatsby-utils');
exports.createPages = ({graphql, actions}) => {
const {createPage} = actions;
return new Promise((resolve, reject) => {
graphql(`
{
allMdx {
nodes {
slug
title
description
link
status
date(formatString: "DD MMM YYYY")
type
client
image {
source {
id
}
alt
attribution {
author
sourceName
sourceUrl
}
}
internal {
contentFilePath
}
}
}
}
`).then((result) => {
result.data.allMdx.nodes.forEach((node, _, nodes) => {
validateNode(node);
const componentPath = path.resolve(`./src/templates/${node.type}.tsx`);
const contentPath = node.internal.contentFilePath;
const component = `${componentPath}?__contentFilePath=${contentPath}`;
function getPath(node) {
if (node.type === 'Page') {
return `${node.slug}`;
}
const pathPrefix = node.type === 'Post' ? 'blog' : 'projects';
return `${pathPrefix}/${node.slug}`;
}
const recommendedReading =
node.type === 'Page'
? []
: buildReadingList(
nodes
.filter(function excludeUnpublishedNodes(currentNode) {
return (
currentNode.type === node.type &&
currentNode.status === 'Published'
);
})
.filter(function excludeCurrentNode(currentNode) {
return currentNode.slug !== node.slug;
}),
);
createPage({
path: getPath(node),
context: {
slug: node.slug,
recommendedReading: recommendedReading,
},
component: component,
});
});
resolve();
});
});
};
exports.onCreateNode = ({node, actions}) => {
const {createNodeField} = actions;
if (node.internal.type === `Mdx`) {
const rawTimeToRead = readingTime(node.body).minutes;
const roundedTimeToRead = Math.ceil(rawTimeToRead);
const timeToRead = roundedTimeToRead === 0 ? 1 : roundedTimeToRead;
createNodeField({
node,
name: `timeToRead`,
value: timeToRead,
});
}
};
exports.createSchemaCustomization = ({actions}) => {
const {createTypes} = actions;
/*
This dumps the type defs at this path, useful if you need to alias
something but want to use the actual type. The file is in the
.gitignore so it won't ever up in a commit.
*/
// actions.printTypeDefinitions({path: './.schema-type-definitions'});
// This proxies all of the frontmatter fields to just live directly
// on the mdx node
createTypes(`
type Mdx implements Node {
timeToRead: Float @proxy(from: "fields.timeToRead")
title: String @proxy(from: "frontmatter.title")
description: String @proxy(from: "frontmatter.description")
slug: String @proxy(from: "frontmatter.slug")
link: String @proxy(from: "frontmatter.link")
status: String @proxy(from: "frontmatter.status")
date: Date @dateformat @proxy(from: "frontmatter.date")
image: MdxFrontmatterImage @proxy(from: "frontmatter.image")
client: String @proxy(from: "frontmatter.client")
type: String @proxy(from: "frontmatter.type")
}
`);
};
// Silence the conflicting order warning
// https://robertmarshall.dev/blog/fix-warn-chunk-commons-mini-css-extract-plugin-error-in-gatsby-js/
const FilterWarningsPlugin = require('webpack-filter-warnings-plugin');
exports.onCreateWebpackConfig = ({actions}) => {
actions.setWebpackConfig({
plugins: [
new FilterWarningsPlugin({
exclude:
/mini-css-extract-plugin[^]*Conflicting order. Following module has been added:/,
}),
],
});
};
// This is so I can use ANTLR in Gatsby
// These are polyfills for Node APIs that are no longer included by default
exports.onCreateWebpackConfig = ({actions}) => {
actions.setWebpackConfig({
resolve: {
fallback: {
assert: require.resolve('assert'),
buffer: require.resolve('buffer'),
util: require.resolve('util'),
},
},
});
};