-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
index.js
executable file
·169 lines (137 loc) · 4.48 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node
// --
// Packages
const path = require('path');
const arg = require('arg');
const chalk = require('chalk').default;
const Listr = require('listr');
const grayMatter = require('gray-matter');
const getPort = require('get-port');
const { watch } = require('chokidar');
// --
// Utils
const help = require('./util/help');
const getMdFilesInDir = require('./util/get-md-files-in-dir');
const readFile = require('./util/read-file');
const serveDirectory = require('./util/serve-dir');
const getHtml = require('./util/get-html');
const writePdf = require('./util/write-pdf');
const config = require('./util/config');
const { getMarginObject, getDir } = require('./util/helpers');
// --
// Configure CLI Arguments
const args = arg({
'--help': Boolean,
'--version': Boolean,
'--watch': Boolean,
'--stylesheet': [String],
'--css': String,
'--body-class': [String],
'--highlight-style': String,
'--marked-options': String,
'--html-pdf-options': String,
'--pdf-options': String,
'--md-file-encoding': String,
'--stylesheet-encoding': String,
'--config-file': String,
'--devtools': Boolean,
'--debug': Boolean,
// aliases
'-h': '--help',
'-v': '--version',
'-w': '--watch',
});
// --
// Main
async function main(args, config) {
const input = args._[0];
const output = args._[1];
if (args['--version']) {
return console.log(require('./package').version);
}
if (args['--help']) {
return help();
}
// throw warning when using --html-pdf-options flag
if (args['--html-pdf-options']) {
console.warn(
[
chalk.red(`--html-pdf-options is not a valid argument anymore. Use --pdf-options instead.`),
chalk.gray(`valid options: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions`),
].join('\n'),
);
}
const mdFiles = input ? [input] : await getMdFilesInDir('.');
if (mdFiles.length === 0) {
return help();
}
// merge config from config file
if (args['--config-file']) {
try {
config = { ...config, ...require(path.resolve(args['--config-file'])) };
} catch (error) {
console.warn(chalk.red(`Warning: couldn't read config file: ${args['--config-file']}`));
}
}
// serve directory of first file because all files will be in the same dir
const port = await getPort();
const server = await serveDirectory(getDir(mdFiles[0]), port);
const getListrTask = mdFile => ({
title: `generating PDF from ${chalk.underline(mdFile)}`,
task: () => convertToPdf(mdFile),
});
// create list of tasks and run concurrently
await new Listr(mdFiles.map(getListrTask), { concurrent: true, exitOnError: false })
.run()
.then(() => {
if (args['--watch']) {
console.log(chalk.bgBlue('\n watching for changes \n'));
watch(mdFiles).on('change', async mdFile => {
await new Listr([getListrTask(mdFile)]).run().catch(error => args['--debug'] && console.error(error));
});
} else {
server.close();
}
})
.catch(error => (args['--debug'] && console.error(error)) || process.exit(1));
// this is the actual function to convert a file
async function convertToPdf(mdFile) {
const mdFileContent = await readFile(path.resolve(mdFile), args['--md-file-encoding'] || config.md_file_encoding);
const { content: md, data: frontMatterConfig } = grayMatter(mdFileContent);
// merge front-matter config
config = { ...config, ...frontMatterConfig };
// sanitize array cli arguments
for (const option of ['stylesheet', 'body_class']) {
if (!Array.isArray(config[option])) {
config[option] = [config[option]].filter(value => Boolean(value));
}
}
// merge cli args into config
const jsonArgs = ['--marked-options', 'pdf-options'];
for (const arg of Object.entries(args)) {
const [argKey, argValue] = arg;
const key = argKey.substring(2).replace(/-/g, '_');
config[key] = jsonArgs.includes(argKey) ? JSON.parse(argValue) : argValue;
}
// sanitize the margin in pdf_options
if (typeof config.pdf_options.margin === 'string') {
config.pdf_options.margin = getMarginObject(config.pdf_options.margin);
}
const highlightStylesheet = path.resolve(
__dirname,
'node_modules',
'highlight.js',
'styles',
`${config.highlight_style}.css`,
);
config.stylesheet = [...new Set([...config.stylesheet, highlightStylesheet])];
const html = getHtml(md, config);
const pdf = await writePdf(mdFile, output, html, { ...config, port });
if (!pdf.filename) {
throw new Error(`Failed to create PDF`);
}
}
}
// --
// Run
main(args, config).catch(error => console.error(error) || process.exit(1));