-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
174 lines (160 loc) · 4.07 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
const path = require('path');
const { createFilePath, createRemoteFileNode } = require('gatsby-source-filesystem');
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
ignoreWarnings: [
{
module: /chevrotain/ // this is ussed by @react-three/drei i think!
}
]
});
};
exports.createSchemaCustomization = async ({ actions: { createTypes } }) => {
createTypes(`
type Mdx implements Node {
frontmatter: Frontmatter
featuredImage: File @link(from: "fields.featuredImage")
embeddedImages: [File] @link(from: "fields.embeddedImages")
logo: File @link(from: "fields.logo")
}
type Frontmatter @dontInfer {
type: String
title: String
name: String
icon: String
tags: [String]
url: String
date: Date @dateformat(formatString: "MMMM DD, YYYY")
dateModified: Date @dateformat(formatString: "MMMM DD, YYYY")
author: String
category: String
role: String
publication: String
status: String
isPrivate: Boolean
pinned: Boolean
logo: String
featuredImage: String
embeddedImages: [String]
}
`);
// Logs out all typeDefs
// actions.printTypeDefinitions({ path: './typeDefs.txt' })
};
exports.sourceNodes = ({ actions: { createNode }, createNodeId, createContentDigest }) => {
const packageJson = require('./package.json');
createNode({
...packageJson,
id: createNodeId(packageJson.version),
internal: {
type: 'packageJson',
contentDigest: createContentDigest(packageJson)
}
});
};
exports.onCreateNode = async ({
node,
actions: { createNodeField, createNode },
getNode,
store,
cache,
createNodeId
}) => {
if (node.internal.type === 'Mdx' || node.internal.type === 'PagesJson') {
const path = createFilePath({ node, getNode });
const {
frontmatter: { type }
} = node;
await createNodeField({
node,
name: 'slug',
value: type === 'page' ? path : `/${type}s${path}`
});
}
if (node.internal.type === 'Mdx') {
if (node.frontmatter.featuredImage) {
let featuredImage = await createRemoteFileNode({
url: node.frontmatter.featuredImage,
parentNodeId: node.id,
createNode,
createNodeId,
cache,
store
});
if (featuredImage) {
createNodeField({ node, name: 'featuredImage', value: featuredImage.id });
}
}
if (node.frontmatter.embeddedImages) {
let embeddedImages = await Promise.all(
node.frontmatter.embeddedImages.map((url) => {
return createRemoteFileNode({
url,
parentNodeId: node.id,
createNode,
createNodeId,
cache,
store
});
})
);
if (embeddedImages) {
createNodeField({
node,
name: 'embeddedImages',
value: embeddedImages.map((embeddedImage) => {
return embeddedImage.id;
})
});
}
}
if (node.frontmatter.logo) {
let logo = await createRemoteFileNode({
url: node.frontmatter.logo,
parentNodeId: node.id,
createNode,
createNodeId,
cache,
store
});
if (logo) {
createNodeField({ node, name: 'logo', value: logo.id });
}
}
}
};
exports.createPages = async ({ graphql, actions: { createPage, createRedirect } }) => {
const {
data: { allMdx }
} = await graphql(`
query {
allMdx(filter: { frontmatter: { status: { ne: "draft" } } }) {
nodes {
id
fields {
slug
}
frontmatter {
type
}
}
}
}
`);
allMdx.nodes.forEach((node) => {
const {
id,
fields: { slug },
frontmatter: { type }
} = node;
createPage({
path: slug,
component: path.join(__dirname, `./src/templates/${type}.js`),
context: {
id: id
},
defer: type !== 'blog' || type !== 'project' ? false : true
// defer: index + 1 > 50
});
});
};