simonhaenisch / md-to-pdf

Hackable CLI tool for converting Markdown files to PDF using Node.js and headless Chrome.
https://www.npmjs.com/md-to-pdf
MIT License
1.12k stars 108 forks source link

feature: add --dest option to the CLI #257

Closed bbaia closed 7 months ago

bbaia commented 10 months ago

Problem:

Setting output path is not trivial:

cat file.md | md-to-pdf > path/to/output.pdf

And cat is not available on Windows.

Solution:

dest is already supported in frontmatter config, why not adding it to CLI

simonhaenisch commented 9 months ago

Hey I think --dest should already work? Maybe I forgot to add it to the list in the help/docs

bbaia commented 9 months ago

unknown or unexpected option: --dest cf https://github.com/simonhaenisch/md-to-pdf/blob/master/src/cli.ts#L24

simonhaenisch commented 9 months ago

Ok i see, i think i remember now why it's not a flag: because it only makes sense if you pass one input file, but you can pass a list (e.g. **/*.md shell glob which is extended to a list of paths of all markdown files found in the current folder recursively, in which case it would infer the output path from each input file path respectively), and for a list of files it's unclear what a single dest should mean.


And cat is not available on Windows.

Use WSL? 🤓


Anyway, you can set dest as an option using the programmatic API which only takes one input at a time:

$ node -e "require('md-to-pdf')({ path: 'path/to/file.md' }, { dest: 'path/to/destination.md' })"

(should work if md-to-pdf is installed globally, -e flag of node is short for --eval to evaluate the given script)


Otherwise, there's two options to implement a --dest flag:

1. --dest has to be a directory path and all files will be generated into it

Difficult because what about naming clashes, e.g. md-to-pdf foo/index.md bar/index.md --dest pdfs? Could create subfolders inside the pdfs folder, inferred from the input paths, but i'm not sure that's what anyone would expect to happen, especially when they convert a single file. Having different output folder structure depending on the number of input files seems a bit confusing, and I like tools that are intuitive 🙃

2. --dest has to be a file path

In that case, passing multiple input files would need to be handled by throwing an error. It's an option I could accept, but I won't have time to implement this myself in the near future.

bbaia commented 7 months ago

OK, I understand your reasons, do not change how it works.

I found a workaround using PowerShell in Windows : "build": "PowerShell \"Get-Item **/*.md | ForEach-Object { npx md-to-pdf --config-file md-to-pdf.config.js $_ }\"",