-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathconvert.js
96 lines (86 loc) · 2.89 KB
/
convert.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
var showdown = require('showdown');
var fs = require('fs');
let filename = process.argv[4] || "README.md"
let pageTitle = process.argv[2] || ""
let plausibleDomain = process.argv[3] || ""
var hljs = require ('highlight.js');
showdown.extension('highlight', function () {
function htmlunencode(text) {
return (
text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
);
}
return [{
type: "output",
filter: function (text, converter, options) {
var left = "<pre><code\\b[^>]*>",
right = "</code></pre>",
flags = "g";
var replacement = function (wholeMatch, match, left, right) {
match = htmlunencode(match);
var lang = (left.match(/class=\"([^ \"]+)/) || [])[1];
left = left.slice(0, 18) + 'hljs ' + left.slice(18);
if (lang && hljs.getLanguage(lang)) {
return left + hljs.highlight(match, {language: lang}).value + right;
} else {
return left + hljs.highlightAuto(match).value + right;
}
};
return showdown.helper.replaceRecursiveRegExp(text, replacement, left, right, flags);
}
}];
});
fs.readFile(__dirname + '/style.css', function (err, styleData) {
fs.readFile(__dirname + '/node_modules/highlight.js/styles/atom-one-dark.css', function(err, highlightingStyles) {
fs.readFile(process.cwd() + '/' + filename, function (err, data) {
if (err) {
throw err;
}
let text = data.toString();
converter = new showdown.Converter({
ghCompatibleHeaderId: true,
simpleLineBreaks: true,
ghMentions: true,
extensions: ['highlight'],
tables: true
});
var preContent = `
<html>
<head>
<title>` + pageTitle + `</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">`
if (plausibleDomain.length > 0) {
preContent += `
<script defer data-domain="` + plausibleDomain + `" src="https://plausible.io/js/script.js"></script>
`
}
preContent += `
</head>
<body>
<div id='content'>
`
let postContent = `
</div>
<style type='text/css'>` + styleData + `</style>
<style type='text/css'>` + highlightingStyles + `</style>
</body>
</html>`;
html = preContent + converter.makeHtml(text) + postContent
converter.setFlavor('github');
// console.log(html);
let markdownFileNameWithoutPath = filename.replace(".md", ".html")
let filePath = process.cwd() + "/" + markdownFileNameWithoutPath
fs.writeFile(filePath, html, { flag: "wx" }, function(err) {
if (err) {
console.log("File '" + filePath + "' already exists. Aborted!");
} else {
console.log("Done, saved to " + filePath);
}
});
});
});
});