What is turndown?
Turndown is a library that converts HTML into Markdown. It provides a simple API to transform HTML strings into Markdown format, making it useful for applications that need to convert rich text content into a more readable and lightweight format.
What are turndown's main functionalities?
Basic HTML to Markdown Conversion
This feature allows you to convert basic HTML elements into their Markdown equivalents. In this example, an HTML heading is converted into a Markdown heading.
const TurndownService = require('turndown');
const turndownService = new TurndownService();
const markdown = turndownService.turndown('<h1>Hello World</h1>');
console.log(markdown); // # Hello World
Custom Rules
Turndown allows you to add custom rules for converting specific HTML elements. In this example, a custom rule is added to convert <del>, <s>, and <strike> tags into Markdown strikethrough syntax.
const TurndownService = require('turndown');
const turndownService = new TurndownService();
turndownService.addRule('strikethrough', {
filter: ['del', 's', 'strike'],
replacement: function (content) {
return '~~' + content + '~~';
}
});
const markdown = turndownService.turndown('<del>Deleted text</del>');
console.log(markdown); // ~~Deleted text~~
Handling Complex HTML Structures
Turndown can handle complex HTML structures and convert them into Markdown. This example demonstrates converting a nested HTML structure with headings, paragraphs, and inline formatting.
const TurndownService = require('turndown');
const turndownService = new TurndownService();
const html = '<div><h1>Title</h1><p>This is a <strong>paragraph</strong> with <em>emphasis</em>.</p></div>';
const markdown = turndownService.turndown(html);
console.log(markdown); // # Title\n\nThis is a **paragraph** with *emphasis*.
Other packages similar to turndown
showdown
Showdown is a bidirectional converter that can convert Markdown to HTML and vice versa. It offers a wide range of options and extensions for customization. Compared to Turndown, Showdown is more versatile as it supports both directions of conversion.
markdown-it
Markdown-it is a fast and flexible Markdown parser that can be extended with plugins. While it primarily focuses on converting Markdown to HTML, it can be extended to support HTML to Markdown conversion with additional plugins. It is known for its speed and extensibility.
remark
Remark is a Markdown processor powered by plugins. It can parse, transform, and compile Markdown. With the right plugins, it can also handle HTML to Markdown conversion. Remark is highly modular and customizable, making it suitable for complex transformations.
Turndown
Convert HTML into Markdown.
Note this is pre-release software. API changes are likely.
Usage
var turndownService = new TurndownService()
var markdown = turndownService.turndown('<h1>Hello world!</h1>')
Options
Options can be passed in to the constructor on instantiation.
Option | Valid values | Default |
---|
headingStyle | setext or atx | setext |
hr | Any Thematic break | * * * |
bulletListMarker | - , + , or * | * |
codeBlockStyle | indented or fenced | indented |
fence | ``` or ~~~ | ``` |
emDelimiter | _ or * | _ |
strongDelimiter | ** or __ | ** |
linkStyle | inlined or referenced | inlined |
linkReferenceStyle | full , collapsed , or shortcut | full |
License
turndown is copyright © 2017+ Dom Christie and released under the MIT license.