-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (56 loc) · 1.44 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
const fs = require('fs');
const MarkdownIt = require('markdown-it');
const HighLightJS = require('highlight.js');
// template of index.html
const template = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>markdown.css</title>
<link rel="stylesheet" href="./node_modules/highlight.js/styles/default.css">
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./markdown.css">
</head>
<body>
<article>
$content$
</article>
</body>
</html>
`;
const MD = new MarkdownIt({
html: true,
highlight: (content, lang) => {
if (lang
&& HighLightJS.getLanguage(lang)) {
try {
return HighLightJS.highlight(lang, content).value;
} catch (error) {
console.log(error);
}
}
return '';
},
});
MD.use(require('markdown-it-sub'));
MD.use(require('markdown-it-sup'));
MD.use(require('markdown-it-deflist'));
MD.use(require('markdown-it-task-lists'));
const build = (mdDir, htmlDir) => {
try {
const content = fs.readFileSync(mdDir, {
encoding: 'utf8',
});
const mdContent = MD.render(content);
const htmlContent = template.replace('$content$', mdContent);
fs.writeFileSync(htmlDir, htmlContent, {
encoding: 'utf8',
});
} catch (error) {
console.log(error);
}
};
build('./markdown.md', './index.html');