blob: d7e72aafe03352986e1d0ae530d7724d5b23f350 (
plain)
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
|
const path = require('path');
const fs = require('fs');
const folder = process.argv[2];
const jsonFile = process.argv[3];
if (!folder || !jsonFile) {
console.log('node ./json-to-files.js {path to folder} {path to json file}');
process.exit(1);
}
const specs = require(jsonFile);
const files = specs.reduce((obj, spec) => {
if (!obj[spec.section]) {
obj[spec.section] = {
md: [],
html: [],
options: {}
};
}
obj[spec.section].md.push(spec.markdown);
obj[spec.section].html.push(spec.html);
Object.assign(obj[spec.section].options, spec.options);
return obj;
}, {});
try {
fs.mkdirSync(folder, {recursive: true});
} catch (ex) {
// already exists
}
for (const section in files) {
const file = files[section];
const name = section.toLowerCase().replace(' ', '_');
const frontMatter = Object.keys(file.options).map(opt => {
let value = file.options[opt];
if (typeof value !== 'string') {
value = JSON.stringify(value);
}
return `${opt}: ${value}`;
}).join('\n');
let markdown = file.md.join('\n\n');
if (frontMatter) {
markdown = `---\n${frontMatter}\n---\n\n${markdown}`;
}
const html = file.html.join('\n\n');
const mdFile = path.resolve(folder, `${name}.md`);
const htmlFile = path.resolve(folder, `${name}.html`);
if (fs.existsSync(mdFile) || fs.existsSync(htmlFile)) {
throw new Error(`${name} already exists.`);
}
fs.writeFileSync(mdFile, markdown);
fs.writeFileSync(htmlFile, html);
}
|