-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
242 lines (210 loc) · 6.29 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
const crypto = require(`crypto`)
const path = require(`path`)
const { joinPath } = require(`gatsby-core-utils`)
const traverse = require(`traverse`)
const { find, isString } = require(`lodash`)
let basePath
let contentPath
const DocTemplate = require.resolve('./src/templates/doc')
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
}
// Adapted from gatsby-remark-relative-images
slash = (path) => {
const isExtendedLengthPath = /^\\\\\?\\/.test(path);
if (isExtendedLengthPath) {
return path;
}
return path.replace(/\\/g, `/`);
}
findMatchingFile = (src, files, options) => {
const result = find(files, (file) => {
const staticPath = slash(path.join(options.staticFolderName, src));
return slash(path.normalize(file.absolutePath)).endsWith(staticPath);
});
if (!result) {
throw new Error(
`No matching file found for src "${src}" in static folder "${options.staticFolderName}". Please check static folder name and that file exists at "${options.staticFolderName}${src}". This error will probably cause a "GraphQLDocumentError" later in build. All converted field paths MUST resolve to a matching file in the "static" folder.`
);
}
return result;
}
exports.onPreBootstrap = (_, themeOptions) => {
basePath = themeOptions.basePath || `/`
contentPath = themeOptions.contentPath || `docs`
}
exports.sourceNodes = ({ actions, schema }) => {
const { createTypes } = actions
createTypes([
`
type Frontmatter @infer {
title: String!
coverImage: File @fileByRelativePath
}
`,
schema.buildObjectType({
name: `Docs`,
fields: {
id: { type: `ID!` },
title: { type: `String!`, },
description: { type: `String`, },
frontmatter: `Frontmatter!`,
slug: { type: `String!`, },
headings: {
type: `[MdxHeadingMdx!]`,
resolve: mdxResolverPassthrough(`headings`),
},
excerpt: {
type: `String!`,
args: {
pruneLength: {
type: `Int`,
defaultValue: 140,
},
},
resolve: mdxResolverPassthrough(`excerpt`),
},
body: {
type: `String!`,
resolve: mdxResolverPassthrough(`body`),
},
},
interfaces: [`Node`],
extensions: {
infer: true,
}
})
])
}
exports.onCreateNode = async ({ node, actions, getNode, getNodesByType, createNodeId }) => {
const { createNode, createParentChildLink, createRedirect } = actions
const isReadme = name => /readme/i.test(name)
const isIndexPath = name => name === 'index' || isReadme(name)
const toOriginalDocsPath = node => {
const { dir } = path.parse(node.relativePath)
const fullPath = [
basePath,
dir,
node.name
]
return joinPath(...fullPath).replace(/\\+/g, ``)
}
const toDocsPath = node => {
const { dir } = path.parse(node.relativePath)
const fullPath = [
basePath,
dir,
!isIndexPath(node.name) && node.name
].filter(Boolean)
return joinPath(...fullPath).replace(/\\+/g, ``)
}
// Make sure it's an MDX node
if (node.internal.type !== `Mdx`) {
return
}
// Create source field (according to contentPath)
const fileNode = getNode(node.parent)
const source = fileNode.sourceInstanceName
if (node.internal.type === `Mdx` && source === contentPath) {
const slug = toDocsPath(fileNode)
// Redirect file/path/readme to file/path/ in order to handle
// potential links that are meant to work with GitHub-style index
// pages.
if (isReadme(fileNode.name)) {
createRedirect({
fromPath: toOriginalDocsPath(fileNode),
toPath: toDocsPath(fileNode),
isPermanent: true
})
}
const frontmatter = node.frontmatter
const title = frontmatter.title
const description = frontmatter.description
// Convert relative image paths frontmatter
// Code adapted from gatsby-remark-relative-images, because it only works on
// Mdx type and not the custom Docs type
const files = getNodesByType(`File`);
const directory = path.dirname(node.fileAbsolutePath);
// Deeply iterate through frontmatter data for absolute paths
traverse(frontmatter).forEach(function (value) {
if (!isString(value)) return;
if (!path.isAbsolute(value) || !path.extname(value)) return;
const paths = this.path.reduce((acc, current) => {
acc.push(acc.length > 0 ? [acc, current].join('.') : current);
return acc;
}, []);
const file = findMatchingFile(value, files, {staticFolderName: ''});
const newValue = path.relative(directory, file.absolutePath);
this.update(newValue);
});
const fieldData = { title, description, slug, frontmatter }
const mdxDocId = createNodeId(`${node.id} >>> Docs`)
await createNode({
...fieldData,
id: mdxDocId,
parent: node.id,
children: [],
internal: {
type: `Docs`,
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(fieldData))
.digest(`hex`),
content: JSON.stringify(fieldData),
description: `Docs`,
},
})
createParentChildLink({ parent: node, child: getNode(mdxDocId) })
}
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql(`
{
docs: allDocs {
nodes {
id
slug
}
}
}
`)
if (result.errors) {
reporter.panic(result.errors)
}
const docs = result.data.docs.nodes
docs.forEach((doc, index) => {
const previous = index === docs.length - 1 ? null : docs[index + 1]
const next = index === 0 ? null : docs[index - 1]
const { slug } = doc
createPage({
path: slug,
component: DocTemplate,
context: {
...doc,
previous,
next,
},
})
})
}
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
node: {
fs: 'empty'
}
})
}