-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
index.ts
executable file
·43 lines (32 loc) · 1.02 KB
/
index.ts
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
#!/usr/bin/env node
import getPort from 'get-port';
import { defaultConfig, Config } from './lib/config';
import { serveDirectory } from './lib/serve-dir';
import { convertMdToPdf } from './lib/md-to-pdf';
import { getDir } from './lib/helpers';
/**
* Convert a markdown file to PDF.
*
* @returns the path that the PDF was written to
*/
export const mdToPdf = async (input: { path: string } | { content: string }, config: Partial<Config> = {}) => {
if (!('path' in input ? input.path : input.content)) {
throw new Error('Specify either content or path.');
}
if (!config.port) {
config.port = await getPort();
}
if (!config.basedir) {
config.basedir = 'path' in input ? getDir(input.path) : process.cwd();
}
const mergedConfig: Config = {
...defaultConfig,
...config,
pdf_options: { ...defaultConfig.pdf_options, ...config.pdf_options },
};
const server = await serveDirectory(mergedConfig);
const pdf = await convertMdToPdf(input, mergedConfig);
server.close();
return pdf;
};
export default mdToPdf;