Ben Borgers

How to convert HTML into Markdown with JavaScript

August 3, 2021

There’s plenty of libraries out there for turning markdown into HTML, but turning HTML into markdown is trickier.

Luckily I found Turndown, which is a fantastic JavaScript library for converting HTML into markdown.

To use it, first install Turndown:

npm install turndown

And then use it:

const turndown = require('turndown')

const turndownService = new turndown()
const markdown = turndownService.turndown('<p>Hello, <strong>world</strong></p>')

Calling the .turndown() method and passing it an HTML string will return that HTML converted into markdown syntax.

There’s a bunch of documented configuration options for Turndown. When I first used it, I found that the default markdown formatting settings were a bit strange. For example, headings are outputted in the setext style (=== on the line under the heading) instead of the atx style (preceding the heading with #).

I used these configuration options to make Turndown’s outputted markdown more what I expected:

const turndownService = new turndown({
    headingStyle: 'atx',
    hr: '---',
    bulletListMarker: '-',
    codeBlockStyle: 'fenced'

Subscribe to my newsletter!

A weekly round-up of new blog posts and updates to projects I’m working on.