-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.config.js
executable file
Β·221 lines (190 loc) Β· 5.93 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
import path from "path";
import jdown from "jdown";
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: `/${key}`,
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),
);
};
// 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),
});
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(8, allPosts);
const sitePages = getSitePages(content);
const authorPages = getAuthorPages(content);
return [
{
path: "/",
template: "src/templates/Landing",
getData: () => ({ recentPosts, tags: topTags(allPosts, 5) }),
},
{
path: "/blog",
template: "src/templates/Blog",
getData: () => ({
allPostsByYear,
allPosts,
tags: topTags(allPosts, 10),
}),
},
{
path: "/courses/tinycanva-clojure-for-react-developers",
template: "src/templates/Tinycanva",
getData: () => {},
},
...postPages,
...sitePages,
...authorPages,
];
},
};
export {
contentDir,
isPublished,
relatedSlugs,
relatedPosts,
injectRelatedPosts,
postsToPostPages,
stripRelatedPostsContent,
stripPostContents,
rawDataToGetData,
injectRelatedPostAuthors,
injectAuthor,
};