-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhbs-renderer.js
44 lines (36 loc) · 1.43 KB
/
hbs-renderer.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
const fs = require('fs').promises;
const Handlebars = require('handlebars');
const minify = require('html-minifier').minify;
const {formatNumber, getColorByTotal, extractStateById} = require('./hbs-helpers');
// register custom helpers
Handlebars.registerHelper('format_number', formatNumber);
Handlebars.registerHelper('color', getColorByTotal);
Handlebars.registerHelper('extract_state_by_id', extractStateById);
async function renderHtml(data, templatePath, destinationPath) {
const content = await fs.readFile(templatePath, 'utf8');
const template = await Handlebars.compile(content, {noEscape: true});
const html = template(data);
const minified = minify(html, {
caseSensitive: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeComments: true
});
await fs.writeFile(destinationPath, minified, 'utf8');
}
// render index.html
async function renderIndex(data) {
const template = 'templates/index.hbs';
const destination = 'public/index.html';
// add google analytics tracking ID
data.tracking_id = process.env.GA_TRACKING_ID;
try {
await renderHtml(data, template, destination);
} catch (error) {
throw new Error(`Error rendering index.html: ${error.message}`);
} finally {
delete data.tracking_id; // prevent caching the tracking ID
}
}
module.exports.renderHtml = renderHtml;
module.exports.renderIndex = renderIndex;