-
Notifications
You must be signed in to change notification settings - Fork 22
/
gatsby-node.js
232 lines (207 loc) · 6.11 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
const fs = require('fs')
const path = require('path')
const CockpitService = require('./src/CockpitService')
const CollectionItemNodeFactory = require('./src/CollectionItemNodeFactory')
const SingletonItemNodeFactory = require('./src/SingletonItemNodeFactory')
const {
MARKDOWN_IMAGE_REGEXP_GLOBAL,
MARKDOWN_ASSET_REGEXP_GLOBAL,
TYPE_PREFIX_COCKPIT,
} = require('./src/constants')
const FileNodeFactory = require('./src/FileNodeFactory')
const MarkdownNodeFactory = require('./src/MarkdownNodeFactory')
const LayoutNodeFactory = require('./src/LayoutNodeFactory')
exports.setFieldsOnGraphQLNodeType = require('./extend-node-type')
exports.sourceNodes = async (
{ actions, reporter, cache, store },
configOptions
) => {
const { createNode, createParentChildLink } = actions
const cockpit = new CockpitService(
configOptions.baseUrl,
configOptions.token,
configOptions.locales,
configOptions.collections,
configOptions.singletons,
configOptions.aliases
)
const fileNodeFactory = new FileNodeFactory(
createNode,
store,
cache,
reporter
)
const markdownNodeFactory = new MarkdownNodeFactory(createNode)
const layoutNodeFactory = new LayoutNodeFactory(createNode)
await cockpit.validateBaseUrl()
await cockpit.validateToken()
const collections = await cockpit.getCollections()
const singletons = await cockpit.getSingletons()
validateNodeNames(collections, singletons)
const nodes = [...collections, ...singletons]
const { images, assets, markdowns, layouts } = cockpit.normalizeResources(
nodes
)
cache.set(TYPE_PREFIX_COCKPIT, nodes)
const brokenImageReplacement = await createBrokenImagePlaceholder(
fileNodeFactory,
configOptions.brokenImageReplacement
)
for (let path in images) {
const imageNode = await fileNodeFactory.createImageNode(path)
if (imageNode) {
images[path] = {
localPath: copyFileToStaticFolder(imageNode),
id: imageNode.id,
}
} else if (brokenImageReplacement) {
reporter.info('Using broken image replacement for missing image', path)
images[path] = brokenImageReplacement
}
}
for (let path in assets) {
const assetNode = await fileNodeFactory.createAssetNode(path)
assets[path] = {
localPath: copyFileToStaticFolder(assetNode),
id: assetNode.id,
}
}
for (let markdown in markdowns) {
const localMarkdown = updateAssetPathsWithLocalPaths(
updateImagePathsWithLocalPaths(markdown, images),
assets
)
const id = markdownNodeFactory.create(localMarkdown)
markdowns[markdown] = { id }
}
for (let layout in layouts) {
const id = layoutNodeFactory.create(layouts[layout])
layouts[layout] = { id }
}
collections.forEach(collection => {
const nodeFactory = new CollectionItemNodeFactory(
createNode,
createParentChildLink,
collection.name,
images,
assets,
markdowns,
layouts
)
collection.items.forEach(item => {
nodeFactory.create(item)
})
})
singletons.forEach(singleton => {
const nodeFactory = new SingletonItemNodeFactory(
createNode,
singleton.name,
images,
assets,
markdowns,
layouts
)
singleton.items.forEach(item => {
nodeFactory.create(item)
})
})
}
const copyFileToStaticFolder = ({ absolutePath, name, ext, internal }) => {
const localPath = path.join(
'/',
'static',
`${name}-${internal.contentDigest}${ext}`
)
fs.copyFileSync(absolutePath, path.join(process.cwd(), 'public', localPath))
return localPath
}
const updateImagePathsWithLocalPaths = (markdown, images) => {
return markdown.replace(MARKDOWN_IMAGE_REGEXP_GLOBAL, (...match) =>
match[0].replace(match[1], images[match[1]].localPath)
)
}
const updateAssetPathsWithLocalPaths = (markdown, assets) => {
return markdown.replace(MARKDOWN_ASSET_REGEXP_GLOBAL, (...match) =>
assets[match[1]]
? match[0].replace(match[1], assets[match[1]].localPath)
: match[0]
)
}
const createBrokenImagePlaceholder = async (
fileNodeFactory,
placeholderURL
) => {
const brokenImageReplacementURL = placeholderURL || null
if (brokenImageReplacementURL) {
const brokenImageReplacementNode = await fileNodeFactory.createImageNode(
brokenImageReplacementURL
)
if (brokenImageReplacementNode) {
const localPath = copyFileToStaticFolder(brokenImageReplacementNode)
return {
localPath: localPath,
id: brokenImageReplacementNode.id,
}
}
}
return null
}
const validateNodeNames = (collections, singletons) => {
const collisions = Object.values(
collections
.map(collection => ({
type: 'collection',
name: collection.name,
}))
.concat(
singletons.map(singleton => ({
type: 'singleton',
name: singleton.name,
}))
)
.reduce((accumulator, node) => {
const key = node.name[0].toUpperCase() + node.name.substring(1)
if (accumulator[key]) {
accumulator[key].push(node)
} else {
accumulator[key] = [node]
}
return accumulator
}, {})
)
.filter(association => association.length > 1)
.map(association =>
association.reduce(
(accumulator, node) =>
accumulator +
(accumulator !== '' ? ' & ' : '') +
`${node.name} (${node.type})`,
''
)
)
if (collisions.length > 0) {
throw new Error(
`Some collections or singletons names are colliding,
you must provide aliases for some of them in the plugin's configuration.` +
'\n' +
`An example for a collection and a singleton both named "team" would be:
options: {
…
aliases: {
singleton: {
team: 'Team', // 'team' would be valid too
},
collection: {
team: 'Teams', // 'teams' would be valid too
}
}
}` +
'\n' +
'The colliding names are:\n' +
collisions.reduce(
(accumulator, collision) => accumulator + '- ' + collision + '\n',
''
)
)
}
}