Overview
I created a simple document management tool to meet the demand for converting Markdown files to PDF.
bmf-san/docs-md-to-pdf-example
I used existing libraries without much thought, so the structure doesn't feel very sustainable.
Motivation
If you just want to convert Markdown files to PDF, you can simply use a library called md-to-pdf.
This library is also helpful for managing resumes. cf. Managing Resumes on Github
I wanted to create something that supports mermaid notation and emojis not registered in unicode.
Using the vscode-markdown-pdf extension for VSCode can easily solve this, but it requires VSCode, which some people may need to install.
I thought using VSCode just for conversion was nonsensical, so I implemented it.
Design
The library md-to-pdf is easy to use and great, but it currently doesn't support the following features by default:
- Mermaid notation
- Emojis (other than those registered in unicode)
- TOC generation
Since md-to-pdf allows configuration extensions with markedjs/marked, it seems possible to achieve these by customizing md-to-pdf.
There seems to be a plan to support TOC. Generate TOC (table of contents) #74
Although using md-to-pdf was an option, it seemed a bit cumbersome, so I wanted to implement it quickly like a hackathon and decided to use a library called md-to-pdf-ng.
This library extends md-to-pdf to support mermaid notation. It doesn't seem to be well-maintained, but it works without issues.
Based on md-to-pdf-ng, emoji support is achieved using node-emojify, and TOC generation is achieved using a library called doctoc.
Implementation
Install the following via npm:
Note: textlint is also included as a bonus, but that part is omitted.
Emoji support is handled by extending marked, so prepare a configuration file like this:
const marked = require('marked');
const { emojify } = require('node-emoji');
const renderer = new marked.Renderer();
renderer.text = emojify;
module.exports = {
marked_options: { renderer },
};
Define the following command in the scripts section of package.json:
doctoc --notitle md/ && md-to-pdf md/*.md --config-file config.js && mv md/*.pdf pdf/
First, generate the TOC with doctoc, then convert Markdown to PDF, and finally move the directory.
It would be nice if the output destination of the pdf generated by md-to-pdf could be specified by directory, but since there doesn't seem to be such an option, I'm handling it with a straightforward method: mv md/*.pdf pdf/.
Thoughts
When trying to create something like this, you tend to rely heavily on external libraries. Ideally, I'd like to implement everything myself, but it seems quite challenging. If I get the chance, I'd like to learn about PDF data structures or create a similar CLI tool in Go.