Press "Enter" to skip to content

Convert HTML into Markdown with JavaScript | Turndown

Turndown is a customizable, standalone JavaScript-based HTML to Markdown converter for both node.js and the browser. The plugin API provides a convenient way for developers to apply with multiple extensions.

Rule Precedence:

  • Blank rule
  • Added rules (optional)
  • commonmark rules
  • Keep rules
  • Remove rules
  • Default rule

How to make use of it:

Install the Turndown through NPM:

# NPM
$ npm install turndown --save

Import the Turndown into your module.

// ES 6
import TurndownService from 'turndown';

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

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

<script src="https://unpkg.com/turndown/dist/turndown.js"></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 choices.

new TurndownService({
    rules: COMMONMARK_RULES,
    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 customized rule to the converter.

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

You’re capable of specifying the html parts which must be saved whereas changing.

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

HTML to Markdown Converter With Pure JavaScript, Turndown Plugin/Github


See Demo And Download

Official Website(domchristie): Click Here

This superior jQuery/javascript plugin is developed by domchristie. For extra Advanced Usages, please go to the official website.

Be First to Comment

    Leave a Reply

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