-
Notifications
You must be signed in to change notification settings - Fork 0
/
matf.js
62 lines (51 loc) · 1.54 KB
/
matf.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
import fs from 'fs/promises';
import path from 'path';
import semver from 'semver';
import markdownit from 'markdown-it'
import ejs from 'ejs';
const md = markdownit({
html: true
});
const root = process.cwd()
// Read files with given extension from given folder
const readFiles = async (folder, extension) => {
const directory = path.join(root, folder)
const files = await fs.readdir(directory);
return new Map(
await Promise.all(
files.map(async file => {
if (path.extname(file) === extension) {
return [
path.basename(file, extension),
await fs.readFile(path.join(folder, file), 'utf8')
];
}
})
)
);
};
// Execute
readFiles('wcag', '.md').then(async map => {
console.log('File map:', map);
const keys = Array.from(map.keys()).sort(semver.compare);
console.log('Sorted keys', keys);
let files = [];
for (const key of keys) {
const html = md.render(map.get(key));
files.push({
key: key,
html: html
})
}
console.log(`Successfully rendered ${files.length} files to HTML`)
const templateFile = path.join(root, 'index.ejs');
console.log(`Reading template from ${templateFile}`)
const template = await fs.readFile(templateFile, 'utf8');
console.log(`Rendering template...`)
const html = ejs.render(template, { files: files });
const indexFile = path.join(root, 'index.html');
console.log(`Writing output to ${indexFile}`);
await fs.writeFile(indexFile, html, 'utf8');
}).catch(error => {
console.error(`Error: ${error}`)
});