-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
205 lines (177 loc) · 4.21 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
// based on gatsby-theme-blog
const fs = require(`fs`)
const path = require(`path`)
const mkdirp = require(`mkdirp`)
const Debug = require(`debug`)
const pkg = require('./package.json')
const debug = Debug(pkg.name)
let basePath
let contentPath
const DeckTemplate = require.resolve(`./src/templates/deck`)
const DecksTemplate = require.resolve(`./src/templates/decks`)
exports.onPreBootstrap = ({ store }, opts = {}) => {
const { program } = store.getState()
basePath = opts.basePath || `/`
contentPath = opts.contentPath || `decks`
if (opts.cli) return
const dirname = path.join(program.directory, contentPath)
mkdirp.sync(dirname)
debug(`Initializing ${dirname} directory`)
}
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
}
const resolveTitle = async (...args) => {
const headings = await mdxResolverPassthrough('headings')(...args)
const [first = {}] = headings
return first.value || ''
}
exports.sourceNodes = ({ actions, schema }) => {
const { createTypes } = actions
createTypes(
schema.buildObjectType({
name: `Deck`,
fields: {
id: { type: `ID!` },
slug: {
type: `String!`,
},
title: {
type: `String!`,
resolve: resolveTitle,
},
body: {
type: `String!`,
resolve: mdxResolverPassthrough(`body`),
},
},
interfaces: [`Node`],
})
)
}
exports.createPages = async ({ graphql, actions, reporter, pathPrefix }) => {
const { createPage } = actions
const result = await graphql(`
{
allDeck {
edges {
node {
id
slug
title
}
}
}
}
`)
if (result.errors) {
reporter.panic(result.errors)
}
const { allDeck } = result.data
const decks = allDeck.edges
// single deck mode
if (decks.length === 1) {
const [deck] = decks
const base = basePath === '/' ? '' : basePath
const matchPath = [base, '*'].join('/')
const slug = [pathPrefix, base].filter(Boolean).join('')
createPage({
path: basePath,
matchPath,
component: DeckTemplate,
context: {
...deck.node,
slug,
},
})
createPage({
path: base + '/print',
component: DeckTemplate,
context: {
...deck.node,
slug,
},
})
return
}
decks.forEach(({ node }, index) => {
const matchPath = [node.slug, '*'].join('/')
const slug = [pathPrefix, node.slug].filter(Boolean).join('')
createPage({
path: node.slug,
matchPath,
component: DeckTemplate,
context: {
...node,
slug,
},
})
createPage({
path: slug + '/print',
component: DeckTemplate,
context: {
...node,
slug,
},
})
})
// index page
createPage({
path: basePath,
component: DecksTemplate,
context: {
decks,
},
})
}
exports.onCreateNode = ({
node,
actions,
getNode,
createNodeId,
createContentDigest,
}) => {
const { createNode, createParentChildLink } = actions
const toPath = node => {
const { dir } = path.posix.parse(node.relativePath)
return path.posix.join(basePath, dir, node.name)
}
if (node.internal.type !== `Mdx`) return
const fileNode = getNode(node.parent)
const source = fileNode.sourceInstanceName
if (node.internal.type !== `Mdx` || source !== contentPath) return
const slug = toPath(fileNode)
createNode({
slug,
// Required fields.
id: createNodeId(`${node.id} >>> Deck`),
parent: node.id,
children: [],
internal: {
type: `Deck`,
contentDigest: createContentDigest(node.rawBody),
content: node.rawBody,
description: `Slide Decks`,
},
})
createParentChildLink({ parent: fileNode, child: node })
}
exports.onCreateDevServer = ({ app }) => {
if (typeof process.send !== 'function') return
process.send({
mdxDeck: true,
})
}