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
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

Incredible Demos

  • By
    Add Styles to Console Statements

    I was recently checking out Google Plus because they implement some awesome effects.  I opened the console and same the following message: WARNING! Using this console may allow attackers to impersonate you and steal your information using an attack called Self-XSS. Do not enter or paste code that you...

  • By
    9 Incredible CodePen Demos

    CodePen is a treasure trove of incredible demos harnessing the power of client side languages.   The client side is always limited by what browsers provide us but the creativity and cleverness of developers always pushes the boundaries of what we think the front end can do.  Thanks to CSS...

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!