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
|
const fs = require("fs");
const path = require('path');
const M = require("mustache");
// https://stackabuse.com/how-to-split-an-array-into-even-chunks-in-javascript/
function sliceIntoChunks(arr, chunkSize) {
const res = [];
for (let i = 0; i < arr.length; i += chunkSize) {
const chunk = arr.slice(i, i + chunkSize);
res.push(chunk);
}
return res;
}
// https://stackoverflow.com/questions/17428587/transposing-a-2d-array-in-javascript
function transpose(matrix) {
return matrix[0].map((col, i) => matrix.map(row => row[i]));
}
const GALLERY_DIR = "_gallery";
const SITE_CONFIG_DIR = "_configuration";
const SRC_DIR = "src";
const DIST_DIR = "dist";
const HTML_TEMP_PATH = path.join(SRC_DIR, "index.html.mustache");
const HTML_OUT_PATH = path.join(DIST_DIR, "index.html");
const CONTACT_INFO_PATH = path.join(SITE_CONFIG_DIR, "contact_info.json");
fs.rmSync(DIST_DIR, { recursive: true, force: true });
fs.cpSync(SRC_DIR, DIST_DIR, { recursive: true });
const htmlTemplate = fs.readFileSync(HTML_TEMP_PATH, "utf-8");
let galleryJsonList = [];
try {
galleryJsonList = fs.readdirSync(GALLERY_DIR);
} catch (err) {
if (err.message?.includes("ENOENT")) {
console.warn("Could not read galleries, using empty list.");
} else {
throw err;
}
}
const galleryViews = galleryJsonList
.map((fileName) => fs.readFileSync(path.join(GALLERY_DIR, fileName), "utf-8"))
.map(JSON.parse)
.map((gallery) => {
// If display_title is "-", do not render a title. Netlify CMS doesn't support optionals well.
const display_title = gallery["display_title"].trim() !== "-"
? gallery["display_title"]
: undefined;
const rows = sliceIntoChunks(gallery["images"], gallery["column_count"]);
const columns = transpose(rows);
return {
...gallery,
columns,
display_title,
};
});
const contactInfoJson = fs.readFileSync(CONTACT_INFO_PATH, "utf-8")
const contactInfoView = JSON.parse(contactInfoJson);
const view = {
galleries: galleryViews,
contact_info_title: contactInfoView["title"],
contact_info_body: contactInfoView["body"],
};
const renderedHtml = M.render(htmlTemplate, view);
fs.writeFileSync(HTML_OUT_PATH, renderedHtml);
console.log("Done.");
|