-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (82 loc) · 4.59 KB
/
index.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module.exports = class themd {
constructor() {
var hljs = require('highlight.js');
const string = require('string')
const slugify = s => string(s).slugify().toString()
var mir = require('markdown-it-replace-it');
mir.replacements.push({ name: '©', re: /\([c|C]\)/g, html: true, sub: function (s) { return '©'; }, default: true });
mir.replacements.push({ name: '®', re: /\([r|R]\)/g, html: true, sub: function (s) { return '®'; }, default: true });
mir.replacements.push({ name: '™', re: /\([t|T][m|M]\)/g, html: true, sub: function (s) { return '™'; }, default: true });
mir.replacements.push({ name: '℗', re: /\([p|P]\)/g, html: true, sub: function (s) { return '℗'; }, default: true });
mir.replacements.push({ name: '±', re: /\+-/g, html: true, sub: function (s) { return '±'; }, default: true });
mir.replacements.push({ name: '→', re: /->/g, html: true, sub: function (s) { return '→'; }, default: true });
mir.replacements.push({ name: '⇒', re: /=>/g, html: true, sub: function (s) { return '⇒'; }, default: true });
mir.replacements.push({ name: '←', re: /<-/g, html: true, sub: function (s) { return '←'; }, default: true });
mir.replacements.push({ name: '⇐', re: /<=/g, html: true, sub: function (s) { return '⇐'; }, default: true });
var MarkdownIt = require('markdown-it')
this.converter = new MarkdownIt({
html: true,
linkify: true
}).use(mir).use(require('markdown-it-github-headings'), { prefixHeadingIds: false, enableHeadingLinkIcons: false }).use(require('markdown-it-attrs'), {
allowedAttributes: ['id', 'class', 'style']
}).use(require('markdown-it-emoji')).use(require('markdown-it-sub')).use(require('markdown-it-sup')).use(require('markdown-it-task-lists')).use(require('markdown-it-ins')).use(require('markdown-it-abbr')).use(require('markdown-it-footnote')).use(require('markdown-it-deflist')).use(require('markdown-it-mark')).use(require('markdown-it-container'), 'dropdown', {
validate: function(params) {
return params.trim().match(/^dropdown\s+(.*)$/);
},
render: function(tokens, idx) {
var m = tokens[idx].info.trim().match(/^dropdown\s+(.*)$/);
if (tokens[idx].nesting === 1 && m[0].startsWith('dropdown')) {
return '<details><summary>' + m[0].slice(9) + '</summary>\n';
} else {
// closing tag
return '</details>\n';
}
}
}).use(require('markdown-it-container'), 'decorate', {
validate: function(params) {
return params.trim().match(/^(.*)$/);
},
render: function(tokens, idx) {
var m = tokens[idx].info.trim().match(/^(.*)$/);
if (tokens[idx].nesting === 1) {
return '<div ' + m[0] + '>\n';
} else {
// closing tag
return '</div>\n';
}
}
})
}
render() {
const readline = require('readline');
const fs = require('fs');
fs.readdir('./pages/', (err, files) => {
const start = Date.now();
files.forEach(file => {
var data = (fs.readFileSync(`./pages/${file}`)).toString().split('\n');
const styleFileName = data.shift();
data = data.join('\n')
data = this.converter.render(data);
const html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${file.slice(0, file.length - 3)}</title>
<link rel="stylesheet" href="../style/${styleFileName}.css">
</head>
<body>
${data}
</body>
</html>`
fs.writeFile(`./dist/${file.slice(0, file.length - 3)}.html`, html, err => {
if (err) {
console.error(err);
}
});
});
console.log(`Themd build in /dist in ${Date.now() - start} ms`)
});
}
}