// ES module importimport TurndownService from 'turndown'const turndownService = new TurndownService()const markdown = turndownService.turndown('<p>Hello world</p>')
The ES module entry point is lib/turndown.es.js, specified via the module field in package.json.
Turndown includes @mixmark-io/domino as a dependency for server-side DOM manipulation. This allows you to parse HTML strings and work with DOM nodes in Node.js:
var TurndownService = require('turndown')var turndownService = new TurndownService()// Parse HTML stringvar html = ` <article> <h1>Article Title</h1> <p>This is the <strong>article content</strong>.</p> <ul> <li>Point one</li> <li>Point two</li> </ul> </article>`var markdown = turndownService.turndown(html)console.log(markdown)
Output:
Article Title=============This is the **article content**.* Point one* Point two
The domino dependency is automatically excluded in browser builds via the browser field in package.json.
Turndown validates input and throws a TypeError for invalid input:
var turndownService = new TurndownService()try { // This will throw a TypeError var markdown = turndownService.turndown(null)} catch (error) { console.error(error.message) // "null is not a string, or an element/document/fragment node."}
Always validate input before passing it to Turndown, especially when processing user-generated content.