-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
index.js
executable file
·31 lines (24 loc) · 868 Bytes
/
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
const getPort = require('get-port');
const defaultConfig = require('./lib/config');
const serveDirectory = require('./lib/serve-dir');
const { getDir } = require('./lib/helpers');
const mdToPdf = require('./lib/md-to-pdf');
/**
* Convert a markdown file to PDF.
*
* @param {string} mdFile path to markdown file
* @param {*} [config] config object
*
* @returns the path that the PDF was written to
*/
module.exports = async (mdFile, config = {}) => {
if (typeof mdFile !== 'string') {
throw new TypeError(`mdFile has to be a string, received ${typeof mdFile}`);
}
const port = await getPort();
const server = await serveDirectory(getDir(mdFile), port);
config = { ...defaultConfig, ...config, pdf_options: { ...defaultConfig.pdf_options, ...config.pdf_options } };
const pdf = await mdToPdf(mdFile, config, port);
server.close();
return pdf;
};