-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathnormalize.js
More file actions
340 lines (280 loc) · 10.1 KB
/
normalize.js
File metadata and controls
340 lines (280 loc) · 10.1 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*eslint no-undef: "error"*/
import _ from "lodash";
import { getContentTypeSchema, makeParentNodeName } from "./helpers";
/**
* Create a child node for json fields
* @param {Object} json value
* @param {Object} ctx
* @returns {Object} gatsby node
*/
const prepareJSONNode = (json, context) => {
const { createContentDigest, createNodeId, parentNode, attributeName } = context;
// Use parentNode.id instead of strapi_document_id_or_regular_id to ensure uniqueness across locales
const jsonNodeId = createNodeId(`${parentNode.id}-${attributeName}-JSONNode`);
const JSONNode = {
...(_.isPlainObject(json) ? { ...json } : { strapi_json_value: json }),
id: jsonNodeId,
parent: parentNode.id,
children: [],
internal: {
type: _.toUpper(`${parentNode.internal.type}_${attributeName}_JSONNode`),
mediaType: `application/json`,
content: JSON.stringify(json),
contentDigest: createContentDigest(json),
},
};
return JSONNode;
};
/**
* Create a child node for relation and link the parent node to it
* @param {Object} relation
* @param {Object} ctx
* @returns {Object} gatsby node
*/
const prepareRelationNode = (relation, context) => {
const { schemas, createNodeId, createContentDigest, parentNode, targetSchemaUid } = context;
// const targetSchema = getContentTypeSchema(schemas, targetSchemaUid);
// const {
// schema: { singularName },
// } = targetSchema;
const nodeType = makeParentNodeName(schemas, targetSchemaUid);
const strapi_document_id_or_regular_id = relation.documentId || relation.id; // support both v5 and v4
// Include locale in node ID to support multiple locales for the same entity
const localeIdentifier = relation.locale ? `-${relation.locale}` : "";
const relationNodeId = createNodeId(
`${nodeType}-${strapi_document_id_or_regular_id}${localeIdentifier}`,
);
const node = {
...relation,
id: relationNodeId,
documentId: relation.documentId,
strapi_id: relation.id,
strapi_document_id_or_regular_id,
parent: parentNode.id,
children: [],
internal: {
type: nodeType,
content: JSON.stringify(relation),
contentDigest: createContentDigest(relation),
},
};
return node;
};
/**
* Create a child node for markdown fields
* @param {String} text value
* @param {Object} ctx
* @returns {Object} gatsby node
*/
const prepareTextNode = (text, context) => {
const { createContentDigest, createNodeId, parentNode, attributeName } = context;
// Use parentNode.id instead of strapi_document_id_or_regular_id to ensure uniqueness across locales
const textNodeId = createNodeId(`${parentNode.id}-${attributeName}-TextNode`);
const textNode = {
id: textNodeId,
parent: parentNode.id,
children: [],
[attributeName]: text,
internal: {
type: _.toUpper(`${parentNode.internal.type}_${attributeName}_TextNode`),
mediaType: `text/markdown`,
content: text,
contentDigest: createContentDigest(text),
},
};
return textNode;
};
/**
* Create a child node for media and link the parent node to it
* @param {Object} media
* @param {Object} ctx
* @returns {Object} gatsby node
*/
const prepareMediaNode = (media, context) => {
const { createNodeId, createContentDigest, parentNode } = context;
const nodeType = "STRAPI__MEDIA";
const relationNodeId = createNodeId(`${nodeType}-${media.id}`);
const node = {
...media,
id: relationNodeId,
strapi_id: media.id,
strapi_document_id_or_regular_id: media.id,
parent: parentNode.id,
children: [],
internal: {
type: nodeType,
content: JSON.stringify(media),
contentDigest: createContentDigest(media),
},
};
return node;
};
/**
* Returns an array of the main node and children nodes to create
* @param {Object} entity the main entry
* @param {String} nodeType the name of the main node
* @param {Object} ctx object of gatsby functions
* @param {String} uid the main schema uid
* @returns {Object[]} array of nodes to create
*/
export const createNodes = (entity, context, uid) => {
const nodes = [];
const { schemas, createNodeId, createContentDigest, getNode } = context;
const nodeType = makeParentNodeName(schemas, uid);
// f.e. components do not have a documentId, allow regular id
// also, support both v5 documentId and v4 id
const strapi_document_id_or_regular_id = entity.documentId || entity.id;
// Include locale in node ID to support multiple locales for the same entity
const localeIdentifier = entity.locale ? `-${entity.locale}` : "";
let entryNode = {
id: createNodeId(`${nodeType}-${strapi_document_id_or_regular_id}${localeIdentifier}`),
documentId: entity.documentId,
strapi_id: entity.id,
strapi_document_id_or_regular_id,
parent: undefined,
children: [],
internal: {
type: nodeType,
content: JSON.stringify(entity),
contentDigest: createContentDigest(entity),
},
};
const schema = getContentTypeSchema(schemas, uid);
for (const attributeName of Object.keys(entity)) {
const value = entity[attributeName];
const attribute = schema.schema.attributes[attributeName];
const type = _.get(attribute, "type");
if (value) {
// Add support for dynamic zones
if (type === "dynamiczone") {
for (const v of value) {
const componentNodeName = makeParentNodeName(schemas, v.strapi_component);
const valueNodes = createNodes(v, context, v.strapi_component).flat();
const compoNodeIds = valueNodes
.filter(({ internal }) => internal.type === componentNodeName)
.map(({ id }) => id);
entity[`${attributeName}___NODE`] = [
...(entity[`${attributeName}___NODE`] || []),
...compoNodeIds,
];
for (const n of valueNodes) {
nodes.push(n);
}
}
delete entity[attributeName];
}
if (type === "relation") {
// Create type for the first level of relations, otherwise the user should fetch the other content type
// to link them
const config = {
schemas,
createContentDigest,
createNodeId,
parentNode: entryNode,
attributeName,
targetSchemaUid: attribute.target,
};
if (Array.isArray(value)) {
const relationNodes = value.map((relation) => prepareRelationNode(relation, config));
entity[`${attributeName}___NODE`] = relationNodes.map(({ id }) => id);
for (const node of relationNodes) {
if (!getNode(node.id)) {
nodes.push(node);
}
}
} else {
const relationNode = prepareRelationNode(value, config);
entity[`${attributeName}___NODE`] = relationNode.id;
const relationNodeToCreate = getNode(relationNode.id);
if (!relationNodeToCreate) {
nodes.push(relationNode);
}
}
delete entity[attributeName];
}
// Apply transformations to components: markdown, json...
if (type === "component") {
const componentSchema = getContentTypeSchema(schemas, attribute.component);
const componentNodeName = makeParentNodeName(schemas, componentSchema.uid);
if (attribute.repeatable) {
const compoNodes = value.flatMap((v) => createNodes(v, context, attribute.component));
// Just link the component nodes and not the components' children one
entity[`${attributeName}___NODE`] = compoNodes
.filter(({ internal }) => internal.type === componentNodeName)
.map(({ id }) => id);
for (const node of compoNodes) {
nodes.push(node);
}
} else {
const compoNodes = createNodes(value, context, attribute.component).flat();
// Just link the component node and not the component's children one
entity[`${attributeName}___NODE`] = compoNodes.find(
({ internal }) => internal.type === componentNodeName,
).id;
for (const node of compoNodes) {
nodes.push(node);
}
}
delete entity[attributeName];
}
// Create nodes for richtext in order to make the markdown-remark plugin works
if (type === "richtext") {
const textNode = prepareTextNode(value.data, {
createContentDigest,
createNodeId,
parentNode: entryNode,
attributeName,
});
entity[attributeName][`data___NODE`] = textNode.id;
delete entity[attributeName].data;
nodes.push(textNode);
}
// Create nodes for JSON fields in order to be able to query each field in GraphiQL
// We can remove this if not pertinent
if (type === "json") {
const JSONNode = prepareJSONNode(value, {
createContentDigest,
createNodeId,
parentNode: entryNode,
attributeName,
});
entryNode.children = [...entryNode.children, JSONNode.id];
entity[`${attributeName}___NODE`] = JSONNode.id;
// Resolve only the attributeName___NODE and not to both ones
delete entity[attributeName];
nodes.push(JSONNode);
}
if (type == "media") {
const config = {
createContentDigest,
createNodeId,
parentNode: entryNode,
};
if (Array.isArray(value)) {
const mediaNodes = value.map((relation) => prepareMediaNode(relation, config));
entity[`${attributeName}___NODE`] = mediaNodes.map(({ id }) => id);
for (const node of mediaNodes) {
if (!getNode(node.id)) {
nodes.push(node);
}
}
} else {
const mediaNode = prepareMediaNode(value, config);
entity[`${attributeName}___NODE`] = mediaNode.id;
const relationNodeToCreate = getNode(mediaNode.id);
if (!relationNodeToCreate) {
nodes.push(mediaNode);
}
}
// Resolve only the attributeName___NODE and not to both ones
delete entity[attributeName];
}
}
}
entryNode = {
...entity,
...entryNode,
};
nodes.push(entryNode);
return nodes;
};