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
function contentUrl(contentBase, path) {
const clean = String(path || "").replace(/^\/+/, "");
return contentBase.replace(/\/?$/, "/") + clean;
}
function sortNum(a, b) {
return a - b;
}
function isSafeNavUrl(url) {
const value = String(url || "").trim();
if (!value || value.startsWith("//") || /^[a-z][a-z0-9+.-]*:/i.test(value)) {
return false;
}
return true;
}
function articleDir(articlePath) {
const clean = String(articlePath || "").replace(/^\/+/, "");
const slash = clean.lastIndexOf("/");
return slash === -1 ? "" : clean.slice(0, slash + 1);
}
function resolveRelative(articlePath, href) {
const base = new URL(articleDir(articlePath), "https://blog.invalid/");
let resolved;
try {
resolved = new URL(href, base);
} catch (_ex) {
return null;
}
if (resolved.origin !== "https://blog.invalid") {
return null;
}
let path = resolved.pathname.replace(/^\/+/, "");
path += resolved.search;
path += resolved.hash;
return path;
}
function renderGraphs(root) {
if (typeof Viz !== "function") {
return;
}
for (const c of root.querySelectorAll("code.graphviz")) {
const p = document.createElement("p");
p.innerHTML = Viz(c.innerText, { format: "svg" });
p.className = "graph";
const pre = c.parentElement;
if (pre) {
pre.replaceWith(p);
}
}
}
function isRelativeResource(href) {
if (!href || href.charAt(0) === "#") {
return false;
}
if (href.charAt(0) === "/") {
return false;
}
if (/^[a-z][a-z0-9+.-]*:/i.test(href)) {
return false;
}
if (href.indexOf("//") === 0) {
return false;
}
if (href.charAt(0) === "?") {
return false;
}
return true;
}
function rewriteRelativeResources(root, contentBase, articlePath) {
for (const img of root.querySelectorAll("img[src]")) {
const src = img.getAttribute("src");
if (!src || !isRelativeResource(src)) {
continue;
}
const resolved = resolveRelative(articlePath, src);
if (resolved) {
img.setAttribute("src", contentUrl(contentBase, resolved));
}
}
for (const anchor of root.querySelectorAll("a[href]")) {
const href = anchor.getAttribute("href");
if (!href || !isRelativeResource(href)) {
continue;
}
const resolved = resolveRelative(articlePath, href);
if (!resolved) {
continue;
}
const hashIndex = resolved.indexOf("#");
const beforeHash = hashIndex === -1 ? resolved : resolved.slice(0, hashIndex);
const hash = hashIndex === -1 ? "" : resolved.slice(hashIndex);
const pathOnly = beforeHash.split("?")[0];
if (/\.(md|markdown)$/i.test(pathOnly)) {
anchor.setAttribute("href", "?article=" + encodeURIComponent(pathOnly) + hash);
} else {
anchor.setAttribute("href", contentUrl(contentBase, resolved));
}
}
}
function addLineNumbers(root) {
for (const block of root.querySelectorAll("pre > code:not(.graphviz)")) {
const lines = block.textContent.split("\n");
if (lines.length && lines[lines.length - 1] === "") {
lines.pop();
}
if (lines.length <= 1) {
continue;
}
const pre = block.parentElement;
if (!pre) {
continue;
}
const wrap = document.createElement("div");
wrap.className = "blog-code";
const langClass = block.className;
for (let i = 0; i < lines.length; i++) {
const row = document.createElement("div");
row.className = "blog-code-line";
const num = document.createElement("span");
num.className = "blog-code-lineno";
num.setAttribute("aria-hidden", "true");
num.textContent = String(i + 1);
const code = document.createElement("code");
if (langClass) {
code.className = langClass;
}
code.textContent = lines[i];
row.append(num, code);
wrap.append(row);
}
pre.replaceWith(wrap);
}
}
function runWhenReady(tryFn, scriptId) {
try {
tryFn();
} catch (_ex) {
const script = document.querySelector("#" + scriptId);
if (script) {
script.addEventListener("load", tryFn);
}
}
}
async function loadmd(articleEl, contentBase, path) {
const converter = new showdown.Converter();
const resp = await fetch(contentUrl(contentBase, path));
if (!resp.ok) {
articleEl.replaceChildren();
const failure = document.createElement("p");
failure.className = "text-danger";
failure.textContent = "Failed to load article.";
articleEl.append(failure);
return;
}
const md = await resp.text();
articleEl.innerHTML = converter.makeHtml(md);
rewriteRelativeResources(articleEl, contentBase, path);
addLineNumbers(articleEl);
if (window.location.hash !== "") {
try {
const target = articleEl.querySelector(window.location.hash);
if (target) {
target.scrollIntoView();
}
} catch (_ex) {
}
}
runWhenReady(() => {
if (typeof MathJax !== "undefined" && MathJax.typeset) {
MathJax.typeset();
}
}, "MathJax-script");
runWhenReady(() => {
if (typeof hljs === "undefined" || !hljs.highlightElement) {
return;
}
for (const block of articleEl.querySelectorAll(".blog-code-line > code, pre > code:not(.graphviz)")) {
hljs.highlightElement(block);
}
}, "Highlight-script");
runWhenReady(() => renderGraphs(articleEl), "GraphViz-script");
}
function appendTextBlock(parent, className, text) {
const el = document.createElement("div");
el.className = className;
el.textContent = text;
parent.append(el);
}
function appendArticleLink(parent, item, dateStr) {
const p = document.createElement("p");
p.append(document.createTextNode(dateStr + ": "));
const anchor = document.createElement("a");
const url = String(item.url || "");
anchor.href = isSafeNavUrl(url) ? url : "#";
anchor.textContent = String(item.name || "");
p.append(anchor);
parent.append(p);
}
function archive(articleEl, manifest) {
const nestedByDate = {};
const undated = [];
for (const item of manifest) {
const cdate = new Date(item.created);
if (Number.isNaN(cdate.getTime())) {
undated.push(item);
continue;
}
const y = cdate.getFullYear();
const m = cdate.getMonth();
const d = cdate.getDate();
if (!nestedByDate[y]) {
nestedByDate[y] = {};
}
if (!nestedByDate[y][m]) {
nestedByDate[y][m] = {};
}
if (!nestedByDate[y][m][d]) {
nestedByDate[y][m][d] = [];
}
nestedByDate[y][m][d].push(item);
}
let opened = false;
const dateOpts = { weekday: "long", year: "numeric", month: "long", day: "numeric" };
const years = Object.keys(nestedByDate).sort(sortNum).reverse();
for (const y of years) {
const yearDetails = document.createElement("details");
if (!opened) {
yearDetails.open = true;
}
const yearSummary = document.createElement("summary");
yearSummary.textContent = y;
yearDetails.append(yearSummary);
const yearQuote = document.createElement("blockquote");
yearQuote.append(yearDetails);
const months = Object.keys(nestedByDate[y]).sort(sortNum).reverse();
for (const m of months) {
const monthDetails = document.createElement("details");
if (!opened) {
monthDetails.open = true;
opened = true;
}
const monthSummary = document.createElement("summary");
monthSummary.textContent = new Date(2000, Number(m), 1).toLocaleString("default", { month: "long" });
monthDetails.append(monthSummary);
const days = Object.keys(nestedByDate[y][m]).sort(sortNum).reverse();
for (const d of days) {
for (const item of nestedByDate[y][m][d]) {
const dateStr = new Date(item.created).toLocaleString("default", dateOpts);
appendArticleLink(monthDetails, item, dateStr);
}
}
const monthQuote = document.createElement("blockquote");
monthQuote.append(monthDetails);
yearDetails.append(monthQuote);
}
articleEl.append(yearQuote);
}
if (undated.length) {
const details = document.createElement("details");
const summary = document.createElement("summary");
summary.textContent = "Undated";
details.append(summary);
for (const item of undated) {
appendArticleLink(details, item, "Undated");
}
const quote = document.createElement("blockquote");
quote.append(details);
articleEl.append(quote);
}
}
function hideSearchResults(resultsEl) {
resultsEl.classList.remove("show");
}
function searchChange(searchEl, resultsEl, searchManifest) {
resultsEl.replaceChildren();
const q = searchEl.value.trim().toLowerCase();
if (!q) {
hideSearchResults(resultsEl);
return;
}
for (const item of searchManifest) {
const name = String(item.name || "");
const tags = String(item.tags || "");
if (!name.toLowerCase().includes(q) && !tags.toLowerCase().includes(q)) {
continue;
}
const row = document.createElement("div");
row.className = "blog-search-result";
row.setAttribute("role", "option");
appendTextBlock(row, "blog-search-result-title", name);
appendTextBlock(row, "blog-search-result-snippet", tags);
appendTextBlock(row, "blog-search-result-date", String(item.created || ""));
const url = String(item.url || "");
if (isSafeNavUrl(url)) {
row.addEventListener("click", () => {
window.location.href = url;
});
}
resultsEl.appendChild(row);
}
if (resultsEl.childElementCount) {
resultsEl.classList.add("show");
} else {
hideSearchResults(resultsEl);
}
}
async function populateManifest(contentBase, path) {
const resp = await fetch(contentUrl(contentBase, path));
if (!resp.ok) {
return [];
}
return await resp.json();
}
async function loadManifest(articleEl, contentBase, path) {
const manifest = await populateManifest(contentBase, path);
archive(articleEl, manifest);
return manifest;
}
function bindSearch(searchManifest) {
const searchEl = document.querySelector(".blog-search");
const resultsEl = document.querySelector(".blog-search-results");
const container = document.querySelector(".blog-search-container");
if (!searchEl || !resultsEl || !container) {
return;
}
searchEl.addEventListener("input", () => searchChange(searchEl, resultsEl, searchManifest));
document.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
hideSearchResults(resultsEl);
}
});
document.addEventListener("click", (event) => {
if (!container.contains(event.target) && !resultsEl.contains(event.target)) {
hideSearchResults(resultsEl);
}
});
}
async function boot() {
const root = document.getElementById("blog-root");
const articleEl = document.getElementById("article");
if (!root || !articleEl) {
return;
}
const contentBase = root.dataset.contentBase || "";
const manifestPath = root.dataset.manifest || "articles/manifest.json";
const u = new URL(window.location.href);
const article = u.searchParams.get("article");
let searchManifest = [];
if (article) {
await loadmd(articleEl, contentBase, article);
searchManifest = await populateManifest(contentBase, manifestPath);
} else {
searchManifest = await loadManifest(articleEl, contentBase, manifestPath);
}
bindSearch(searchManifest);
}
boot();