-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit.js
120 lines (109 loc) · 5.52 KB
/
pre-commit.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
const fs = require('fs');
const sharp = require('sharp');
const sensibleTimes = time => time.substring(0, time.length -2);
const removeMilliseconds = originalDate => originalDate.toISOString().split('.')[0];
const forceUpdate = false;
const dataPath = './data/';
const imagePath = './img/';
const templateFile = fs.readFileSync('./404.html', 'utf8');
const templateFileModified = removeMilliseconds(new Date(fs.statSync('./404.html').mtime));
const previousTemplateFile = fs.readFileSync('./previous-404.html', 'utf8');
const previousTemplateFileModified = removeMilliseconds(new Date(fs.statSync('./previous-404.html').mtime));
const dataFiles = fs.readdirSync(dataPath);
const imageFiles = fs.readdirSync(imagePath);
let sitemapXML = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
const todaysDate = new Date();
const baseDomain = 'https://peggybabcock.co.uk';
const structuredData = {
"@context": "https://schema.org",
"@type": "Article",
"author": {
"@type": "Organization",
"name": "Peggy Babcock's Biographers"
},
"publisher": {
"@type": "Organization",
"name": "Peggy Babcock, Inc.",
"logo": {
"@type": "ImageObject",
"url": "https://peggybabcock.co.uk/icons/mstile-310x310.png"
}
},
"datePublished": "1997-07-31T09:26:58Z",
}
const shouldWriteFile = (modifiedDate, file) => {
return forceUpdate ||
sensibleTimes(previousTemplateFileModified) !== sensibleTimes(templateFileModified) ||
sensibleTimes(removeMilliseconds(fs.statSync(`data/${file}`).mtime)) !== sensibleTimes(removeMilliseconds(fs.statSync(`previous-data/${file}`).mtime))
};
dataFiles.forEach((file, i) => {
if (file.indexOf('.json') !== -1) {
const newFileName = file.replace('.json', '.html');
let modifiedDate = new Date(fs.statSync(newFileName).mtime);
const dataFileContents = fs.readFileSync(dataPath + file, 'utf8');
const data = JSON.parse(dataFileContents);
let toReturn = `<h2>${data.title}</h2>`;
const newHtml = data.img ? `<picture><source srcset="./img/${data.img.replace('.jpg', '.avif')}" type="image/avif"><source srcset="./img/${data.img.replace('.jpg', '.webp')}" type="image/webp"><img src="./img/${data.img}" width="${data.img === 'peggy-babcock.jpg' ? '1424' : '1024'}" height="${data.img === 'peggy-babcock.jpg' ? '848' : '1024'}" alt="${data.img_alt}" /></picture>` : '';
let newHTML = `${newHtml}<h2>${data.title}</h2>`;
if (Array.isArray(data.body)) {
newHTML += data.body.reduce((newstring, block) => newstring + block.block, '');
}
else {
newHTML += data.body;
}
structuredData.url = `${baseDomain}/${newFileName}`;
structuredData.mainEntity = structuredData.url;
structuredData.name = file === 'index.json' ? 'Peggy Babcock' : `${data.title} | Peggy Babcock`;
structuredData.image = data.img ? `${baseDomain}/img/${data.img}`: `${baseDomain}/img/peggy-babcock.jpg`;
structuredData.dateModified = modifiedDate;
structuredData.headline = data.title;
let fileContents = templateFile;
fileContents = fileContents.replace('<title>Peggy Babcock</title>', `<title>${data.title} | Peggy Babcock</title>`);
fileContents = fileContents.replace('___REPLACE_THIS___', newHTML);
fileContents = fileContents.replace('<article class="visually-hidden">', '<article>');
fileContents = fileContents.replace('<script type="application/ld+json"></script>', `<script type="application/ld+json">${JSON.stringify(structuredData)}</script>`);
if (shouldWriteFile(modifiedDate, file)) {
console.log('changing file', newFileName);
const previousStructuredData = structuredData;
structuredData.dateModified = modifiedDate;
fileContents = fileContents.replace(`<script type="application/ld+json">${JSON.stringify(previousStructuredData)}</script>`, `<script type="application/ld+json">${JSON.stringify(structuredData)}</script>`);
fs.writeFileSync(newFileName, fileContents);
fs.copyFileSync(`data/${file}`, `previous-data/${file}`);
fs.writeFileSync(`data/${file}`, dataFileContents);
modifiedDate = todaysDate;
}
sitemapXML = `${sitemapXML}<url><loc>${baseDomain}/${newFileName}</loc><lastmod>${modifiedDate.toISOString().split('T')[0]}</lastmod></url>`;
}
})
imageFiles.forEach(img => {
if (img.indexOf('.jpg') !== -1) {
const webpFilename = img.replace('.jpg', '.webp');
const webpAlreadyExists = fs.existsSync(imagePath + webpFilename);
if (!webpAlreadyExists) {
sharp(imagePath + img).webp({ quality: 80 }).toFile(imagePath + webpFilename, (err, info) => {
if (err) {
console.error(err);
} else {
console.log(info);
}
});
}
const avifFilename = img.replace('.jpg', '.avif');
const avifAlreadyExists = fs.existsSync(imagePath + avifFilename);
if (!avifAlreadyExists) {
sharp(imagePath + img).avif({ effort: 2 }).toFile(imagePath + avifFilename, (err, info) => {
if (err) {
console.error(err);
} else {
console.log(info);
}
});
}
}
});
if (forceUpdate || sensibleTimes(previousTemplateFileModified) !== sensibleTimes(templateFileModified)) {
fs.writeFileSync('./previous-404.html', templateFile);
fs.writeFileSync('./404.html', templateFile);
}
sitemapXML = sitemapXML + '</urlset>';
fs.writeFileSync('sitemap.xml', sitemapXML);