forked from DavidAnson/simple-website-with-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.js
382 lines (364 loc) · 12.5 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
"use strict";
const {hostnameToken, showFuturePosts, redirectToHttps, siteRoot} = require("./config");
const fs = require("fs").promises;
const path = require("path");
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, lang) => {
if (lang && highlightJs.getLanguage(lang)) {
return highlightJs.highlight(lang, str).value;
}
return "";
},
"xhtmlOut": true
});
const postsDir = `${siteRoot}/posts`;
const postExtension = /\.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,
// eslint-disable-next-line max-len
/<(?: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 escapeForRegExp = (str) => str.replace(/[-/\\^$*+?.()|[\]{}]/gu, "\\$&");
const hostnameTokenEscaped =
`${escapeForRegExp(hostnameToken)}|${escapeForRegExp(encodeURIComponent(hostnameToken))}`;
const hostnameTokenRe = new RegExp(hostnameTokenEscaped, "gu");
const referenceRe = new RegExp(`(${hostnameTokenEscaped})/blog/post/([\\w-]+)`, "gu");
const getSiteUrl =
(req) => `${(redirectToHttps || req.secure) ? "https" : "http"}://${req.headers.host}`;
const getPublishedPostFilter = (includeDateless) => {
const now = 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.valueOf() !== lastPeriodValue) {
archivePeriods.push(postPeriod);
lastPeriodValue = postPeriod.valueOf();
}
});
return archivePeriods;
};
// eslint-disable-next-line dot-notation
router["postsLoaded"] = fs.readdir(postsDir).
catch((reason) => {
if (reason.code !== "ENOENT") {
throw reason;
}
return [];
}).
then((files) => Promise.all(files.
filter((file) => postExtension.test(file)).
map((file) => {
const id = file.replace(postExtension, "");
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);
post.id = id;
post.contentDate = new Date(post.contentDate || post.publishDate || 0);
post.publishDate = new Date(post.publishDate || 0);
post.tags = post.tags || [];
post.tag = post.tags.join(" ");
post.references = [];
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 = null;
while ((match = linkRe.exec(contentHtml)) !== null) {
const [, url] = match;
try {
// eslint-disable-next-line no-new
new URL(url);
} catch (ex) {
throw new Error(`URL "${url}" in post "${post.id}" must be absolute for RSS.`);
}
}
});
postsSortedByContentDate.push(post);
postsIndexedById[post.id] = post;
});
});
}))).
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));
}).
then(() => {
postsSortedByPublishDate.forEach((post) => {
const references = [];
let match = null;
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;
references.push(target);
target.references.push(post);
}
post.references = [
...references,
...post.references
];
});
}).
then(() => {
postsSortedByPublishDate.forEach((post) => {
post.references = [...new Set(post.references)];
});
}).
then(() => {
searchIndex = lunr(function Config () {
const commonHtmlStopWordFilter = lunr.generateStopWordFilter(commonHtmlStopWords);
lunr.Pipeline.registerFunction(commonHtmlStopWordFilter, "commonHtmlStopWords");
this.pipeline.after(lunr.stopWordFilter, commonHtmlStopWordFilter);
this.field("title");
this.field("contentSearch");
this.field("tag");
postsSortedByContentDate.forEach((post) => {
post.contentSearch = post.contentSearch.replace(/[\W_]+/gu, " ");
this.add(post);
delete post.contentSearch;
});
});
});
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 pageSize = 10;
const prevIndex = currIndex - pageSize;
const nextIndex = currIndex + pageSize;
let prevLink = null;
if (currIndex > 0) {
if (prevIndex > 0) {
url.searchParams.set(pageParam, posts[prevIndex].id);
} else {
url.searchParams.delete(pageParam);
}
prevLink = `${url.pathname}${url.search}`;
}
let nextLink = null;
if (nextIndex < posts.length) {
url.searchParams.set(pageParam, posts[nextIndex].id);
nextLink = `${url.pathname}${url.search}`;
}
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,
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": [],
"references": []
};
};
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 = parseInt(req.params.period.slice(0, 4), 10);
const month = parseInt(req.params.period.slice(4, 6), 10) - 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 (!query) {
return next();
}
const posts = searchIndex.
search(query).
map((result) => postsIndexedById[result.ref]).
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());
});
// eslint-disable-next-line no-unused-vars
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;