How to convert HTML into Markdown with JavaScript

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'


Last updated August 3, 2021