Skip to content

Commit bb46626

Browse files
committed
fix: relative paths
1 parent 7de6435 commit bb46626

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ The pdf is generated into the same directory as the source file and uses the sam
7878
md-to-pdf ./**/*.md
7979
```
8080

81+
_(You might need to enable the `globstar` option in bash for recursive globbing)_
82+
8183
Alternatively, you can pass the markdown in from `stdin`:
8284

8385
```sh

src/lib/get-output-file-path.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { parse, join } from 'path';
2-
import { Config } from './config';
1+
import { parse, resolve } from 'path';
32

43
/**
5-
* Derive the output file path from the source markdown file.
4+
* Derive the output file path from a source file.
65
*/
7-
export const getOutputFilePath = (mdFilePath: string, config: Partial<Config>) => {
6+
export const getOutputFilePath = (mdFilePath: string, extension: 'html' | 'pdf') => {
87
const { dir, name } = parse(mdFilePath);
9-
const extension = config.as_html ? 'html' : 'pdf';
108

11-
return join(dir, `${name}.${extension}`);
9+
return resolve(dir, `${name}.${extension}`);
1210
};

src/lib/md-to-pdf.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import { writeOutput } from './write-output';
1212
* Convert markdown to pdf.
1313
*/
1414
export const convertMdToPdf = async (input: { path: string } | { content: string }, config: Config, args: any = {}) => {
15-
const mdContent =
15+
const mdFileContent =
1616
'content' in input
1717
? input.content
18-
: await readFile(resolve(input.path), args['--md-file-encoding'] || config.md_file_encoding);
18+
: await readFile(input.path, args['--md-file-encoding'] || config.md_file_encoding);
1919

20-
const { content: md, data: frontMatterConfig } = grayMatter(mdContent);
20+
const { content: md, data: frontMatterConfig } = grayMatter(mdFileContent);
2121

2222
// merge front-matter config
2323
config = {
@@ -57,7 +57,7 @@ export const convertMdToPdf = async (input: { path: string } | { content: string
5757
if (!config.dest) {
5858
config.dest =
5959
'path' in input
60-
? getOutputFilePath(input.path, config)
60+
? getOutputFilePath(input.path, config.as_html ? 'html' : 'pdf')
6161
: resolve(process.cwd(), `output.${config.as_html ? 'html' : 'pdf'}`);
6262
}
6363

@@ -72,7 +72,9 @@ export const convertMdToPdf = async (input: { path: string } | { content: string
7272

7373
const html = getHtml(md, config);
7474

75-
const output = await writeOutput(mdContent, html, config);
75+
const relativePath = 'path' in input ? resolve(input.path).replace(config.basedir, '') : '/';
76+
77+
const output = await writeOutput(html, relativePath, config);
7678

7779
if (!('filename' in output)) {
7880
throw new Error(`Failed to create ${config.as_html ? 'HTML' : 'PDF'}.`);

src/lib/write-output.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { writeFile as fsWriteFile } from 'fs';
22
import { promisify } from 'util';
33
import puppeteer from 'puppeteer';
4-
import { getOutputFilePath } from './get-output-file-path';
54
import { isHttpUrl } from './is-http-url';
65
import { Config } from './config';
76

@@ -13,15 +12,19 @@ const writeFile = promisify(fsWriteFile);
1312
* The reason that relative paths are resolved properly is that the base dir is served locally
1413
*/
1514
export const writeOutput = async (
16-
mdContent: string,
1715
html: string,
16+
relativePath: string,
1817
config: Config,
1918
): Promise<{} | { filename: string; content: string | Buffer }> => {
19+
if (!config.dest) {
20+
throw new Error('No output file destination has been specified.');
21+
}
22+
2023
const browser = await puppeteer.launch({ devtools: config.devtools, ...config.launch_options });
2124

2225
const page = await browser.newPage();
2326

24-
await page.goto(`http://localhost:${config.port}`);
27+
await page.goto(`http://localhost:${config.port}${relativePath}`); // make sure relative paths work as expected
2528
await page.setContent(html); // overwrite the page content with what was generated from the markdown
2629

2730
await Promise.all([
@@ -42,26 +45,22 @@ export const writeOutput = async (
4245
page.evaluate(() => history.pushState(undefined, '', '#')),
4346
]);
4447

45-
/**
46-
* @todo should it be `getOutputFilePath(config.dest || mdFilePath, config)`?
47-
*/
48-
const outputFilePath = config.dest || getOutputFilePath(mdContent, config);
49-
5048
let outputFileContent: string | Buffer = '';
5149

5250
if (config.devtools) {
5351
await new Promise(resolve => page.on('close', resolve));
54-
} else if (config.as_html) {
55-
outputFileContent = await page.content();
56-
await writeFile(outputFilePath, outputFileContent);
5752
} else {
58-
await page.emulateMediaType('screen');
59-
outputFileContent = await page.pdf(config.pdf_options);
53+
if (config.as_html) {
54+
outputFileContent = await page.content();
55+
} else {
56+
await page.emulateMediaType('screen');
57+
outputFileContent = await page.pdf(config.pdf_options);
58+
}
6059

61-
await writeFile(outputFilePath, outputFileContent);
60+
await writeFile(config.dest, outputFileContent);
6261
}
6362

6463
await browser.close();
6564

66-
return config.devtools ? {} : { filename: outputFilePath, content: outputFileContent };
65+
return config.devtools ? {} : { filename: config.dest, content: outputFileContent };
6766
};

src/test/lib.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ test('getMarked should accept a custom renderer with custom code highlighter', t
105105
test('getOutputFilePath should return the same path but with different extension', t => {
106106
const mdFilePath = posix.join('/', 'var', 'foo', 'bar.md');
107107

108-
t.is(getOutputFilePath(mdFilePath, {}), `${sep}var${sep}foo${sep}bar.pdf`);
109-
t.is(getOutputFilePath(mdFilePath, { as_html: true }), `${sep}var${sep}foo${sep}bar.html`);
108+
t.is(getOutputFilePath(mdFilePath, 'pdf'), `${sep}var${sep}foo${sep}bar.pdf`);
109+
t.is(getOutputFilePath(mdFilePath, 'html'), `${sep}var${sep}foo${sep}bar.html`);
110110
});
111111

112112
// --

0 commit comments

Comments
 (0)