Skip to main content
Dublin Library

The Publishing Project

Getting Markdown plugins to work with Gulp


I finally solved two big misteries: Why does Gulp give you the ability to write your own code and how to integrate plugins into a Gulp-based Markdown project.

Why write your own code #

There are times when there is no plugin available for a certain task or the plugin doesn't as expected.

In my gulp-starter I use Markdown-It to convert Markdown to HTML.

I would love to use plugins to provide equivalent functionality to what I get using Markdown in WordPress via the Jetpack Plugin in the classic editor.

Using a Gulp plugin (gulp-markdown-it) is a good way to get started but I wasn't successful in adding Markdown-It plugins to work with the Gulp plugin.

A solution I found was to write my own code and bypass the Gulp Markdown-It plugin altogether while still using Markdown-It as the parser.

The next sections explain how I did it.

Integrating plugins to Markdown workflows #

The first step is to require the plugins we want to use.

I'm using CommonJS modules rather than ESM modules. Even though all currently supported versions of Node support ESM imports, not all modules I'm using do and I don't want to go through the hassle of converting them.

I'm also using the Markdown-It tool directly rather than using a plugin. I will explain why in the next section.

We require all the plugins we want to use before we use them.

// require gulp first
const gulp = require('gulp');

// require markdown-it
const markdownIt = require('markdown-it');

// require markdown-it plugins
const abbr = require("markdown-it-abbr");
const alerts = require("markdown-it-alerts");
const anc = require("markdown-it-anchor");
const attrs = require("markdown-it-attrs");
const embed = require("markdown-it-block-embed");
const fn = require("markdown-it-footnote");
const figs = require("markdown-it-implicit-figures");
const kbd = require("markdown-it-kbd");
const prism = require("markdown-it-prism");
const toc = require("markdown-it-table-of-contents");
const list = require("markdown-it-task-lists");

First, we configure Markdown-It parser options.

I chose to use the Commonmark preset. Commonmark solves a lot of the problems that Markdown has experienced, pretty much from the beginning.

I also chose to enable the following options by setting them to true:

Enable HTML tags in the source
Uses / to close single tags (
). : This is only for full CommonMark compatibility.
Autoconvert URL-like text to links
Enable some language-neutral replacement + quotes beautification

For the full list of replacements, see

// Markdown-It Options
const options = {
  preset: 'commonmark',
  html: true,
  xhtmlOut: true,
  linkify: true,
  typographer: true,

The next step is to initialize Markdown-It and then declare the plugins we want to use via the use method.

The use() method also allows us to pass options to the plugins as the second parameter of the use() declaration.

const md = new markdownIt();

The first custom function will actually convert the file to HTML and replace the extension with .html.

function markdownToHtml(file) {
  const result = md.render(file.contents.toString());
  file.contents = new Buffer(result);
  file.path = replaceExt(file.path, '.html');

Next is our Markdown converter task. We use the gulp-tap plugin to run the external function inside the task. Otherwise, it looks like a regular task and works like one too :).

function markdown() {
  return gulp

Customizing Markdown-It #

Right now we have all our functionality running, from definition lists to table of contents. But there are parts that can't be done with plugins and are not part of the Markdown-It API.

We'll explore how to do this in a future post.

Edit on Github