-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathblog.js
503 lines (480 loc) · 16.4 KB
/
blog.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
"use strict";
/* eslint-disable unicorn/no-array-for-each */
const {hostnameToken, showFuturePosts, redirectToHttps, siteRoot} = require("./config");
const {createHash, randomInt} = require("node:crypto");
const fs = require("node:fs").promises;
const path = require("node:path");
const Ajv = require("ajv");
const express = require("express");
const highlightJs = require("highlight.js");
const lunr = require("lunr");
const MarkdownIt = require("markdown-it");
const React = require("react");
const ReactDOMServer = require("react-dom/server");
const RSS = require("rss");
const render = require(`${siteRoot}/render.js`);
const router = express.Router({
"caseSensitive": true,
"strict": true
});
const markdownIt = new MarkdownIt({
"highlight": (str, language) => {
if (language && highlightJs.getLanguage(language)) {
const ignoreIllegals = true;
return highlightJs.highlight(str, {
language,
ignoreIllegals
}).value;
}
return "";
},
"xhtmlOut": true
});
const baseTen = 10;
const postsDir = `${siteRoot}/posts`;
const postExtensionRe = /\.json$/u;
const postsSortedByContentDate = [];
const postsSortedByPublishDate = [];
const postsIndexedById = {};
let searchIndex = null;
const commonHtmlStopWords =
"alt br div h1 h2 h3 h4 h5 h6 href img li ol pre src srcset ul".split(" ");
const linkRes = [
/<(?:a|area|link)\s[^>]*href\s*=\s*"?([^">\s]+)/giu,
/<(?:audio|embed|iframe|img|input|script|source|track|video)\s[^>]*(?:src|srcset)\s*=\s*"?([^">\s]+)/giu,
/<object\s[^>]*data\s*=\s*"?([^">\s]+)/giu,
/<video\s[^>]*poster\s*=\s*"?([^">\s]+)/giu
];
const yyyyMMddRe = /^\d{8}$/u;
const escapeForRegExp = (str) => str.replaceAll(/[-/\\^$*+?.()|[\]{}]/gu, "\\$&");
const hostnameTokenEscaped =
`${escapeForRegExp(hostnameToken)}|${escapeForRegExp(encodeURIComponent(hostnameToken))}`;
const hostnameTokenRe = new RegExp(hostnameTokenEscaped, "gu");
const referenceRe = new RegExp(`(${hostnameTokenEscaped})/blog/post/([\\w-]+)`, "gu");
const formatStringForSearch = (str) => str.replaceAll(/[\W_]+/gu, " ");
const getSiteUrl =
(req) => {
const protocol = (redirectToHttps || req.secure) ? "https" : "http";
const host = req.headers["x-forwarded-host"] || req.headers.host;
return `${protocol}://${host}`;
};
const getPublishedPostFilter = (includeDateless, dateValue) => {
const now = dateValue || Date.now();
return (post) => {
const publishDate = post.publishDate.getTime();
return ((publishDate > 0) && (publishDate <= now)) ||
(includeDateless && (publishDate === 0)) ||
showFuturePosts;
};
};
const getTagNames = () => {
const tagSet = new Set();
postsSortedByContentDate.
filter(getPublishedPostFilter()).
forEach((post) => {
post.tags.forEach((tag) => tagSet.add(tag));
});
const tagList = [...tagSet];
tagList.sort((left, right) => left.localeCompare(right));
return tagList;
};
const getArchivePeriods = () => {
const archivePeriods = [];
let lastPeriodValue = 0;
postsSortedByContentDate.
filter(getPublishedPostFilter()).
forEach((post) => {
const postPeriod = new Date(post.contentDate.getFullYear(), post.contentDate.getMonth());
if (postPeriod.getTime() !== lastPeriodValue) {
archivePeriods.push(postPeriod);
lastPeriodValue = postPeriod.getTime();
}
});
return archivePeriods;
};
const ajv = new Ajv();
const validatePostSchema = ajv.compile(require("./post-schema.json"));
const contentSchema = render.getContentJsonSchema();
const validateContentSchema = contentSchema ?
ajv.compile(contentSchema) :
() => true;
// eslint-disable-next-line dot-notation
router["postsLoaded"] = fs.readdir(postsDir).
catch((error) => {
if (error.code !== "ENOENT") {
throw error;
}
return [];
}).
// Read posts from files
then((files) => Promise.all(files.
filter((file) => postExtensionRe.test(file)).
map((file) => {
const id = file.replace(postExtensionRe, "");
if (!(/[\w-]+/u).test(id)) {
throw new Error(`Post id "${id}" contains unsupported characters.`);
}
const filePath = path.join(postsDir, file);
return fs.readFile(filePath, "utf8").
then((content) => {
const post = JSON.parse(content);
if (!validatePostSchema(post)) {
const message = JSON.stringify(validatePostSchema.errors, null, 2);
throw new Error(`Post schema validation error in "${file}"\n${message}`);
}
if (post.contentJson && !validateContentSchema(post.contentJson)) {
const message = JSON.stringify(validateContentSchema.errors, null, 2);
throw new Error(`Content schema validation error in "${file}"\n${message}`);
}
post.id = id;
post.contentDate = new Date(post.contentDate || post.publishDate || 0);
post.publishDate = new Date(post.publishDate || 0);
post.tags ||= [];
post.related ||= [];
post.title = render.getPostTitle(post);
return post;
}).
then((post) => {
let promise = Promise.resolve();
if (post.contentJson) {
post.contentSearch = JSON.stringify(post.contentJson);
const contentElements = render.getContentJsonElements(post);
post.contentHtml = ReactDOMServer.renderToStaticMarkup(contentElements);
post.contentSource = "json";
delete post.contentJson;
} else {
const htmlFile = `${id}.html`;
const includesHtmlFile = files.includes(htmlFile);
const mdFile = `${id}.md`;
const includesMdFile = files.includes(mdFile);
if (!includesHtmlFile && !includesMdFile) {
throw new Error(`Post id "${id}" missing 'contentJson'/${htmlFile}/${mdFile}.`);
}
promise =
fs.readFile(path.join(postsDir, includesHtmlFile ? htmlFile : mdFile), "utf8").
then((content) => {
post.contentSearch = content;
post.contentHtml = includesHtmlFile ? content : markdownIt.render(content);
post.contentSource = includesHtmlFile ? "html" : "markdown";
});
}
return promise.
then(() => {
const contentHtml = post.contentHtml.replace(hostnameTokenRe, "https://example.com");
linkRes.forEach((linkRe) => {
let match;
while ((match = linkRe.exec(contentHtml)) !== null) {
const [, url] = match;
try {
// eslint-disable-next-line no-new
new URL(url);
} catch {
throw new Error(`URL "${url}" in post "${post.id}" must be absolute for RSS.`);
}
}
});
postsSortedByContentDate.push(post);
postsIndexedById[post.id] = post;
});
});
}))).
// Sort posts by date
then(() => {
postsSortedByContentDate.sort((left, right) => (right.contentDate - left.contentDate) ||
right.id.localeCompare(left.id));
postsSortedByPublishDate.push(...postsSortedByContentDate);
postsSortedByPublishDate.sort((left, right) => (right.publishDate - left.publishDate) ||
right.id.localeCompare(left.id));
}).
// Resolve related posts
then(() => {
postsSortedByPublishDate.forEach((post) => {
post.related = post.related.map((relatedPostId) => {
const relatedPost = postsIndexedById[relatedPostId];
if (!relatedPost) {
throw new Error(`Related post "${relatedPostId}" in post "${post.id}" is not valid.`);
}
return relatedPost;
});
});
}).
// Find referenced posts
then(() => {
postsSortedByPublishDate.forEach((post) => {
let match;
while ((match = referenceRe.exec(post.contentHtml)) !== null) {
const [, , id] = match;
const matches = postsSortedByPublishDate.filter((pst) => pst.id === id);
if (matches.length !== 1) {
throw new Error(`Reference "${id}" in post "${post.id}" is not valid.`);
}
const [target] = matches;
post.related.push(target);
target.related.push(post);
}
});
}).
// Remove duplicates from related and sort
then(() => {
postsSortedByPublishDate.forEach((post) => {
post.related = [...new Set(post.related)];
post.related.sort((left, right) => left.id.localeCompare(right.id));
});
}).
// Build search index
then(() => {
const dateFormatOptionsYearMonth = {
"year": "numeric",
"month": "long"
};
const dateFormatYearMonth = new Intl.DateTimeFormat("en-US", dateFormatOptionsYearMonth);
searchIndex = lunr(function Config () {
const commonHtmlStopWordFilter = lunr.generateStopWordFilter(commonHtmlStopWords);
lunr.Pipeline.registerFunction(commonHtmlStopWordFilter, "commonHtmlStopWords");
this.pipeline.after(lunr.stopWordFilter, commonHtmlStopWordFilter);
this.pipeline.remove(lunr.stopWordFilter);
this.ref("id");
this.field("title");
this.field("content");
this.field("tag");
this.field("date");
for (const post of postsSortedByContentDate) {
this.add({
"id": post.id,
"title": formatStringForSearch(post.title),
"content": formatStringForSearch(post.contentSearch),
"tag": post.tags,
"date": dateFormatYearMonth.format(post.contentDate).split(" ")
});
delete post.contentSearch;
}
});
});
const questionQueryString = (searchParams) => {
const urlSearchParams = new URLSearchParams();
for (const [key, value] of Object.entries(searchParams)) {
urlSearchParams.set(key, value);
}
urlSearchParams.sort();
const str = urlSearchParams.toString();
return str ? `?${str}` : "";
};
const renderPosts = (req, res, next, posts, noindex, title, period, tag, query) => {
const siteUrl = getSiteUrl(req);
const url = new URL(req.originalUrl, siteUrl);
const urlHref = url.href;
const pageParam = "page";
const page = url.searchParams.get(pageParam);
let currIndex = 0;
const findIndexOfPage = (post, index) => {
if (post.id === page) {
currIndex = index;
return false;
}
return true;
};
if (page && posts.every(findIndexOfPage)) {
return next();
}
const searchParams = {};
const queryParams = query ? {query} : null;
const defaultCount = 10;
const countParam = "count";
const count =
Math.max(Number.parseInt(url.searchParams.get(countParam), baseTen), 0) ||
defaultCount;
if (count !== defaultCount) {
searchParams[countParam] = count;
}
const prevIndex = currIndex - count;
const nextIndex = currIndex + count;
let prevLink = null;
if (currIndex > 0) {
const prevLinkParams = {
...searchParams,
...queryParams
};
if (prevIndex > 0) {
prevLinkParams[pageParam] = posts[prevIndex].id;
}
prevLink = `${url.pathname}${questionQueryString(prevLinkParams)}`;
}
let nextLink = null;
if (nextIndex < posts.length) {
const nextLinkParams = {
...searchParams,
...queryParams
};
nextLinkParams[pageParam] = posts[nextIndex].id;
nextLink = `${url.pathname}${questionQueryString(nextLinkParams)}`;
}
const elements = render.getHtmlElements({
"posts": posts.slice(currIndex, nextIndex),
"tags": getTagNames(),
"archives": getArchivePeriods(),
"noindex": noindex || (Object.keys(req.query).length > 0),
"publishedPostFilter": getPublishedPostFilter(true),
urlHref,
title,
period,
tag,
query,
searchParams,
questionQueryString,
prevLink,
nextLink
});
const staticMarkup = ReactDOMServer.renderToStaticMarkup(elements);
const finalMarkup = staticMarkup.replace(hostnameTokenRe, siteUrl);
const body = `<!DOCTYPE html>${finalMarkup}`;
return res.send(body);
};
const createPost = (id, contentHtml) => {
const noDate = new Date(0);
return {
id,
contentHtml,
"contentDate": noDate,
"publishDate": noDate,
"tags": [],
"related": []
};
};
router.get("/", (req, res, next) => {
const posts = postsSortedByPublishDate.filter(getPublishedPostFilter());
return renderPosts(req, res, next, posts, false);
});
router.get("/post/:id", (req, res, next) => {
const posts = postsSortedByContentDate.
filter(getPublishedPostFilter(true)).
filter((post) => post.id === req.params.id);
if (posts.length === 0) {
return next();
}
return renderPosts(req, res, next, posts, false, posts[0].title);
});
router.get("/tag/:tag", (req, res, next) => {
const {tag} = req.params;
const posts = postsSortedByContentDate.
filter(getPublishedPostFilter()).
filter((post) => post.tags.includes(tag));
if (posts.length === 0) {
return next();
}
return renderPosts(req, res, next, posts, true, null, null, tag);
});
router.get("/archive/:period(\\d{6})", (req, res, next) => {
const year = Number.parseInt(req.params.period.slice(0, 4), baseTen);
const month = Number.parseInt(req.params.period.slice(4, 6), baseTen) - 1;
const posts = postsSortedByContentDate.
filter(getPublishedPostFilter()).
filter((post) => (post.contentDate.getFullYear() === year) &&
(post.contentDate.getMonth() === month));
if (posts.length === 0) {
return next();
}
return renderPosts(req, res, next, posts, true, null, new Date(year, month));
});
router.get("/search", (req, res, next) => {
const {query} = req.query;
if (typeof query !== "string") {
return next();
}
const posts = (query
? searchIndex.
search(query).
map((result) => postsIndexedById[result.ref])
: postsSortedByPublishDate
).filter(getPublishedPostFilter());
if (posts.length === 0) {
const noResults = "No results";
const contentHtml = ReactDOMServer.renderToStaticMarkup(React.createElement(
React.Fragment,
null,
React.createElement("p", null, noResults)
));
posts.push(createPost(noResults, contentHtml));
}
return renderPosts(req, res, next, posts, true, null, null, null, query);
});
router.get("/rss", (req, res, next) => {
const posts = postsSortedByPublishDate.
filter(getPublishedPostFilter()).
filter((post, index) => index < 20);
if (posts.length === 0) {
return next();
}
const siteUrl = getSiteUrl(req);
const {title, description, author, copyright} = render.getRssMetadata();
const feed = new RSS({
title,
description,
"feed_url": `${siteUrl}/blog/rss`,
"site_url": `${siteUrl}/blog`,
copyright,
"pubDate": posts[0].publishDate,
"ttl": 60
});
posts.forEach((post) => {
feed.item({
"title": post.title,
"url": `${siteUrl}/blog/post/${post.id}`,
"description": post.contentHtml.replace(hostnameTokenRe, siteUrl),
"date": post.publishDate,
author
});
});
res.setHeader("Content-Type", "application/rss+xml");
return res.send(feed.xml());
});
router.get("/flashback", (req, res, next) => {
const dateString = req.query.date ||
(new Date()).
toISOString().
slice(0, 10).
replaceAll("-", "");
if (!yyyyMMddRe.test(dateString)) {
return next();
}
const dateValue = (new Date(
Number.parseInt(dateString.slice(0, 4), baseTen),
Number.parseInt(dateString.slice(4, 6), baseTen) - 1,
Number.parseInt(dateString.slice(6, 8), baseTen)
)).getTime();
const posts = postsSortedByPublishDate.
filter(getPublishedPostFilter(false, dateValue));
if (posts.length === 0) {
return next();
}
const random =
createHash("sha256").
update(dateString).
digest().
readUInt32BE();
const index = random % posts.length;
const {id} = posts[index];
return res.redirect(`/blog/post/${id}`);
});
router.get("/random", (req, res, next) => {
const posts = postsSortedByPublishDate.filter(getPublishedPostFilter());
if (posts.length === 0) {
return next();
}
const index = randomInt(posts.length);
const {id} = posts[index];
return res.redirect(`/blog/post/${id}`);
});
router.use((req, res, next) => {
const statusCode = 404;
const statusText = "Not Found";
res.status(statusCode);
const contentHtml = ReactDOMServer.renderToStaticMarkup(React.createElement(
React.Fragment,
null,
React.createElement("strong", null, `HTTP ${statusCode}`),
React.createElement("p", null, statusText)
));
const post = createPost(statusText, contentHtml);
return renderPosts(req, res, next, [post], true, statusText);
});
module.exports = router;