Convert HTML to Markdown

By  on  

One of my biggest mistakes with this blog was not finding a WordPress plugin that would allow me to write my posts with markdown; to this day I still need to write posts in "Visual" mode and then manually convert the post to HTML for "Text" mode.  One of these days I want to convert existing posts to Markdown and then enable a plugin that will convert Markdown to HTML. This painful process made me ask myself:  is there a way I can use Node.js JavaScript to convert HTML to Markdown?  There is, and it's called Turndown by Dom Christie.

Convert HTML to Markdown with Node.js

Start by installing Turndown:

yarn add turndown

Then use Turndown's simple API to convert HTML to markdown:

var TurndownService = require('turndown');
var turndownService = new TurndownService();

var markdown = turndownService.turndown(`
    

Title

DavidWalsh.Name is awesome!

`); /* Title ===== [DavidWalsh.Name](https://davidwalsh.name) is awesome! */

You can use the interactive Turndown demo to experiment with its capabilities.  Turndown has a number of options and allows you to use filters to keep elements you believe could be at risk for improper conversion.

Most developers look for a Markdown to HTML solution so it's rate to find myself in a position to need to convert HTML to Markdown.  I look forward to migrating my site's content to Markdown so that writing posts is much less stressful in the future!

Recent Features

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

  • By
    Create a NoScript Compatible Select Form Element with an onChange Event

    I wouldn't say that I'm addicted to checking Google Analytics but I do check my statistics often. I guess hoping for a huge burst of traffic from some unknown source. Anyway, I have multiple sites set up within my account. The way to...

  • By
    Xbox Live Gamer API

    My sharpshooter status aside, I've always been surprised upset that Microsoft has never provided an API for the vast amount of information about users, the games they play, and statistics within the games. Namely, I'd like to publicly shame every n00b I've baptized with my...

Discussion

  1. Hi David,

    there’s even more you can do with it!

    We explored it in a hackathon:
    Exporting content from Confluence using its ReST api and feed the markdown to a static site generator (like Netlify or Hapi.js).

    Was quite fun :-)

    Sadly I can’t share details because of work during business hours :-(

    Cheers

    André

  2. Jennifer

    Jetpack includes an option to write posts using Markdown. https://jetpack.com/support/markdown/

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!