]> git.r.bdr.sh - rbdr/blog/blob - lib/generators/txt.js
Use serde and time
[rbdr/blog] / lib / generators / txt.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 kTextName: 'index.txt'
10 };
11
12 /**
13 * Generates a TXT version of the blog
14 *
15 * @name TXTGenerator
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 TXTGenerator(source, target, posts) {
21
22 internals.debuglog('Generating TXT');
23 const textTarget = join(target, internals.kTextName);
24 const textLocation = join(source, internals.kTextName);
25
26 internals.debuglog(`Reading ${textLocation}`);
27 const textTemplate = await readFile(textLocation, { encoding: 'utf8' });
28
29 internals.debuglog('Writing TXT');
30 const text = Dot.template(textTemplate, {
31 ...Dot.templateSettings,
32 strip: false
33 })({ posts });
34 await writeFile(textTarget, text);
35 }