]> git.r.bdr.sh - rbdr/blog/blob - lib/generators/html.js
Add generator
[rbdr/blog] / lib / generators / html.js
1 import Dot from 'dot';
2 import { readFile, writeFile } from 'fs/promises';
3 import { join } from 'path';
4 import { debuglog } from 'util';
5
6 const internals = {
7 debuglog: debuglog('blog'),
8
9 kIndexName: 'index.html'
10 };
11
12 /**
13 * Generates the blog index page
14 *
15 * @name HTMLGenerator
16 * @param {string} source the source directory
17 * @param {string} target the target directory
18 * @param {Array.<Blog.tPost>} posts the list of posts
19 */
20 export default async function HTMLGenerator(source, target, posts) {
21
22 internals.debuglog('Generating HTML');
23 const indexTarget = join(target, internals.kIndexName);
24 const indexLocation = join(source, internals.kIndexName);
25
26 internals.debuglog(`Reading ${indexLocation}`);
27 const indexTemplate = await readFile(indexLocation, { encoding: 'utf8' });
28
29 internals.debuglog('Writing HTML');
30 const indexHtml = Dot.template(indexTemplate, {
31 ...Dot.templateSettings,
32 strip: false
33 })({ posts });
34 await writeFile(indexTarget, indexHtml);
35 }