HTML to Markdown Converter With Pure JavaScript – Turndown

Category: Javascript , Recommended , Text | January 31, 2019
Author: domchristie
Views Total: 1,973
Official Page: Go to website
Last Update: January 31, 2019
License: MIT


HTML to Markdown Converter With Pure JavaScript – Turndown


Turndown is a customizable, standalone JavaScript-based  HTML to Markdown converter for both node.js and browser.

How to use it:

Install the Turndown via NPM:

$ npm install turndown --save

Import the Turndown into your module.

// ES 6
import TurndownService from 'turndown';

// CommonJS:
const TurndownService = require('turndown');

For browser, include the JavaScript file ‘turndown.js’ on the webpage.

<script src=""></script>

Create a new Turndown instance.

const turndownService = new TurndownService()

Convert any HTML markup to Markdown.

const markdown = turndownService.turndown('<h1>Hello world!</h1>')

All default options.

new TurndownService({
    headingStyle: 'setext',
    hr: '* * *',
    bulletListMarker: '*',
    codeBlockStyle: 'indented',
    fence: '```',
    emDelimiter: '_',
    strongDelimiter: '**',
    linkStyle: 'inlined',
    linkReferenceStyle: 'full',
    br: '  ',
    blankReplacement: function (content, node) {
      return node.isBlock ? '\n\n' : ''
    keepReplacement: function (content, node) {
      return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML
    defaultReplacement: function (content, node) {
      return node.isBlock ? '\n\n' + content + '\n\n' : content

Add a custom rule to the converter.

turndownService.addRule('rule-name', {
  // your own rule here

You’re able to specify the html elements which should be kept while converting.

turndownService.keep(['del', 'ins'])


v5.0.3 (01/31/2019)

  • Consistently handle inline elements with spaces

v5.0.1 (10/31/2018)

  • Do not escape hyphens unnecessarily

You Might Be Interested In:

Leave a Reply