-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
gatsby-node.js
229 lines (197 loc) · 6.52 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
/****
* gatsby-node.js
*
* Generate Gatsby nodes based on a custom schema derived from the Ghost V3 API spec.
*
* This source plugin will source and generate Posts, Pages, Tags, Authors and Settings.
*
* https://ghost.org/docs/api/v3/
*/
const ContentAPI = require('./content-api');
const {
PostNode,
PageNode,
TagNode,
AuthorNode,
SettingsNode,
TiersNode
} = require('./ghost-nodes');
const _ = require(`lodash`);
const cheerio = require(`cheerio`);
/**
* Import all custom ghost types.
*/
const ghostTypes = require('./ghost-schema');
let fetchTiers = null;
/**
* Extract specific tags from html and return them in a new object.
*
* Only style tags are extracted at present.
*/
const parseCodeinjection = (html) => {
let $ = null;
/**
* Attempt to load the HTML into cheerio. Do not escape the HTML.
*/
try {
$ = cheerio.load(html, {decodeEntities: false});
} catch (e) {
return {};
}
/**
* Extract all style tags from the markup.
*/
const $parsedStyles = $(`style`);
const codeInjObj = {};
/**
* For each extracted tag, add or append the tag's HTML to the new object.
*/
$parsedStyles.each((i, style) => {
if (i === 0) {
codeInjObj.styles = $(style).html();
} else {
codeInjObj.styles += $(style).html();
}
});
return codeInjObj;
};
/**
* Extracts specific tags from the code injection header and footer and
* transforms posts to include extracted tags as a new key and value in the post object.
*
* Only the `codeinjection_styles` key is added at present.
*/
const transformCodeinjection = (posts) => {
posts.map((post) => {
const allCodeinjections = [
post.codeinjection_head,
post.codeinjection_foot
].join('');
if (!allCodeinjections) {
return post;
}
const headInjection = parseCodeinjection(allCodeinjections);
if (_.isEmpty(post.codeinjection_styles)) {
post.codeinjection_styles = headInjection.styles;
} else {
post.codeinjection_styles += headInjection.styles;
}
return post;
});
return posts;
};
/**
* Create Live Ghost Nodes
* Uses the Ghost Content API to fetch all posts, pages, tags, authors and settings
* Creates nodes for each record, so that they are all available to Gatsby
*/
exports.sourceNodes = ({actions}, configOptions) => {
const {createNode} = actions;
let {apiUrl, contentApiKey, version = `v5.0`, postAndPageFetchCustomOptions = {}} = configOptions;
if (version.match(/^v\d$/)) {
version = version.replace(/^(v\d)$/, '$1.0');
}
const api = ContentAPI.configure({apiUrl, contentApiKey, version});
const ignoreNotFoundElseRethrow = (err) => {
if (err && err.response && err.response.status !== 404) {
throw err;
}
};
const postAndPageFetchOptions = {
limit: 'all',
include: 'tags,authors',
formats: 'html,plaintext',
...postAndPageFetchCustomOptions
};
const fetchPosts = api.posts
.browse(postAndPageFetchOptions)
.then((posts) => {
posts = transformCodeinjection(posts);
posts.forEach(post => createNode(PostNode(post)));
}).catch(ignoreNotFoundElseRethrow);
const fetchPages = api.pages
.browse(postAndPageFetchOptions)
.then((pages) => {
pages.forEach(page => createNode(PageNode(page)));
}).catch(ignoreNotFoundElseRethrow);
const tagAndAuthorFetchOptions = {
limit: 'all',
include: 'count.posts'
};
const fetchTags = api.tags
.browse(tagAndAuthorFetchOptions)
.then((tags) => {
tags.forEach((tag) => {
tag.postCount = tag.count.posts;
createNode(TagNode(tag));
});
}).catch(ignoreNotFoundElseRethrow);
const fetchAuthors = api.authors
.browse(tagAndAuthorFetchOptions)
.then((authors) => {
authors.forEach((author) => {
author.postCount = author.count.posts;
createNode(AuthorNode(author));
});
}).catch(ignoreNotFoundElseRethrow);
const fetchSettings = api.settings.browse().then((setting) => {
/**
* Assert the presence of any code injections, from both the use and ghost.
*/
const codeinjectionHead =
setting.codeinjection_head || setting.ghost_head;
const codeinjectionFoot =
setting.codeinjection_foot || setting.ghost_foot;
const allCodeinjections = codeinjectionHead
? codeinjectionHead.concat(codeinjectionFoot)
: codeinjectionFoot
? codeinjectionFoot
: null;
/**
* If there are any code injections, extract style tags from the markup and
* transform the setting object to include the `codeinjection_styles` key with the value of those style tags.
*/
if (allCodeinjections) {
const parsedCodeinjections = parseCodeinjection(allCodeinjections);
if (_.isEmpty(setting.codeinjection_styles)) {
setting.codeinjection_styles = parsedCodeinjections.styles;
} else {
setting.codeinjection_styles += parsedCodeinjections.styles;
}
}
/**
* Ensure always non-null by setting `codeinjection_styles` to
* an empty string instead of null.
*/
setting.codeinjection_styles = _.isNil(setting.codeinjection_styles)
? ''
: setting.codeinjection_styles;
// The settings object doesn't have an id, prevent Gatsby from getting 'undefined'
setting.id = 1;
createNode(SettingsNode(setting));
}).catch(ignoreNotFoundElseRethrow);
if (version.match(/^v5\.\d/)) {
api.tiers
.browse({include: 'benefits,monthly_price,yearly_price'})
.then((tiers) => {
tiers.forEach(tier => createNode(TiersNode(tier)));
}).catch(ignoreNotFoundElseRethrow);
}
return Promise.all([
fetchPosts,
fetchPages,
fetchTags,
fetchAuthors,
fetchSettings,
fetchTiers
]);
};
/**
* Creates custom types based on the Ghost V3 API.
*
* This creates a fully custom schema, removing the need for dummy content or fake nodes.
*/
exports.createSchemaCustomization = ({actions}) => {
const {createTypes} = actions;
createTypes(ghostTypes);
};