forked from explosion/spaCy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
290 lines (270 loc) · 11.7 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const DEFAULT_TEMPLATE = path.resolve('./src/templates/index.js')
const BASE_PATH = 'docs'
const PAGE_EXTENSIONS = ['.md', '.mdx']
function replacePath(pagePath) {
return pagePath === `/` ? pagePath : pagePath.replace(/\/$/, ``)
}
function getNodeTitle({ childMdx }) {
const frontmatter = (childMdx || {}).frontmatter || {}
return (frontmatter.title || '').replace("'", '’')
}
function findNode(pages, slug) {
return slug ? pages.find(({ node }) => node.fields.slug === slug) : null
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
resolve(
graphql(
`
{
site {
siteMetadata {
sections {
id
title
theme
}
languages {
code
name
models
example
has_examples
}
universe {
resources {
id
title
slogan
}
categories {
label
items {
id
title
description
}
}
}
}
}
allFile(filter: { ext: { in: [".md", ".mdx"] } }) {
edges {
node {
base
ext
name
relativeDirectory
absolutePath
childMdx {
code {
scope
}
frontmatter {
title
teaser
source
api_base_class
api_string_name
api_trainable
tag
new
next
search_exclude
menu
sidebar {
label
items {
text
url
}
}
section
}
}
fields {
id
slug
fileName
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
reject(result.errors)
}
const sectionData = result.data.site.siteMetadata.sections
const sections = Object.assign({}, ...sectionData.map(s => ({ [s.id]: s })))
/* Regular pages */
const pages = result.data.allFile.edges
pages.forEach(page => {
const { name } = path.parse(page.node.absolutePath)
if (!name.startsWith('_')) {
const mdx = page.node.childMdx || {}
const frontmatter = mdx.frontmatter || {}
const section = frontmatter.section || page.node.relativeDirectory
const sectionMeta = sections[section] || {}
const title = getNodeTitle(page.node)
const next = findNode(pages, frontmatter.next)
const baseClass = findNode(pages, frontmatter.api_base_class)
const apiDetails = {
stringName: frontmatter.api_string_name,
baseClass: baseClass
? {
title: getNodeTitle(baseClass.node),
slug: frontmatter.api_base_class,
}
: null,
trainable: frontmatter.api_trainable,
}
createPage({
path: replacePath(page.node.fields.slug),
component: DEFAULT_TEMPLATE,
context: {
id: page.node.id,
slug: page.node.fields.slug,
isIndex: page.node.fields.fileName === 'index',
title,
section,
sectionTitle: sectionMeta.title,
menu: frontmatter.menu || [],
teaser: frontmatter.teaser,
apiDetails,
source: frontmatter.source,
sidebar: frontmatter.sidebar,
tag: frontmatter.tag,
version: frontmatter.new,
theme: sectionMeta.theme,
searchExclude: frontmatter.search_exclude,
relativePath: page.node.relativePath,
next: next
? {
title: getNodeTitle(next.node),
slug: next.node.fields.slug,
}
: null,
},
})
}
})
/* Universe */
const universeContext = {
section: 'universe',
sectionTitle: sections.universe.title,
theme: sections.universe.theme,
}
createPage({
path: '/universe',
component: DEFAULT_TEMPLATE,
context: {
slug: '/universe',
isIndex: true,
title: 'Overview',
...universeContext,
},
})
const universe = result.data.site.siteMetadata.universe.resources
universe.forEach(page => {
const slug = `/universe/project/${page.id}`
createPage({
path: slug,
component: DEFAULT_TEMPLATE,
context: {
id: page.id,
slug: slug,
isIndex: false,
title: page.title || page.id,
teaser: page.slogan,
data: { ...page, isProject: true },
...universeContext,
},
})
})
const universeCategories = result.data.site.siteMetadata.universe.categories
const categories = [].concat.apply([], universeCategories.map(cat => cat.items))
categories.forEach(page => {
const slug = `/universe/category/${page.id}`
createPage({
path: slug,
component: DEFAULT_TEMPLATE,
context: {
id: page.id,
slug: slug,
isIndex: false,
title: page.title,
teaser: page.description,
data: { ...page, isCategory: true },
...universeContext,
},
})
})
/* Models */
const langs = result.data.site.siteMetadata.languages
const modelLangs = langs.filter(({ models }) => models && models.length)
modelLangs.forEach(({ code, name, models, example, has_examples }, i) => {
const slug = `/models/${code}`
const next = i < modelLangs.length - 1 ? modelLangs[i + 1] : null
createPage({
path: slug,
component: DEFAULT_TEMPLATE,
context: {
id: code,
slug: slug,
isIndex: false,
title: name,
section: 'models',
sectionTitle: sections.models.title,
theme: sections.models.theme,
next: next ? { title: next.name, slug: `/models/${next.code}` } : null,
meta: { models, example, hasExamples: has_examples },
},
})
})
})
)
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (PAGE_EXTENSIONS.includes(node.ext)) {
const slug = createFilePath({ node, getNode, basePath: BASE_PATH, trailingSlash: false })
const { name } = path.parse(node.absolutePath)
createNodeField({ name: 'fileName', node, value: name })
createNodeField({ name: 'slug', node, value: slug })
createNodeField({ name: 'id', node, value: node.id })
}
}
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
// Support relative paths in MDX components
actions.setWebpackConfig({
resolve: {
modules: [
path.resolve(__dirname),
path.resolve(__dirname, 'docs'),
path.resolve(__dirname, 'src'),
'node_modules',
],
},
module: {
rules: [
{
test: /\.(html|svg)$/,
use: 'raw-loader',
},
],
},
})
if (stage === 'build-javascript') {
// Turn off source maps
actions.setWebpackConfig({
devtool: false,
})
}
}