# How to convert HTML to Markdown with JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Mar 28, 2020, updated Oct 1, 2022. Topics: Node.js, JavaScript.
Canonical URL: https://attacomsian.com/blog/convert-html-to-markdown

> A quick guide to learn how to convert HTML to Markdown using  Turndown in a Node.js application as well as in browser.

Markdown is a lightweight markup language with a plain-text-formatting syntax that can be converted to many other output formats. It provides a simple way to add HTML formatting like headings, bold, italic, bulleted lists, images, and so on to the plain text.

In this article, you'll learn **how to convert HTML into Markdown** in a Node.js application and a web browser using the [Turndown](https://github.com/domchristie/turndown) library. Turndown is a customizable HTML to Markdown converter written in vanilla JavaScript.

## Convert markdown to HTML using Node.js

To use Turndown as a Node.js module, type the following command in your terminal to install it from the NPM registry:

```bash
$ npm install turndown --save
```

After the installation is completed, you'll be able to import the Turndown module into your Node.js application like below:

```javascript
// import Turndown module
const TurndownService = require('turndown')
```

Now you can use the Turndown API to easily convert any HTML string to Markdown:

```javascript
// create an instance of the Turndown service
const turndownService = new TurndownService()

// convert HTML to Markdown
const markdown = turndownService.turndown(`
    <h1>JavaScript for Beginners</h1>
    <p>Follow <a href="https://attacomsian.com/blog">Atta</a> to learn <b>JavaScript</b> from scratch!</p>
`)

// output Markdown
console.log(markdown)
```

You should see the following output printed on the console:

```markdown
JavaScript for Beginners
========================

Follow [Atta](https://attacomsian.com/blog) to learn **JavaScript** from scratch!
```

## Convert markdown to HTML using JavaScript

Turndown can also be used in a web browser to convert HTML to Markdown. Just include the following JavaScript file in your HTML document:

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

After including the script, you should be able to convert HTML to Markdown by using the same code we've used in the above example:

```javascript
// create an instance of the Turndown service
const turndownService = new TurndownService()

// convert HTML to Markdown
const markdown = turndownService.turndown(`
    <h1>JavaScript for Beginners</h1>
    <p>Follow <a href="https://attacomsian.com/blog">Atta</a> to learn <b>JavaScript</b> from scratch!</p>
`)

// output Markdown
console.log(markdown)
```

In JavaScript, you can even pass [DOM](https://attacomsian.com/blog/getting-dom-elements-javascript) nodes as input to Turndown API:

```javascript
// convert document <body> to Markdown
const markdown = turndownService.turndown(document.body)

// convert first <p> tag to Markdown
const markdown = turndownService.turndown(document.querySelector('p'))
```

Take a look at the [documentation](https://github.com/domchristie/turndown) or [interactive demo](http://domchristie.github.io/turndown/) to learn more about Turndown capabilities.
