-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
255 lines (226 loc) · 6.28 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const fs = require(`fs`);
const path = require("path");
const kebabCase = require(`lodash.kebabcase`);
const mkdirp = require(`mkdirp`);
const defaultOptions = require(`./src/utils/default-options`);
const mdxResolverPassthrough = (fieldName) => async (
source,
args,
context,
info
) => {
const type = info.schema.getType(`Mdx`);
const mdxNode = context.nodeModel.getNodeById({
id: source.parent,
});
const resolver = type.getFields()[fieldName].resolve;
const result = await resolver(mdxNode, args, context, {
fieldName,
});
return result;
};
// Create general interfaces that you could can use to leverage other data sources
// The core theme sets up MDX as a type for the general interface
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes, createFieldExtension } = actions;
const slugify = (source) => {
const slug = source.slug ? source.slug : kebabCase(source.title);
return `/${slug}`.replace(/\/\/+/g, `/`);
};
createFieldExtension({
name: `slugify`,
extend() {
return {
resolve: slugify,
};
},
});
createFieldExtension({
name: `mdxpassthrough`,
args: {
fieldName: `String!`,
},
extend({ fieldName }) {
return {
resolve: mdxResolverPassthrough(fieldName),
};
},
});
createTypes(`
interface Page implements Node {
id: ID!
slug: String!
title: String!
description: String
excerpt(pruneLength: Int = 160): String!
body: String!
}
type MdxPage implements Node & Page {
slug: String!
title: String!
description: String
excerpt(pruneLength: Int = 140): String! @mdxpassthrough(fieldName: "excerpt")
body: String! @mdxpassthrough(fieldName: "body")
}
type MinimalBlogConfig implements Node {
pagesPath: String
externalLinks: [ExternalLink]
navigation: [NavigationEntry]
}
type ExternalLink {
name: String!
url: String!
}
type NavigationEntry {
title: String!
slug: String!
}
`);
};
exports.sourceNodes = ({ actions, createContentDigest }) => {
const { createNode } = actions;
const minimalBlogConfig = {
pagesPath: defaultOptions.pagesPath,
externalLinks: defaultOptions.externalLinks,
navigation: defaultOptions.navigation,
};
createNode({
...minimalBlogConfig,
id: `site >>> config`,
parent: null,
children: [],
internal: {
type: `MinimalBlogConfig`,
contentDigest: createContentDigest(minimalBlogConfig),
content: JSON.stringify(minimalBlogConfig),
},
});
};
exports.onCreateNode = ({
node,
actions,
getNode,
createNodeId,
createContentDigest,
}) => {
const { createNode, createParentChildLink } = actions;
// Make sure that it's an MDX node
if (node.internal.type !== `Mdx`) {
return;
}
// Create a source field
// And grab the sourceInstanceName to differentiate the different sources
// In this case "postsPath" and "pagesPath"
const fileNode = getNode(node.parent);
const source = fileNode.sourceInstanceName;
// Check for "pages" and create the "Page" type
if (node.internal.type === `Mdx` && source === "content") {
const fieldData = {
title: node.frontmatter.title,
description: node.frontmatter.description,
slug: node.frontmatter.slug,
};
const mdxPageId = createNodeId(`${node.id} >>> MdxPage`);
createNode({
...fieldData,
// Required fields
id: mdxPageId,
parent: node.id,
children: [],
internal: {
type: `MdxPage`,
contentDigest: createContentDigest(fieldData),
content: JSON.stringify(fieldData),
description: `Mdx implementation of the Page interface`,
},
});
createParentChildLink({ parent: node, child: getNode(mdxPageId) });
}
};
// These template are only data-fetching wrappers that import components
const pageTemplate = require.resolve(`./src/templates/page-query.tsx`);
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const result = await graphql(`
query {
allPage {
nodes {
slug
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild(
`There was an error loading your pages`,
result.errors
);
return;
}
result.data.allPage.nodes.forEach((page) => {
const breadcrumbs = page.slug.split("/").filter(Boolean);
createPage({
path: `/${page.slug}`.replace(/\/\/+/g, `/`),
component: pageTemplate,
context: {
breadcrumbs,
slug: page.slug,
slugDirectoryGlob: `/${breadcrumbs.join("/")}/*`,
},
});
});
};
exports.onCreateWebpackConfig = ({ actions, getConfig }) => {
const config = getConfig();
config.module.rules = [
// Strip out the svg files from the following built-in rule
// See https://github.com/zabute/gatsby-plugin-svgr/blob/master/gatsby-node.js
...config.module.rules.map((rule) => {
// Gatsby < 2.30 (no AVIF support)
if (
String(rule.test) ===
String(/\.(ico|svg|jpg|jpeg|png|gif|webp)(\?.*)?$/)
) {
return {
...rule,
test: /\.(ico|jpg|jpeg|png|gif|webp)(\?.*)?$/,
};
}
// Gatsby ≥ 2.30 (AVIF support)
if (
String(rule.test) ===
String(/\.(ico|svg|jpg|jpeg|png|gif|webp|avif)(\?.*)?$/)
) {
return {
...rule,
test: /\.(ico|jpg|jpeg|png|gif|webp|avif)(\?.*)?$/,
};
}
return rule;
}),
{
test: /\.svg$/,
include: /files\/svg/,
use: [
{
loader: require.resolve("@svgr/webpack"),
options: {
// NOTE: disable this and manually add `removeViewBox: false` in the SVGO plugins list
// See related PR: https://github.com/smooth-code/svgr/pull/137
icon: false,
svgoConfig: {
plugins: [{ removeViewBox: false }, { cleanupIDs: true }],
},
},
},
],
},
];
config.resolve = {
...config.resolve,
// Add support for absolute imports
modules: [path.resolve(__dirname, "src"), "node_modules"],
};
// This will completely replace the webpack config with the modified object.
actions.replaceWebpackConfig(config);
};