-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatic.config.js
executable file
Β·394 lines (342 loc) Β· 10.5 KB
/
static.config.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import path from "path";
import jdown from "jdown";
import marked from "marked";
import { getHighlighter } from "shiki";
import { rebuildRoutes } from "react-static/node";
import chokidar from "chokidar";
import { paramCase } from "change-case";
import R from "ramda";
const contentDir = "./content";
// chokidari keval dev mode main
if (process.env.REACT_STATIC_ENV === "development") {
chokidar.watch(contentDir).on("all", () => {
try {
rebuildRoutes();
} catch (e) {
// it's okay
}
});
}
const getSitePages = (content) => {
const sitePagesObj = R.mapObjIndexed(
(val, key, obj) => ({
path: `/${val.slug}`,
template: "src/templates/Page",
getData: () => val,
}),
content.pages
);
return R.values(sitePagesObj);
};
const getAuthorPages = (content) => {
return R.map(
(a) => ({
path: `/authors/${a.slug}`,
template: "src/templates/Author",
getData: () => a,
}),
R.values(content.authors)
);
};
const stripJobContents = (job) =>
R.omit(["requirements", "responsibilities", "intro"], job);
const getPublishedJobs = (content) =>
R.filter((j) => j.isPublished, R.values(content.jobs));
const jobsWithoutContents = (content) =>
R.map((j) => stripJobContents(j), getPublishedJobs(content));
/* Takes an object and a list of keys containing markdown.
* Returns a new maps where values of the keys is converted from Markdown to Html.
*/
const convertMdKeysToHtml = (obj, keysToConvert) => {
return R.mapObjIndexed((value, key) => {
return R.contains(key, keysToConvert) ? marked.parse(value) : value;
}, obj);
};
const getJobPages = (content) => {
return R.map(
(j) => ({
path: `/jobs/${j.slug}`,
template: "src/templates/Job",
getData: () =>
convertMdKeysToHtml(j, ["requirements", "responsibilities", "intro"]),
}),
getPublishedJobs(content)
);
};
const getPublishedCampaigns = (content) =>
R.filter((j) => j.isPublished, R.values(content.campaigns));
const getCampaignPages = (content) => {
return R.map(
(c) => ({
path: `/campaigns/${c.slug}`,
template: "src/templates/Campaign",
getData: () => c,
}),
getPublishedCampaigns(content)
);
};
/*********
* Guides
*********/
const getGuideChapterPages = (content) => {
const guideChapterPagesObj = R.mapObjIndexed(
(val, key, obj) => ({
path: `/guides/${val.guideSlug}/${val.slug}`,
template: "src/templates/guide/Chapter",
getData: () => val,
}),
content.guidechapters // why no camelcasing? because jdown doesn't read camel casing on mac
);
return R.values(guideChapterPagesObj);
};
const getPublishedGuides = (content) =>
R.filter((j) => j.isPublished, R.values(content.guides));
const getGuidePages = (content) => {
const guidePagesObj = R.mapObjIndexed(
(val, key, obj) => ({
path: `/guides/${val.slug}`,
template: "src/templates/Guide",
getData: () => val,
}),
getPublishedGuides(content)
);
return R.values(guidePagesObj);
};
const generateGuideIndex = (guidePages, guideChapterPages, guideSlug) => {
const guide = R.find(R.propEq("path", `/guides/${guideSlug}`))(guidePages);
const chapters = R.filter(
(cp) => cp.getData().guideSlug === guideSlug,
guideChapterPages
);
const chaptersWithData = R.map(
(c) => ({
...R.dissoc("getData", c),
data: R.dissoc("contents", c.getData()),
}),
chapters
);
const sortedChapters = R.sortBy(
R.path(["data", "chapterNumber"]),
chaptersWithData
);
return {
guide: {
...R.dissoc("getData", guide),
data: R.dissoc("contents", guide.getData()),
},
chapters: sortedChapters,
};
};
const generateGuideIndices = (guidePages, guideChapterPages) => {
return R.reduce(
(acc, g) => {
const guideSlug = g.getData().slug;
return {
...acc,
[guideSlug]: generateGuideIndex(
guidePages,
guideChapterPages,
guideSlug
),
};
},
{},
guidePages
);
};
const injectGuideIndex = (page, index) => {
return {
...page,
getData: () => ({ ...page.getData(), index }),
};
};
/********
* Posts
********/
// need weak inequality check so objects with no `publishedOn` key return false
const isPublished = (post) => post.publishedOn != null;
const postsToPostPages = (posts) => {
return R.map(
(post) => ({
path: `/blog/${post.slug}`,
template: "src/templates/blog/Post",
data: post,
}),
posts
);
};
const relatedSlugs = (post) => R.pathOr([], ["data", "relatedSlugs"], post);
/**
- for a given list of posts and a specific post,
- find all posts related to this specific post,
- by reading the `relatedSlugs` on the post
*/
const relatedPosts = (posts, post) => {
const postRelatedSlugs = relatedSlugs(post);
return R.filter((p) => R.contains(p.data.slug, postRelatedSlugs), posts);
};
const injectRelatedPosts = (posts) => (post) =>
R.assocPath(["data", "relatedPosts"], relatedPosts(posts, post), post);
const rawDataToGetData = (post) => {
// this has to be a constant to prevent it from being passed as a value
// which causes a problem when data is stripped
const postDataCopy = R.clone(post.data);
return R.assoc("getData", () => postDataCopy, post);
};
const stripPostContents = (post) => R.dissocPath(["data", "contents"], post);
const stripRelatedPostsContent = (post) =>
R.assocPath(
["data", "relatedPosts"],
R.map(stripPostContents, post.data.relatedPosts),
post
);
const stripRelatedPosts = (post) =>
R.dissocPath(["data", "relatedPosts"], post);
const injectAuthor = (authors) => (post) => {
const author = R.find(R.propEq("slug", post.data.author))(R.values(authors));
const authorWithoutContents = R.dissoc("contents", author);
return R.assocPath(["data", "author"], authorWithoutContents, post);
};
const injectRelatedPostAuthors = (authors) => (post) => {
const relatedPosts = R.pathOr([], ["data", "relatedPosts"], post);
const withAuthors = R.map(injectAuthor(authors), relatedPosts);
return R.assocPath(["data", "relatedPosts"], withAuthors, post);
};
/**
- take an initial value of postPages and
- transform it to the required form
*/
const transformPostPages = (postPages, authors) => (post) => {
// R.compose works right to left, i.e.
// the last fn will be applied first, followed by second last until first
const transform = R.compose(
rawDataToGetData,
stripRelatedPostsContent,
injectRelatedPostAuthors(authors),
injectRelatedPosts(postPages),
injectAuthor(authors)
);
return transform(post);
};
const sortByPublishDateDesc = (postPages) => {
const dateDiff = (a, b) => {
return (
new Date(b.data.publishedOn).getTime() -
new Date(a.data.publishedOn).getTime()
);
};
return R.sort(dateDiff, postPages);
};
const byPublishYear = R.groupBy((postPage) =>
new Date(postPage.data.publishedOn).getFullYear()
);
// https://marked.js.org/using_advanced
const markdownOptionsFactory = (highlighter) => ({
highlight: (code, lang) => highlighter.codeToHtml(code, lang),
});
const topTags = (postPages, n) => {
const tags = R.compose(
R.countBy(R.identity),
R.flatten
)(R.map(({ data }) => data.tags, postPages));
const tagsList = R.zip(R.keys(tags), R.values(tags));
const sortedTagList = R.sortBy(R.prop(1))(tagsList);
const topNTags = R.takeLast(n)(sortedTagList);
return R.map(R.prop(0), topNTags);
};
export default {
// siteRoot: "https://krimlabs.com",
plugins: [
"react-static-plugin-react-router",
// "react-static-plugin-sitemap"
],
getRoutes: async () => {
const highlighter = await getHighlighter({ theme: "monokai" });
const content = await jdown(contentDir, {
markdown: markdownOptionsFactory(highlighter),
});
// posts
const publishedPosts = R.filter(isPublished, R.values(content.posts));
const rawPostPages = postsToPostPages(publishedPosts);
const txfmFn = transformPostPages(rawPostPages, content.authors);
const postPages = R.map(txfmFn, rawPostPages);
const sortedPostPages = sortByPublishDateDesc(postPages);
const allPosts = R.map(
R.compose(stripPostContents, stripRelatedPosts),
sortedPostPages
);
const allPostsByYear = byPublishYear(allPosts);
const recentPosts = R.take(4, allPosts);
//
const sitePages = getSitePages(content);
const authorPages = getAuthorPages(content);
const jobPages = getJobPages(content);
const campaignPages = getCampaignPages(content);
const guidePagesWithoutIndex = getGuidePages(content);
const guideChapterPagesWithoutIndex = getGuideChapterPages(content);
const guideIndices = generateGuideIndices(
guidePagesWithoutIndex,
guideChapterPagesWithoutIndex
);
const guidePages = R.map(
(g) => injectGuideIndex(g, guideIndices[g.getData().slug]),
guidePagesWithoutIndex
);
const guideChapterPages = R.map(
(gc) => injectGuideIndex(gc, guideIndices[gc.getData().guideSlug]),
guideChapterPagesWithoutIndex
);
return [
{
path: "/",
template: "src/templates/Landing.js",
getData: () => ({
recentPosts,
jobs: jobsWithoutContents(content),
}),
},
{
path: "/gleam-thank-you",
template: "src/templates/GleamThankYou.js",
getData: () => {},
},
{
path: "/blog",
template: "src/templates/Blog",
getData: () => ({
allPostsByYear,
allPosts,
tags: topTags(allPosts, 10),
}),
},
{
path: "/careers",
template: "src/templates/Careers",
getData: () => ({ jobs: jobsWithoutContents(content) }),
},
...postPages,
...sitePages,
...authorPages,
...jobPages,
...campaignPages,
...guidePages,
...guideChapterPages,
];
},
};
export {
contentDir,
isPublished,
relatedSlugs,
relatedPosts,
injectRelatedPosts,
postsToPostPages,
stripRelatedPostsContent,
stripPostContents,
rawDataToGetData,
injectRelatedPostAuthors,
injectAuthor,
generateGuideIndex,
generateGuideIndices,
injectGuideIndex,
};