Generating HTML and PDF from Markdown
[Markdown](https://en.wikipedia.org/wiki/Markdown) is my favorite way to write. It is lightweight, requiring a few characters to convey the meaning of the text, it's supported in both many places, including Github and Wordpress (via Jetpack) so I don't need to change the way I write to publish in different places and it only needs a text editor to create (for me, so does HTML but that's another story). In this article, we'll look at different ways to take Markdown input and convert it to HTML for web view. ## Markdown to HTML in a build process This process is part of the build for my writing process and covers both HTML and PDF generation from the same Markdown source. I've created an HTML template to place the Markdown-produced HTML Inside of. It does three things: 1. Defines the CSS that the document will load 2. Defines the document metadata: charset, viewport, and title 3. Defines the container and the placeholder for the generated HTML 4. Defines the scripts we want the page to run at the bottom of the document. We could also place them on the head and use `defer` but we don't really need to ```html
<%= contents %>
```
The first step is to create the HTML files using the appropriate template after we generate the HTML from the Markdown content.
```js
gulp.task('build-pm-template', () => {
gulp.src('./src/html-content/*.html')
.pipe(wrap({src: './src/templates/template-pm.html'}))
.pipe(gulp.dest('./src/pm-content'));
});
```
The next step is where the differences lie. Instead of just generating the HTML and being done with it, we have to push the HTML through a CSS paged media processor.
I've used [PrinceXML](https://www.princexml.com/) to generate PDF from multiple sources with different inputs (XML, HTML, and XSL-FO) so we're sticking with it for this project.
I use a second stylesheet that has all the font definitions and styles for the document. I've made [article-styles.css](https://gist.github.com/caraya/8d12c8bbfb07681b4d5b56dfeecc88bc) available as Github GIST
The final bit is how we run PrinceXML in the build process. I know that `gulp-exec` is not well liked in the Gulp community but none of the alternatives I've found don't do quite what I needed to, so `gulp-exec` it is.
The idea is that, for each file in the source directory, we run prince with the command given.
```js
gulp.task('build-pdf', ['build-pm-template'], () => {
return gulp.src('./src/pm-content/*.html')
.pipe(newer('src/pdf/'))
.pipe(exec('prince --verbose --input=html --javascript --style ./src/css/article-styles.css <%= file.path %> '))
.pipe(exec.reporter());
});
```
So we've gone from Markdown to HTML and Markdown to PDF. A next step may be how we can populate Handlebar or Dust templates from our Markdown sources.