Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow Links to Combine Documents #34

Closed
shellscape opened this issue May 1, 2019 · 6 comments · Fixed by #39
Closed

Follow Links to Combine Documents #34

shellscape opened this issue May 1, 2019 · 6 comments · Fixed by #39

Comments

@shellscape
Copy link

I'd like to request the ability to instruct the parser to follow file links to other markdown files, to combine multiple documents into one.

@simonhaenisch
Copy link
Owner

Thanks for the request 🙂 However I think it might be out of scope for this package.

Depending on the platform you're on, it's relatively straight-forward to combine multiple PDFs into one (e. g. on macOS you can use Preview.app and drag-and-drop them together).

There's also lots of apps and other npm packages out there to merge PDFs... try searching for "pdf merge" on npm. Can you please try see if there's any simple solution for this by using another package/cli tool/app?

@TabithaLarkin
Copy link

It might be cool if you could pass in a stream into md-to-pdf such as:

concat 1.md 2.md 3.md | md-to-pdf joined.pdf

Otherwise @shellscape you could use concat to join the files to an intermediate one, and then use that in md-to-pdf.

@simonhaenisch
Copy link
Owner

@StephenLarkin reading from stdin is one thing I want to implement in the next major release 👍 But it'll probably take a bit till I have the time to do it...

@simonhaenisch
Copy link
Owner

simonhaenisch commented Jan 14, 2020

After @gigaga asked for something similar (he suggested a [include](file.md) syntax), one other idea that I had was to use a custom renderer for Marked, because it's already possible to extend the renderer using the marked_options of the config:

const { readFileSync } = require('fs');
const { Renderer } = require('marked');
const getMarked = require('md-to-pdf/lib/get-marked-with-highlighter');

const renderer = new Renderer();

const originalLinkRenderer = renderer.link.bind(renderer);

renderer.link = (href, title, text) => {
  if (text !== 'include') {
    return originalLinkRenderer(href, title, text);
  }

  // 1. read file
  const md = readFileSync(href, 'utf-8');

  // 2. get marked instance that's equivalent to the one from md-to-pdf
  const marked = getMarked(marked_options);

  // 3. convert file to html string
  return marked(md);
};

const marked_options = { renderer };

module.exports = { marked_options };

I made a Gist that you can clone if you want to try it out:

git clone https://gist.github.com/b5c935e76052a40b1e32bfe28673b188 md-to-pdf-includes
cd md-to-pdf-includes
npm install
npm start
open test.pdf

@gigaga
Copy link

gigaga commented Jan 14, 2020

Thanks a lot! It's perfect!!

@simonhaenisch
Copy link
Owner

I just made a prerelease of version 3, you can try it out with npm i -g md-to-pdf@next. With that it's now possible to do the above mentioned concatenating:

concat 1.md 2.md 3.md | md-to-pdf

It will generate a file output.pdf in the current working directory. It's not yet possible to pipe the generated pdf into a file, but this will be implemented before the release of v3. Then you can do

concat 1.md 2.md 3.md | md-to-pdf > my-file.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants