Skip to content

Commit 9a68f5f

Browse files
committed
feat(cli): expose watchOptions
Closes #84.
1 parent 58d923a commit 9a68f5f

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Options:
5555
-h, --help ............... Output usage information
5656
-v, --version ............ Output version
5757
-w, --watch .............. Watch the current file(s) for changes
58+
--watch-options .......... Options for Chokidar's watch call
5859
--basedir ................ Base directory to be served by the file server
5960
--stylesheet ............. Path to a local or remote stylesheet (can be passed multiple times)
6061
--css .................... String of styles
@@ -90,6 +91,12 @@ _Tip: You can concatenate multiple files using `cat file1.md file2.md`._
9091

9192
The current working directory (`process.cwd()`) serves as the base directory of the file server by default. This can be adjusted with the `--basedir` flag (or equivalent config option).
9293

94+
#### Watch Mode
95+
96+
Watch mode (`--watch`) uses Chokidar's `watch` method on the markdown file. If you're having issues, you can adjust the watch options via the config (`watch_options`) or `--watch-options` CLI arg. The `awaitWriteFinish` option might be particularly useful if you use editor plugins (e. g. TOC generators) that modify and save the file after the initial save. Check out the [Chokidar docs](https://github.com/paulmillr/chokidar#api) for a full list of options.
97+
98+
Note that Preview on macOS does not automatically reload the preview when the file has changed (or at least not reliably). There are PDF viewers available that can check for file changes and offer auto-reload (e. g. [Skim](https://skim-app.sourceforge.io/)'s "Sync" feature).
99+
93100
#### Programmatic API
94101

95102
The programmatic API is very simple: it only exposes one function that accepts either a `path` to or `content` of a markdown file, and an optional config object (which can be used to specify the output file destination).

src/cli.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import arg from 'arg';
77
import chalk from 'chalk';
8-
import { watch } from 'chokidar';
8+
import { watch, WatchOptions } from 'chokidar';
99
import getPort from 'get-port';
1010
import getStdin from 'get-stdin';
1111
import Listr from 'listr';
@@ -26,6 +26,7 @@ export const cliFlags = arg({
2626
'--version': Boolean,
2727
'--basedir': String,
2828
'--watch': Boolean,
29+
'--watch-options': String,
2930
'--stylesheet': [String],
3031
'--css': String,
3132
'--body-class': [String],
@@ -144,7 +145,11 @@ async function main(args: typeof cliFlags, config: Config) {
144145
if (args['--watch']) {
145146
console.log(chalk.bgBlue('\n watching for changes \n'));
146147

147-
watch(files).on('change', async (file) =>
148+
const watchOptions = args['--watch-options']
149+
? (JSON.parse(args['--watch-options']) as WatchOptions)
150+
: config.watch_options;
151+
152+
watch(files, watchOptions).on('change', async (file) =>
148153
new Listr([getListrTask(file)], { exitOnError: false }).run().catch(console.error),
149154
);
150155
} else {

src/lib/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WatchOptions } from 'chokidar';
12
import { MarkedOptions } from 'marked';
23
import { resolve } from 'path';
34
import { LaunchOptions, PDFOptions, ScriptTagOptions } from 'puppeteer';
@@ -133,4 +134,11 @@ interface BasicConfig {
133134
* Port to run the local server on.
134135
*/
135136
port?: number;
137+
138+
/**
139+
* Options to pass to Chokidar's `watch` call.
140+
*
141+
* This is specifically useful when running into issues when editor plugins trigger additional saves after the initial save.
142+
*/
143+
watch_options?: WatchOptions;
136144
}

src/lib/help.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const helpText = `
88
-h, --help ${chalk.dim('...............')} Output usage information
99
-v, --version ${chalk.dim('............')} Output version
1010
-w, --watch ${chalk.dim('..............')} Watch the current file(s) for changes
11+
--watch-options ${chalk.dim('..........')} Options for Chokidar's watch call
1112
--basedir ${chalk.dim('................')} Base directory to be served by the file server
1213
--stylesheet ${chalk.dim('.............')} Path to a local or remote stylesheet (can be passed multiple times)
1314
--css ${chalk.dim('....................')} String of styles
@@ -38,6 +39,14 @@ const helpText = `
3839
3940
${chalk.cyan('$ md2pdf ./**/*.md')}
4041
42+
${chalk.gray('–')} Convert and enable watch mode
43+
44+
${chalk.cyan('$ md2pdf ./*.md --watch')}
45+
46+
${chalk.gray('–')} Convert and enable watch mode with custom options
47+
48+
${chalk.cyan('$ md2pdf ./*.md --watch --watch-options \'{ "atomic": true }\'')}
49+
4150
${chalk.gray('–')} Convert path/to/file.md with a different base directory
4251
4352
${chalk.cyan('$ md2pdf path/to/file.md --basedir path')}

0 commit comments

Comments
 (0)