-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
executable file
·123 lines (104 loc) · 3.07 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env node
const showdown = require('showdown');
const fs = require('fs-extra');
const { program } = require('commander');
const footnotes = require('showdown-footnotes');
const highlight = require("showdown-highlight");
program
.option(
'-i, --input <filename>',
'The input readme/markdown file',
'README.md'
)
.option(
'-d, --dir <dirname>',
'The output directory',
"./dist"
)
.option('-o, --output <filename>', 'The output HTML file', 'index.html')
.option(
'-s, --style <mode>',
'The style mode to use, either \'light\' or \'dark\'',
"light"
)
.option('-t, --title <title>', 'The page title', 'Read Me');
program.parse(process.argv);
const options = program.opts();
const readmeFile = './' + options.input;
const styleMode = options.style;
const pageTitle = options.title;
const distDir = options.dir;
const outputFile = distDir + '/' + options.output;
const assetsDirSource = './assets/';
const assetsDirTarget = distDir + '/assets';
// style defaults to 'light'
let cssFileName = 'style.css';
let hljsCssFileName = 'github.css';
if (styleMode === "dark") {
cssFileName = 'style-dark.css';
hljsCssFileName = 'github-dark-dimmed.css';
}
const cssFile = __dirname + '/' + cssFileName; //inside our module
const hljsCssFile = require.resolve('highlight.js/styles/' + hljsCssFileName);
const converter = new showdown.Converter({
ghCompatibleHeaderId: true,
simpleLineBreaks: true,
ghMentions: true,
tables: true,
emoji: true,
parseImgDimensions: true,
extensions: [footnotes, highlight]
});
converter.setFlavor('github');
fs.readFile(cssFile, 'utf-8', (err, cssText) => {
if (err) {
console.error('failed to read', cssFile);
throw err;
}
fs.readFile(hljsCssFile, 'utf-8', (err, hljsCssText) => {
if (err) {
console.error('failed to read', hljsCssFile);
throw err;
}
fs.readFile(readmeFile, 'utf-8', (err, readmetext) => {
if (err) {
console.error('failed to read', readmeFile);
throw err;
}
const preContent = `
<!DOCTYPE html>
<html>
<head>
<title>${pageTitle}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
${cssText}
</style>
<style>
${hljsCssText}
</style>
</head>
<body>
<main>
`;
const postContent = `
</main>
</body>
</html>`;
const html = preContent + converter.makeHtml(readmetext) + postContent;
fs.ensureDirSync(distDir);
fs.writeFile(outputFile, html, { flag: 'w' }, function (err) {
if (err) {
console.log('Failed, could not open', outputFile, err);
} else {
console.log('Done, saved to ' + outputFile);
if (fs.existsSync(assetsDirSource)) {
fs.copySync(assetsDirSource, assetsDirTarget);
console.log('assets copied to ' + assetsDirTarget);
}
}
});
});
});
});