Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getDir } from './lib/helpers';
import { convertMdToPdf } from './lib/md-to-pdf';
import { serveDirectory } from './lib/serve-dir';

type Input = ContentInput | PathInput;
type Input = ContentInput | PathInput | PathsInput;

interface ContentInput {
content: string;
Expand All @@ -17,17 +17,24 @@ interface PathInput {
path: string;
}

interface PathsInput {
paths: string[];
}

const hasContent = (input: Input): input is ContentInput => 'content' in input;
const hasPath = (input: Input): input is PathInput => 'path' in input;
const hasPaths = (input: Input): input is PathsInput => 'paths' in input;

/**
* Convert a markdown file to PDF.
*/
export async function mdToPdf(input: ContentInput | PathInput, config?: Partial<PdfConfig>): Promise<PdfOutput>;
export async function mdToPdf(input: ContentInput | PathInput, config?: Partial<HtmlConfig>): Promise<HtmlOutput>;
export async function mdToPdf(input: Input, config: Partial<Config> = {}): Promise<Output> {
if (!hasContent(input) && !hasPath(input)) {
throw new Error('The input is missing one of the properties "content" or "path".');
export async function mdToPdf(input: PathsInput, config?: Partial<PdfConfig>): Promise<PdfOutput[]>;
export async function mdToPdf(input: PathsInput, config?: Partial<HtmlConfig>): Promise<HtmlOutput[]>;
export async function mdToPdf(input: Input, config: Partial<Config> = {}): Promise<Output | Output[]> {
if (!hasContent(input) && !hasPath(input) && !hasPaths(input)) {
throw new Error('The input is missing one of the properties "content", "path" or "paths".');
}

if (!config.port) {
Expand All @@ -38,7 +45,11 @@ export async function mdToPdf(input: Input, config: Partial<Config> = {}): Promi
config.basedir = 'path' in input ? getDir(input.path) : process.cwd();
}

if (!config.dest) {
if (hasPaths(input) && config.dest) {
console.warn('WARNING: config.dest will be ignored when converting multiple files.');
}

if (!config.dest || hasPaths(input)) {
config.dest = '';
}

Expand All @@ -50,11 +61,14 @@ export async function mdToPdf(input: Input, config: Partial<Config> = {}): Promi

const server = await serveDirectory(mergedConfig);

const pdf = await convertMdToPdf(input, mergedConfig);
const result =
hasContent(input) || hasPath(input)
? await convertMdToPdf(input, mergedConfig)
: await Promise.all(input.paths.map(async (path) => convertMdToPdf({ path }, mergedConfig)));

server.close();

return pdf;
return result;
}

export default mdToPdf;
Expand Down
14 changes: 14 additions & 0 deletions src/test/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,17 @@ test('compile the MathJax test', async (t) => {
t.true(text.startsWith('Formulas with MathJax'));
t.true(text.includes('a≠0'));
});

test('compile multiple files to pdf', async (t) => {
const testFile = resolve(__dirname, 'basic', 'test.md');

const pdfs = await mdToPdf({ paths: [testFile, testFile] });

t.is(pdfs.length, 2);

t.is(pdfs[0].filename, '');
t.is(pdfs[1].filename, '');

t.truthy(pdfs[0].content instanceof Buffer);
t.truthy(pdfs[1].content instanceof Buffer);
});
8 changes: 7 additions & 1 deletion src/test/api.type-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import { HtmlOutput, PdfOutput } from '../lib/generate-output';
(async () => {
expectType<PdfOutput>(await mdToPdf({ content: 'foo' }));
expectType<PdfOutput>(await mdToPdf({ path: 'foo.md' }));

expectType<PdfOutput[]>(await mdToPdf({ paths: ['foo.md'] }));
expectType<PdfOutput>(await mdToPdf({ path: 'foo.md' }, { as_html: false }));

expectType<HtmlOutput>(await mdToPdf({ content: 'foo' }, { as_html: true }));
expectType<HtmlOutput>(await mdToPdf({ path: 'foo.md' }, { as_html: true }));
expectType<HtmlOutput[]>(await mdToPdf({ paths: ['foo.md'] }, { as_html: true }));

expectType<string | undefined>((await mdToPdf({ content: 'foo' })).filename);

expectType<Buffer>((await mdToPdf({ content: 'foo' })).content);
expectType<string>((await mdToPdf({ content: 'foo' }, { as_html: true })).content);
})();