For projects using RequireJS or other UMD-compatible module loaders, Turndown provides a browser-specific UMD build:
require(['turndown'], function(TurndownService) { var turndownService = new TurndownService() var markdown = turndownService.turndown('<p>Convert this HTML</p>')})
The UMD build is located at lib/turndown.browser.umd.js in the npm package. If you need to generate it manually:
npm run build
The browser UMD build excludes the @mixmark-io/domino dependency, which is only needed for Node.js environments.
One of Turndown’s most powerful features in the browser is its ability to accept DOM nodes directly as input:
var turndownService = new TurndownService()// Convert a specific elementvar markdown = turndownService.turndown(document.getElementById('content'))// Convert the entire documentvar markdown = turndownService.turndown(document)// Convert a document fragmentvar fragment = document.createDocumentFragment()// ... add nodes to fragmentvar markdown = turndownService.turndown(fragment)
Turndown accepts element nodes (nodeType 1), document nodes (nodeType 9), and document fragment nodes (nodeType 11).
Turndown validates that the input can be converted before processing:
If you pass invalid input (not a string or valid DOM node), Turndown will throw a TypeError:
input is not a string, or an element/document/fragment node.