HTML to Markdown Converter With Pure JavaScript – Turndown

Category: Javascript , Recommended , Text | March 22, 2023
Author:domchristie
Views Total:2,683 views
Official Page:Go to website
Last Update:March 22, 2023
License:MIT

Preview:

HTML to Markdown Converter With Pure JavaScript – Turndown

Description:

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

How to use it:

1. Install the Turndown with NPM:

# NPM
$ npm install turndown --save

2. Import the Turndown into your module.

// ES 6
import TurndownService from 'turndown';
// CommonJS:
const TurndownService = require('turndown');

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

<script src="https://unpkg.com/turndown/dist/turndown.js"></script>

4. Create a new Turndown instance.

const turndownService = new TurndownService()

5. Convert any HTML markup to Markdown.

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

6. Available options to customize the markdown editor.

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
    }
})

7. Add a custom rule to the converter.

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

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

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

Changelog:

v7.1.2 (03/22/2023)

  • Bugfix

v7.1.1 (06/30/2021)

  • Update

v6.0.0 (03/12/2019)

  • Consistently handle inline elements with spaces
  • Fixed multiple ticks/tildes inside code blocks

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