Templated markdown with handlebars

To used templated markdown files, we need a library for markdown (showdown) and a templating library (handlebars). A good resource for finding well-maintained libraries is https://www.javascripting.com/.

let showdown = require('showdown'),
    Handlebars = require('handlebars');

We’ll need some sample data:

let data = {
  Title: 'A test title',
  Description: 'Testing description'
}

And a template file:

let template = `
## Test
Title:
Slug:
Excerpt:

*[{{Title}}]({{Url}})*:

> {{Description}}

`;

We then apply the handlebars template, and then a markdown template:

let apply = Handlebars.compile(template)
let untemplate = apply(data)
let converter = new showdown.Converter();
let html = converter.makeHtml(untemplate)

This gives a result like so:

Test

Title:
Slug:
Excerpt:

A test title:

Testing description

Leave a Reply

Your email address will not be published. Required fields are marked *