-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
409 lines (367 loc) · 23.9 KB
/
index.mjs
File metadata and controls
409 lines (367 loc) · 23.9 KB
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
import fs from "fs/promises"
import { tmpdir } from "os"
import { join as pathJoin } from "path"
import { createHash, randomUUID } from "node:crypto"
import { createServer } from "http"
const __dirname = import.meta.dirname;
if (__dirname) {
let server
try {
let separator = "/"
let rxSeparator = "/"
if (process.platform === "win32") {
separator = "\\"
rxSeparator = "\\\\"
}
const rdOpts = {
recursive: true
}
let argv = process.argv.slice()
let doPublish = false
if (argv.length === 5 && argv.includes("-p")) {
argv = argv.filter(x => x !== "-p")
doPublish = true
}
if (argv.length === 4) {
let rootContentPath = argv[2]
let rootOutPath = argv[3]
if (!doPublish) {
server = createServer(async (req, res) => {
try {
let url = req.url
let fileEndingMatch = url.match(/\.(css|html)$/)
if (!fileEndingMatch) {
url += (url.charAt(url.length - 1) === "/" ? "" : "/") + "index.html"
}
let data = await fs.readFile(rootOutPath + url)
let contentType = "text/html"
fileEndingMatch = url.match(/\.(css|html)$/)
if (fileEndingMatch && fileEndingMatch[1] === "css") {
contentType = "text/css"
}
res.writeHead(200, { "Content-Type": contentType });
res.end(data);
} catch (err) {
res.writeHead(404);
res.end("Not found");
}
}).listen(8000)
}
do {
console.log("Rebuilding everything.")
// Remove everything except possible .git folder
try {
for (const file of await fs.readdir(rootOutPath)) {
if (file !== ".git") {
await fs.rm(pathJoin(rootOutPath, file), { recursive: true })
}
}
} catch (err) {
if (err.code !== "ENOENT") {
throw err
}
}
let allContent = {}
let contentPerType = {}
for (const shortFilePath of await fs.readdir(rootContentPath, rdOpts)) {
let contentMatch = shortFilePath.match(new RegExp("^(.*)" + rxSeparator + "([^" + rxSeparator + "]+)\.md$"))
if (!contentMatch) {
// Special handling of root folder
contentMatch = shortFilePath.match("()(_index.md)")
}
if (contentMatch) {
let contentType = contentMatch[1]
let contentTypeSlug = contentType
.replace(/ /g, "-")
.toLowerCase()
.replace(/[åä]/g,"a")
.replace(/[ö]/g,"o")
let name = contentMatch[2]
let nameSlug = name
.replace(/ /g, "-")
.toLowerCase()
.replace(/[åä]/g,"a")
.replace(/[ö]/g,"o")
let filePath = pathJoin(...[rootContentPath, shortFilePath].map(x => x.trim()))
let content = await fs.readFile(filePath, "utf-8")
let contentParts = content.split("+++").map(x => x.trim()).filter(x => x)
let settings = contentParts[0]
.split(/[\r\n|\n|\r]/)
.filter(x => x.trim())
.map(row => Object.assign({ row, match:row.match(/^(.*?)=(.*)$/) }))
.reduce((acc, rowAndmatch) => {
let match = rowAndmatch.match
if (!match) {
throw new Error(rowAndmatch.row + " i " + name + " ser lite fel ut.")
}
let strValue = match[2].trim()
.replace(/^['"](.*)['"]$/g, "$1")
acc[match[1].trim()] = strValue.match(/(?:true|false)/i) ?
!!strValue.match(/true/i) :
strValue
return acc
},{})
let contentItem = {
contentType,
contentTypeSlug,
name,
nameSlug,
settings,
data: contentParts[1]
}
allContent[shortFilePath] = contentItem
if (!contentPerType[contentTypeSlug]) {
contentPerType[contentTypeSlug] = []
}
contentPerType[contentTypeSlug].push(contentItem)
}
}
let nav = []
for (const shortFilePath of await fs.readdir(rootContentPath, rdOpts)) {
if (!shortFilePath.includes("Ordbok") && shortFilePath.includes(separator) && shortFilePath.indexOf("_index") === -1) {
let parts = shortFilePath.split(separator)
let subjectName = parts[0]
let itemName = parts[1]?.replace(".md", "")
let contentItem = allContent[shortFilePath]
if (!contentItem.settings.draft || (!doPublish && contentItem.settings.draft)) {
let navItem = nav.find(x => x.subject === subjectName)
if (!navItem) {
navItem = {
subject: subjectName,
items: []
}
nav.push(navItem)
}
navItem.items.push(itemName)
}
}
}
nav.forEach(navItem => navItem.items.sort())
nav.sort((a,b) => a.subject.localeCompare(b.subject))
let htmlMainNav = '<section class="main-nav"><details><summary>Innehåll</summary>' + nav.reduce((html, navItem) => {
let contentTypeSlug = navItem.subject.replace(/ /g, "-")
.toLowerCase()
.replace(/[åä]/g,"a")
.replace(/[ö]/g,"o")
html += '<section class="main-nav__content-type">'
html += ' <details>'
html += ' <summary><a href="/' + contentTypeSlug + '">' + navItem.subject + '</a></summary>'
for (const pageItemName of navItem.items) {
let nameSlug = pageItemName.replace(/ /g, "-")
.toLowerCase()
.replace(/[åä]/g,"a")
.replace(/[ö]/g,"o")
html += ' <section class="main-nav__content-item">'
html += ' <a href="/' + contentTypeSlug + '/' + nameSlug + '">' + pageItemName + '</a>'
html += ' </section>'
}
html += ' </details>'
html += '</section>'
return html
},"") + '</details></section>'
let cacheBusting = {}
for (const filePath of await fs.readdir("source", rdOpts)) {
let sourceMatch = filePath.match(new RegExp("^(?:(.*)" + rxSeparator + ")?([^" + rxSeparator + "]+)\.(css|js)$"))
if (sourceMatch) {
let path = sourceMatch[1] || ""
let filename = sourceMatch[2]
let extension = sourceMatch[3]
let pathSlug = path
.replace(/ /g, "-")
.toLowerCase()
.replace(/[åä]/g,"a")
.replace(/[ö]/g,"o")
// Files that should be copied as they are, possible with cache busting
let srcFilePath = pathJoin(...["source", path, filename + "." + extension].filter(x => x.trim()))
let outPath = pathJoin(...[rootOutPath, pathSlug].filter(x => x.trim()))
await fs.mkdir(outPath, { recursive: true })
let textData = await fs.readFile(srcFilePath, "utf-8")
let finalFilename = filename
let hash = createHash("sha256")
hash.update(textData)
let key = hash.digest("hex").slice(0,12)
if (!cacheBusting[pathSlug]) {
cacheBusting[pathSlug] = {}
}
finalFilename = filename + "." + key
cacheBusting[pathSlug][filename + "." + extension] = finalFilename + "." + extension
let outFilePath = pathJoin(...[rootOutPath, pathSlug, finalFilename + "." + extension].filter(x => x.trim()))
await fs.writeFile(outFilePath, textData)
}
}
let templates = {}
for (const filePath of await fs.readdir("source", rdOpts)) {
let sourceMatch = filePath.match(new RegExp("^(?:(.*)" + rxSeparator + ")?([^" + rxSeparator + "]+)\.(html)$"))
if (sourceMatch) {
let path = sourceMatch[1] || ""
let filename = sourceMatch[2]
let extension = sourceMatch[3]
let pathSlug = path
.replace(/ /g, "-")
.toLowerCase()
.replace(/[åä]/g,"a")
.replace(/[ö]/g,"o")
// Files that should be copied almost as they are, possible with cache busting
let srcFilePath = pathJoin(...["source", path, filename + "." + extension].filter(x => x.trim()))
let outPath = pathJoin(...[rootOutPath, pathSlug].filter(x => x.trim()))
await fs.mkdir(outPath, { recursive: true })
let textData = await fs.readFile(srcFilePath, "utf-8")
let finalFilename = filename
let busted = cacheBusting[pathSlug]
if (busted) {
for (const name of Object.keys(busted)) {
textData = textData.replace(new RegExp('"' + name + '"', "g"), busted[name])
textData = textData.replace(new RegExp('"/' + [pathSlug,name].filter(x => x.trim()).join("/") + '"', "g"), "/" + [pathSlug,busted[name]].filter(x => x.trim()).join("/"))
}
}
textData = textData
.replace(/{{main-nav}}/g, htmlMainNav)
let outFilePath = pathJoin(...[rootOutPath, pathSlug, finalFilename + "." + extension].filter(x => x.trim()))
if (filename.charAt(0) !== "_") {
await fs.writeFile(outFilePath, textData)
} else {
templates[filename + "." + extension] = textData
}
}
}
for (const shortFilePath of await fs.readdir(rootContentPath, rdOpts)) {
let contentItem = allContent[shortFilePath]
if (contentItem) {
let contentRows = contentItem.data
.split(/(?:\r\n|\n|\r)/)
.map(x => x.trim())
let htmlContent = contentRows
.map(row => row
.replace(/\[\^([0-9]+?)\]:(.*)/g, '<li id="footnote$1">OL$2</li>')
.replace(/\[\^([0-9]+?)\]/g, '<a class="footnote-link" href="#footnote$1"><sup>[$1]</sup></a>')
.replace(/!\[([^\]]+?)\]\((.+?)(?: "?(.+?)"?)?\)TXT:([^|]+)\|ATTR:([^|]+)\|/, (ms, g1, g2, g3, g4, g5) => `<section class="inline-image__container"><img class="inline-image" src="${g2}" alt="${g1}" title="${g3}"><section class="image-text"><details><summary><section>${g4}</section><section class="image-attribution__icon">ℹ️</section></summary><section class="image-attribution">${g5.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>')}</section></details></section></section>`)
.replace(/!\[([^\]]+?)\]\((.+?)(?: "?(.+?)"?)?\)TXT:([^|]+)\|/, (ms, g1, g2, g3, g4) => `<section class="inline-image__container"><img class="inline-image" src="${g2}" alt="${g1}" title="${g3}"><section class="image-text">${g4}</section></section>`)
.replace(/!\[([^\]]+?)\]\((.+?)(?: "?(.+?)"?)?\)ATTR:([^|]+)\|/, (ms, g1, g2, g3, g4) => `<section class="inline-image__container"><img class="inline-image" src="${g2}" alt="${g1}" title="${g3}"><section class="image-text"><details><summary><section></section><section class="image-attribution__icon">ℹ️</section></summary><section class="image-attribution">${g4.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>')}</section></details></section></section>`)
.replace(/!\[([^\]]+?)\]\((.+?)(?: "?(.+?)"?)?\)/, '<section class="inline-image__container"><img class="inline-image" src="$2" alt="$1" title="$3"></section>')
.replace(/\[([^\]]+?)\]\((.+?)\)/g, '<a href="$2">$1</a>')
.replace(/^ATTR:(.*)$/, '<section class="image-attribution">$1</section>')
.replace(/##### (.*)$/, "<h5>$1</h5>")
.replace(/#### (.*)$/, "<h4>$1</h4>")
.replace(/### (.*)$/, "<h3>$1</h3>")
.replace(/## (.*)$/, "<h2>$1</h2>")
.replace(/# (.*)$/, "<h1>$1</h1>")
.replace(/^\* (.*)$/, '<li>UL$1</li>')
.replace(/^[0-9]+\. (.*)$/, '<li>OL$1</li>')
.replace(/^>(.*)/, "<blockquote><p>$1</p></blockquote>")
.replace(/^([^<].*)$/, "<p>$1</p>")
.replace(/\*\*\*(.*?)\*\*\*/g, "<strong><em>$1</em></strong>")
.replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>")
.replace(/\*(.*?)\*/g, "<em>$1</em>")
.replace(/---/g, "<hr>"))
.join("\n")
.replace(/<\/blockquote>\n<blockquote>/gs, "")
.replace(/((?:<li.*?>UL.*?<\/li>(?:\n|$))+)/g, "<ul>$1</ul>")
.replace(/((?:<li.*?>OL.*?<\/li>(?:\n|$))+)/g, "<ol>$1</ol>")
.replace(/(<li.*?>)(?:OL|UL)/g, "$1")
.replace(/<\/p>\n<p>/g, "<br>")
let contentTemplate = templates["_default-content.html"]
if (contentItem.name === "_index.md" && contentItem.contentTypeSlug === "") {
contentTemplate = templates["_main-index.html"]
} else if (contentItem.name.indexOf("_index") === 0) {
contentTemplate = templates["_default-index.html"]
} else if (contentItem.contentTypeSlug === "ordbok") {
contentTemplate = templates["_default-dictionary-content.html"]
}
let htmlBreadcrumbs = `<section class="breadcrumbs"><a href="/">Hielmbygden</a> | ${contentItem.contentType}</section>`
if (contentItem.name.indexOf("_index") !== 0) {
htmlBreadcrumbs = `<section class="breadcrumbs"><a href="/">Hielmbygden</a> | <a href="/${contentItem.contentTypeSlug}">${contentItem.contentType}</a> | ${contentItem.name}</section>`
}
let htmlContentIndex = ""
if (contentItem.name.indexOf("_index") === 0) {
let collection = contentPerType[contentItem.contentTypeSlug]
if (collection) {
htmlContentIndex += '<section class="content-index">'
htmlContentIndex += "<hr />"
htmlContentIndex += `<h2>Alla ${contentItem.contentType.toLowerCase()}</h2>`
for (const indexItem of collection.filter(x => x.name !== "_index")) {
if (!indexItem.settings.draft || (!doPublish && indexItem.settings.draft)) {
htmlContentIndex += ` <a class="index-card__link-container" href="/${indexItem.contentTypeSlug}/${indexItem.nameSlug}">`
htmlContentIndex += ' <section class="index-card">'
htmlContentIndex += ` <img class="inline-image" src="${indexItem.settings.featured_image}">`
htmlContentIndex += ` <h3>${indexItem.settings.title}</h3>`
htmlContentIndex += " <p>Klicka för att läsa mer.</p>"
htmlContentIndex += " </section>"
htmlContentIndex += " </a>"
}
}
htmlContentIndex += "</section>"
}
}
let featuredImageHtml = '<section class="banner-image__container">'
featuredImageHtml += `<img class="banner-image" src="${contentItem.settings.featured_image}" alt="${contentItem.settings.featured_image_alt}">`
if (contentItem.settings.featured_image_attribution) {
featuredImageHtml += ` <section class="image-text"><details><summary><section>${contentItem.settings.featured_image_text || ""}</section><section class="image-attribution__icon">ℹ️</section></summary><section class="image-attribution">${contentItem.settings.featured_image_attribution.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>')}</section></details></section>`
} else if (contentItem.settings.featured_image_text) {
featuredImageHtml += ` <section class="image-text">${contentItem.settings.featured_image_text}</section>`
}
featuredImageHtml += '</section>'
if (contentItem.settings.featured_image_narrow) {
featuredImageHtml = ""
featuredImageHtml += '<section class="banner-image__container">'
featuredImageHtml += ' <picture>'
featuredImageHtml += ` <source media="(max-width: 799px)" srcset="${contentItem.settings.featured_image_narrow}" />`
featuredImageHtml += ` <source media="(min-width: 800px)" srcset="${contentItem.settings.featured_image}" />`
featuredImageHtml += ` <img class="banner-image" src="${contentItem.settings.featured_image}" alt="${contentItem.settings.featured_image_alt}">`
featuredImageHtml += ' </picture>'
if (contentItem.settings.featured_image_attribution) {
featuredImageHtml += ` <section class="image-attribution">${contentItem.settings.featured_image_attribution}</section>`
}
featuredImageHtml += '</section>'
}
let html = contentTemplate
.replace(/{{title}}/g, contentItem.settings.title)
.replace(/{{subtitle}}/g, contentItem.settings.subtitle ?? "")
.replace(/{{featured_image_url}}/g, contentItem.settings.featured_image)
.replace(/{{featured_image}}/g, featuredImageHtml)
.replace(/{{content}}/g, htmlContent)
.replace(/{{main-nav}}/g, htmlMainNav)
.replace(/{{breadcrumbs}}/g, htmlBreadcrumbs)
.replace(/{{content-index}}/g, htmlContentIndex)
.replace(/{{urlPath}}/g, "/" + contentItem.contentTypeSlug + (contentItem.nameSlug === "_index" ? "" : "/" + contentItem.nameSlug))
if (!contentItem.settings.draft || (!doPublish && contentItem.settings.draft)) {
let outPath = pathJoin(...[rootOutPath, contentItem.contentTypeSlug, contentItem.nameSlug].map(x => x.trim()))
let outFilePath = pathJoin(...[rootOutPath, contentItem.contentTypeSlug, contentItem.nameSlug, "index.html"].map(x => x.trim()))
if (contentItem.name.indexOf("_index") === 0) {
outFilePath = pathJoin(...[rootOutPath, contentItem.contentTypeSlug, "index.html"].map(x => x.trim()))
} else {
await fs.mkdir(outPath, { recursive: true })
}
await fs.writeFile(outFilePath, html)
}
if (contentItem.contentTypeSlug === "ordbok") {
let minOutFilePath = pathJoin(...[rootOutPath, contentItem.contentTypeSlug, contentItem.nameSlug, "min.html"].map(x => x.trim()))
await fs.writeFile(minOutFilePath, htmlContent)
}
}
}
await fs.writeFile(pathJoin(rootOutPath, "CNAME"), "hielmbygden.se", "utf-8")
// Copy favicons
await fs.cp(pathJoin(__dirname, "favicon.ico"), pathJoin(rootOutPath, "favicon.ico"))
await fs.cp(pathJoin(__dirname, "apple-touch-icon.png"), pathJoin(rootOutPath, "apple-touch-icon.png"))
console.log("Done")
if (!doPublish) {
console.log("Waiting 10 seconds until next rebuild...")
await new Promise(r => setTimeout(r, 10 * 1000))
}
} while (!doPublish)
} else {
console.error("Det saknas sökvägar för katalog att hämta från och lägga saker i!")
}
} catch (err) {
console.error("")
console.error("Nu blev det fel!")
console.error("")
console.error(err.message)
console.error("")
if (server) {
server.close()
}
}
} else {
console.log("Ser ut som för gammal node version. Dags att uppgradera!")
}