-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
236 lines (193 loc) · 9.8 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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.sourceNodes = undefined;
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _stringify = require('babel-runtime/core-js/json/stringify');
var _stringify2 = _interopRequireDefault(_stringify);
var _assign = require('babel-runtime/core-js/object/assign');
var _assign2 = _interopRequireDefault(_assign);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _typeof2 = require('babel-runtime/helpers/typeof');
var _typeof3 = _interopRequireDefault(_typeof2);
var _keys = require('babel-runtime/core-js/object/keys');
var _keys2 = _interopRequireDefault(_keys);
var _prismicDom = require('prismic-dom');
var _prismicDom2 = _interopRequireDefault(_prismicDom);
var _gatsbyNodeHelpers = require('gatsby-node-helpers');
var _gatsbyNodeHelpers2 = _interopRequireDefault(_gatsbyNodeHelpers);
var _pipe = require('lodash/fp/pipe');
var _pipe2 = _interopRequireDefault(_pipe);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var _fetch = require('./fetch');
var _fetch2 = _interopRequireDefault(_fetch);
var _getPath = require('./getPath');
var _getPath2 = _interopRequireDefault(_getPath);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _createNodeHelpers = (0, _gatsbyNodeHelpers2.default)({ typePrefix: 'Prismic' }),
createNodeFactory = _createNodeHelpers.createNodeFactory;
function clear(data) {
if (data === undefined || data === null) {
return data;
}
(0, _keys2.default)(data).forEach(function (key) {
if (Array.isArray(data[key])) {
data[key] = [];
} else if ((0, _typeof3.default)(data[key]) === "object") {
data[key] = clear(data[key]);
} else {
data[key] = undefined;
}
});
return data;
}
function checkForType(doc) {
(0, _keys2.default)(doc).forEach(function (key) {
if (_lodash2.default.findKey(doc[key], "type") && key !== "body") {
doc[key + "_first"] = doc[key][0].text;
doc[key + "_html"] = _prismicDom2.default.RichText.asHtml(doc[key], {}, htmlSerializer);
}
if (key === "body") {
doc[key].forEach(function (item) {
doc[key]["primary"] = checkForType(item.primary);
if (doc[key]["items"] !== undefined) {
doc[key]["items"].forEach(function (single, i) {
doc[key]["items"][i] = checkForType(doc[key]["items"][i]);
});
}
});
}
});
//console.log(doc.data)
return doc;
}
var sourceNodes = exports.sourceNodes = function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(_ref2, _ref3) {
var createNode = _ref2.boundActionCreators.createNode;
var repositoryName = _ref3.repositoryName,
accessToken = _ref3.accessToken;
var _ref4, documents, createNodeFunction, template, preparedDocs;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return (0, _fetch2.default)({ repositoryName: repositoryName, accessToken: accessToken });
case 2:
_ref4 = _context.sent;
documents = _ref4.documents;
createNodeFunction = {};
documents.forEach(function (doc) {
if (!_lodash2.default.findKey(createNodeFunction, doc.type)) {
createNodeFunction[doc.type] = createNodeFactory(doc.type + "Document");
}
});
template = {};
documents.forEach(function (doc) {
if (template[doc.type] === undefined) {
template[doc.type] = {};
}
template[doc.type] = (0, _assign2.default)(template[doc.type], doc);
});
(0, _keys2.default)(template).forEach(function (key) {
var emptyData = JSON.parse((0, _stringify2.default)(template[key]));
template[key] = clear(emptyData);
});
preparedDocs = [];
documents.forEach(function (doc) {
/*Object.keys(doc.data).forEach(key => {
if(_.findKey(doc.data[key], "type")) {
doc.data[key+"_first"] = doc.data[key][0].text
doc.data[key+"_html"] = PrismicDOM.RichText.asHtml(doc.data[key], {}, htmlSerializer)
}
})*/
doc.data = checkForType(doc.data);
var mergedDoc = (0, _assign2.default)(template[doc.type], doc);
var newDoc = createNodeFunction[doc.type](mergedDoc);
newDoc.id = doc.id;
preparedDocs.push(newDoc);
});
preparedDocs.forEach(function (doc) {
doc.alternate_languages.forEach(function (single, index) {
var found = _lodash2.default.find(preparedDocs, function (o) {
return o.id === single.id;
});
doc.alternate_languages[index].uid = found.uid;
});
(0, _keys2.default)(doc.data).forEach(function (key) {
if (doc.data[key].id !== undefined) {
var found = _lodash2.default.find(preparedDocs, function (o) {
return o.id === doc.data[key]["id"];
});
doc.data[key]["linkTo"] = found.data;
}
});
createNode(doc);
});
case 12:
case 'end':
return _context.stop();
}
}
}, _callee, undefined);
}));
return function sourceNodes(_x, _x2) {
return _ref.apply(this, arguments);
};
}();
var Elements = _prismicDom2.default.RichText.Elements;
var htmlSerializer = function htmlSerializer(element, content, children) {
switch (element.type) {
case Elements.heading1:
return '<h1>' + children.join('') + '</h1>';
case Elements.heading2:
return '<h2>' + children.join('') + '</h2>';
case Elements.heading3:
return '<h3>' + children.join('') + '</h3>';
case Elements.heading4:
return '<h4>' + children.join('') + '</h4>';
case Elements.heading5:
return '<h5>' + children.join('') + '</h5>';
case Elements.heading6:
return '<h6>' + children.join('') + '</h6>';
case Elements.paragraph:
return '<p>' + children.join('') + '</p>';
case Elements.preformatted:
return '<pre>' + children.join('') + '</pre>';
case Elements.strong:
return '<strong>' + children.join('') + '</strong>';
case Elements.em:
return '<em>' + children.join('') + '</em>';
case Elements.listItem:
return '<li>' + children.join('') + '</li>';
case Elements.oListItem:
return '<li>' + children.join('') + '</li>';
case Elements.list:
return '<ul>' + children.join('') + '</ul>';
case Elements.oList:
return '<ol>' + children.join('') + '</ol>';
case Elements.image:
var linkUrl = element.linkTo ? _prismicDom2.default.Link.url(element.linkTo, module.exports.linkResolver) : null;
var linkTarget = element.linkTo && element.linkTo.target ? 'target="' + element.linkTo.target + '" rel="noopener"' : '';
var wrapperClassList = [element.label || '', 'block-img'];
var img = '<img src="' + element.url + '" alt="' + (element.alt || '') + '" copyright="' + (element.copyright || '') + '">';
return '\n <p class="' + wrapperClassList.join(' ') + '">\n ' + (linkUrl ? '<a ' + linkTarget + ' href="' + linkUrl + '">' + img + '</a>' : img) + '\n </p>\n ';
case Elements.embed:
return '\n <div data-oembed="' + element.oembed.embed_url + '"\n data-oembed-type="' + element.oembed.type + '"\n data-oembed-provider="' + element.oembed.provider_name + '"\n >\n ' + element.oembed.html + '\n </div>\n ';
case Elements.hyperlink:
var target = element.data.target ? 'target="' + element.data.target + '" rel="noopener"' : '';
var linkUrl = _prismicDom2.default.Link.url(element.data, module.exports.linkResolver);
return '<a ' + target + ' href="' + linkUrl + '">' + children.join('') + '</a>';
case Elements.label:
var label = element.data.label ? ' class="' + element.data.label + '"' : '';
return '<span ' + label + '>' + children.join('') + '</span>';
case Elements.span:
return content ? content.replace(/\n/g, "<br />") : '';
default:
return null;
}
};