-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
60 lines (50 loc) · 1.78 KB
/
index.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
const { pascalize } = require('humps');
const { camelize } = require('datocms-client');
const { localizedRead } = require('datocms-client');
const buildNode = require('../utils/buildNode');
module.exports = function buildItemNode(entity, { generateType }) {
const type = generateType(`${pascalize(entity.itemType.apiKey)}`);
const itemNode = buildNode(type, entity.id, node => {
node.entityPayload = entity.payload;
});
const nodesForItemFieldsGeneratingMarkdown = entity.itemType.fields
.filter(field => field.fieldType === 'text')
.flatMap(field => {
const camelizedApiKey = camelize(field.apiKey);
const mediaType =
field.appearance.editor === 'markdown'
? 'text/markdown'
: field.appearance.editor === 'wysiwyg'
? 'text/html'
: 'text/plain';
if (field.localized) {
const locales = Object.keys(entity[camelizedApiKey]);
const nodes = locales.map(locale =>
buildNode(
'DatoCmsTextNode',
[entity.id, locale, camelizedApiKey].join('-'),
node => {
node.internal.mediaType = mediaType;
node.internal.content = entity[camelizedApiKey][locale] || '';
node.digest = entity.meta.updatedAt;
},
),
);
return nodes;
} else {
const nodes = [
buildNode(
'DatoCmsTextNode',
[entity.id, camelizedApiKey].join('-'),
node => {
node.internal.mediaType = mediaType;
node.internal.content = entity[camelizedApiKey] || '';
node.digest = entity.meta.updatedAt;
},
),
];
return nodes;
}
});
return [itemNode].concat(nodesForItemFieldsGeneratingMarkdown);
};