This guide will walk you through your first HTML to Markdown conversion using Turndown. In just a few minutes, you’ll be converting HTML to clean, readable Markdown.
Turndown handles complex HTML structures with ease:
var TurndownService = require('turndown')var turndownService = new TurndownService()var html = ` <article> <h1>Getting Started with Turndown</h1> <p>Turndown is a <strong>powerful</strong> HTML to Markdown converter.</p> <h2>Features</h2> <ul> <li>CommonMark support</li> <li>Highly configurable</li> <li>Plugin system</li> </ul> <blockquote> <p>It just works!</p> </blockquote> </article>`var markdown = turndownService.turndown(html)console.log(markdown)
Output:
Getting Started with Turndown==============================Turndown is a **powerful** HTML to Markdown converter.Features--------* CommonMark support* Highly configurable* Plugin system> It just works!
In a browser environment, you can convert DOM elements directly:
var turndownService = new TurndownService()// Convert a specific elementvar contentDiv = document.getElementById('content')var markdown = turndownService.turndown(contentDiv)// Convert the entire documentvar fullMarkdown = turndownService.turndown(document)// Convert a document fragmentvar fragment = document.createDocumentFragment()// ... add nodes to fragmentvar fragmentMarkdown = turndownService.turndown(fragment)
Customize how Turndown converts HTML by passing options to the constructor:
var turndownService = new TurndownService({ headingStyle: 'atx', // Use # for headings instead of underlines codeBlockStyle: 'fenced', // Use ``` for code blocks bulletListMarker: '-', // Use - for bullet lists emDelimiter: '*', // Use * for emphasis strongDelimiter: '**' // Use ** for strong})var html = '<h1>Title</h1><p><em>italic</em> and <strong>bold</strong></p>'var markdown = turndownService.turndown(html)console.log(markdown)// Output:// # Title//// *italic* and **bold**
// Setext style (default): uses underlines for h1 and h2var service1 = new TurndownService({ headingStyle: 'setext' })service1.turndown('<h1>Title</h1>')// Title// =====// ATX style: uses # for all headingsvar service2 = new TurndownService({ headingStyle: 'atx' })service2.turndown('<h1>Title</h1>')// # Title
// Indented code blocks (default)var service1 = new TurndownService({ codeBlockStyle: 'indented' })service1.turndown('<pre><code>var x = 1;</code></pre>')// var x = 1;// Fenced code blocksvar service2 = new TurndownService({ codeBlockStyle: 'fenced' })service2.turndown('<pre><code>var x = 1;</code></pre>')// ```// var x = 1;// ```
var html = '<p>Use the <code>turndown()</code> method to convert HTML.</p>'var markdown = turndownService.turndown(html)console.log(markdown)// Output: Use the `turndown()` method to convert HTML.
Turndown throws a TypeError if you pass invalid input:
var turndownService = new TurndownService()try { var markdown = turndownService.turndown(null)} catch (error) { console.error(error.message) // Output: null is not a string, or an element/document/fragment node.}
Here’s a complete working example bringing it all together:
var TurndownService = require('turndown')// Create service with custom optionsvar turndownService = new TurndownService({ headingStyle: 'atx', codeBlockStyle: 'fenced', bulletListMarker: '-'})// HTML content to convertvar html = ` <article> <h1>My Blog Post</h1> <p>This is a paragraph with <strong>bold</strong> and <em>italic</em> text.</p> <h2>Code Example</h2> <pre><code class="language-javascript">const greeting = 'Hello, world!';console.log(greeting); </code></pre> <h2>Features</h2> <ul> <li>Easy to use</li> <li>Highly configurable</li> <li>Great output</li> </ul> <p>Read more at <a href="https://example.com">our website</a>.</p> </article>`// Convert to Markdownvar markdown = turndownService.turndown(html)console.log(markdown)