Skip to content

Commit

Permalink
feat: expose puppeteer launch options
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhaenisch committed Nov 14, 2018
1 parent f7b16af commit aa36a5e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 39 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const args = arg({
'--marked-options': String,
'--html-pdf-options': String,
'--pdf-options': String,
'--launch-options': String,
'--md-file-encoding': String,
'--stylesheet-encoding': String,
'--config-file': String,
Expand Down Expand Up @@ -64,7 +65,10 @@ async function main(args, config) {
return help();
}

// throw warning when using --html-pdf-options flag
/**
* throw warning when using --html-pdf-options flag
* @todo remove in a future version
*/
if (args['--html-pdf-options']) {
console.warn(
[
Expand Down Expand Up @@ -135,7 +139,7 @@ async function main(args, config) {
}

// merge cli args into config
const jsonArgs = ['--marked-options', '--pdf-options'];
const jsonArgs = ['--marked-options', '--pdf-options', '--launch-options'];
for (const arg of Object.entries(args)) {
const [argKey, argValue] = arg;
const key = argKey.substring(2).replace(/-/g, '_');
Expand Down
37 changes: 20 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,21 @@ $ md-to-pdf [options] [path/to/file.md] [path/to/output.pdf]
Options:
-h, --help Output usage information
-v, --version Output version
-w, --watch Watch the current file(s) for changes
--stylesheet Path to a local or remote stylesheet (can be passed multiple times)
--css String of styles (can be used to overwrite stylesheets)
--body-class Classes to be added to the body tag (can be passed multiple times)
--highlight-style Style to be used by highlight.js (default: github)
--marked-options Set custom options for marked (as a JSON string)
--pdf-options Set custom options for the generated PDF (as a JSON string)
--md-file-encoding Set the file encoding for the markdown file
--stylesheet-encoding Set the file encoding for the stylesheet
--config-file Path to a JSON or JS configuration file
--devtools Open the browser with devtools instead of creating PDF
--debug Show more output on errors
-h, --help ............... Output usage information
-v, --version ............ Output version
-w, --watch .............. Watch the current file(s) for changes
--stylesheet ............. Path to a local or remote stylesheet (can be passed multiple times)
--css .................... String of styles (can be used to overwrite stylesheets)
--body-class ............. Classes to be added to the body tag (can be passed multiple times)
--highlight-style ........ Style to be used by highlight.js (default: github)
--marked-options ......... Set custom options for marked (as a JSON string)
--pdf-options ............ Set custom options for the generated PDF (as a JSON string)
--launch-options ......... Set custom launch options for Puppeteer
--md-file-encoding ....... Set the file encoding for the markdown file
--stylesheet-encoding .... Set the file encoding for the stylesheet
--config-file ............ Path to a JSON or JS configuration file
--devtools ............... Open the browser with devtools instead of creating PDF
--debug .................. Show more output on errors
```

If no arguments are given, all markdown files in the current directory will be converted. Otherwise, the first argument is `path/to/file.md` and the second one optionally specifies the `path/to/output.pdf`. If you omit the second argument, it will derive the pdf name from the markdown filename and save it into the same directory that contains the markdown file. Run `md2pdf --help` for examples on how to use the cli options.
Expand Down Expand Up @@ -105,7 +106,7 @@ pdf_options:
---
```

Refer to the [puppeteer docs](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions) for more info about headers and footers.
Refer to the [Puppeteer docs](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions) for more info about headers and footers.

#### Default and Advanced Options

Expand All @@ -115,6 +116,7 @@ For advanced options see the following links:

* [Marked Advanced Options](https://github.com/markedjs/marked/blob/master/docs/USING_ADVANCED.md)
* [Puppeteer PDF Options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions)
* [Puppeteer Launch Options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions)
* [highlight.js Styles](https://github.com/isagalaev/highlight.js/tree/master/src/styles)

## Options
Expand All @@ -125,8 +127,9 @@ For advanced options see the following links:
| `--css` | `body { color: tomato; }` |
| `--body_class` | `markdown-body` |
| `--highlight-style` | `monokai`, `solarized-light` |
| `--marked-options` | `'{"gfm": false }'` |
| `--pdf-options` | `'{"format": "Letter", margin: "20mm" }'` |
| `--marked-options` | `'{ "gfm": false }'` |
| `--pdf-options` | `'{ "format": "Letter", margin: "20mm" }'` |
| `--launch-options` | `'{ "args": ["--no-sandbox"] }'` |
| `--md-file-encoding` | `utf-8`, `windows1252` |
| `--stylesheet-encoding` | `utf-8`, `windows1252` |
| `--config-file` | `path/to/config.json` |
Expand Down
7 changes: 7 additions & 0 deletions util/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ module.exports = {
},
},

/**
* Launch options for Puppeteer.
*
* @see https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions
*/
launch_options: {},

/**
* Markdown file encoding.
*/
Expand Down
39 changes: 20 additions & 19 deletions util/help.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
const chalk = require('chalk');
const chalk = require('chalk').default;

const help = `
${chalk.bold('$ md-to-pdf')} [options] [path/to/file.md] [path/to/output.pdf]
${chalk.dim('Options:')}
-h, --help Output usage information
-v, --version Output version
-w, --watch Watch the current file(s) for changes
--stylesheet Path to a local or remote stylesheet (can be passed multiple times)
--css String of styles (can be used to overwrite stylesheets)
--body-class Classes to be added to the body tag (can be passed multiple times)
--highlight-style Style to be used by highlight.js (default: github)
--marked-options Set custom options for marked (as a JSON string)
--pdf-options Set custom options for the generated PDF (as a JSON string)
--md-file-encoding Set the file encoding for the markdown file
--stylesheet-encoding Set the file encoding for the stylesheet
--config-file Path to a JSON or JS configuration file
--devtools Open the browser with devtools instead of creating PDF
--debug Show more output on errors
${chalk.dim('Examples:')}
${chalk.dim.underline.bold('Options:')}
-h, --help ${chalk.dim('...............')} Output usage information
-v, --version ${chalk.dim('............')} Output version
-w, --watch ${chalk.dim('..............')} Watch the current file(s) for changes
--stylesheet ${chalk.dim('.............')} Path to a local or remote stylesheet (can be passed multiple times)
--css ${chalk.dim('....................')} String of styles (can be used to overwrite stylesheets)
--body-class ${chalk.dim('.............')} Classes to be added to the body tag (can be passed multiple times)
--highlight-style ${chalk.dim('........')} Style to be used by highlight.js (default: github)
--marked-options ${chalk.dim('.........')} Set custom options for marked (as a JSON string)
--pdf-options ${chalk.dim('............')} Set custom options for the generated PDF (as a JSON string)
--launch-options ${chalk.dim('.........')} Set custom launch options for Puppeteer
--md-file-encoding ${chalk.dim('.......')} Set the file encoding for the markdown file
--stylesheet-encoding ${chalk.dim('....')} Set the file encoding for the stylesheet
--config-file ${chalk.dim('............')} Path to a JSON or JS configuration file
--devtools ${chalk.dim('...............')} Open the browser with devtools instead of creating PDF
--debug ${chalk.dim('..................')} Show more output on errors
${chalk.dim.underline.bold('Examples:')}
${chalk.gray('–')} Convert all markdown files in current directory
Expand Down
4 changes: 3 additions & 1 deletion util/write-pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ const getPdfFilePath = require('./get-pdf-file-path');
* @param {string} [outputPath] path that the PDF will be written to
* @param {string} html HTML document as a string
* @param {Object} config configuration object
* @param {number} config.port port that the server runs on
* @param {string[]} config.stylesheet list of stylesheets (urls or paths)
* @param {string} config.css string with CSS rules
* @param {Object} config.pdf_options PDF options for Puppeteer
* @param {boolean} config.devtools show the Devtools instead of saving the PDF
* @param {puppeteer.LaunchOptions} config.launch_options browser launch options
*
* @returns a promise that resolves once the file is written and contains the
* pdf's filename
*/
module.exports = async (mdFilePath, outputPath, html, config) => {
const pdfFilePath = outputPath || getPdfFilePath(mdFilePath);

const browser = await puppeteer.launch({ devtools: config.devtools });
const browser = await puppeteer.launch({ devtools: config.devtools, ...config.launch_options });

const page = await browser.newPage();

Expand Down

0 comments on commit aa36a5e

Please sign in to comment.