simonhaenisch/md-to-pdf

feature: add --dest option to the CLI

bbaia opened this issue · 4 comments

bbaia commented

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

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

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

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 $_ }\"",