-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbuild.js
75 lines (52 loc) · 2.13 KB
/
build.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
63
64
65
66
67
68
69
70
71
72
73
74
75
const fs = require('fs');
const ncp = require('ncp').ncp;
const crypto = require('crypto');
const path = require('path');
const { Remarkable } = require('remarkable');
const toc = require('markdown-toc');
const SRC_DIR = 'src'
const OUT_DIR = 'dist'
const md = () => new Remarkable({
html: true,
typographer: true,
});
function slugify(text) {
const hash = crypto.createHmac('sha512', 'abc')
hash.update(text)
return hash.digest('hex').substr(0, 6)
}
// FAQ
const faqInput = fs.readFileSync('./src/index.md', 'utf-8')
const faqTocMarkdown = md().use(toc.plugin({
maxdepth: 2,
slugify: slugify
})).render(faqInput)
let faqOutput = md()
.use(function (remarkable) {
remarkable.renderer.rules.heading_open = function (tokens, idx) {
if (idx === 0) return '<h' + tokens[idx].hLevel + '>';
return '<h' + tokens[idx].hLevel + ' id=' + slugify(tokens[idx + 1].content) + '>'
};
remarkable.renderer.rules.heading_close = function (tokens, idx) {
if (idx === 2) return '</h' + tokens[idx].hLevel + '>';
return '<span class="permalink">[<a href="#' + slugify(tokens[idx - 1].content) + '">link</a>]</span>' + '</h' + tokens[idx].hLevel + '>';
};
})
.render(
faqInput.replace('<!-- toc -->', faqTocMarkdown.content)
)
const contribMarkdown = '## Contributors\n' + fs.readFileSync('./src/contributors.txt', 'utf-8').split('\n').map(x => `* ${x}`).join('\n') + '\n## Table of Contents'
faqOutput = faqOutput.replace('<!-- contrib -->', md().render(contribMarkdown))
faqOutput += '\n\n<link rel="stylesheet" href="./style.css">'
faqOutput = require('./plugins/wot')(faqOutput)
const jsonOutput = require('./plugins/jsongen')(faqInput)
// Create dist directory if it doesn't exist & save output to it
if (!fs.existsSync(OUT_DIR)) {
fs.mkdirSync(OUT_DIR);
}
fs.copyFileSync('./src/style.css', path.join('.', OUT_DIR, 'style.css'))
ncp('./src/assets', './dist/assets')
const faqOutputPath = path.join('.', OUT_DIR, 'index.html');
fs.writeFileSync(faqOutputPath, faqOutput, 'utf-8');
const jsonPath = path.join('.', OUT_DIR, 'faq.json');
fs.writeFileSync(jsonPath, JSON.stringify(jsonOutput, null, 4), 'utf-8');