11import { writeFile as fsWriteFile } from 'fs' ;
22import { promisify } from 'util' ;
33import puppeteer from 'puppeteer' ;
4- import { getOutputFilePath } from './get-output-file-path' ;
54import { isHttpUrl } from './is-http-url' ;
65import { 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 */
1514export 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} ;
0 commit comments