Giter Site home page Giter Site logo

html2md's Introduction

HTML 2 Markdown

A simple NodeJS library to convert HTML to Markdown.

How it works

The given HTML string is transformed into a virtual DOM using cheerio and afterwards minified using html-minifier. The resulting DOM-Nodes are then run though (extendable) rules and deconstructed into Markdown text.

Each DOM-Node replaces itself with the transformed output text. The end result is a <body> with only text nodes inside. The resulting innerHTML() of the body is then sanitized (removing more then 2 linkbreaks in a row etc.) and its content given back.

Why yet another lib?

I was trying serveral other libs before and none was a perfect fit. Often they didn't escape correctly, so that a <div># Hallo Welt</div> would result in # Hello World as Markdown, which is not correct. Also, some libs did not work in newer NodeJS versions.

Feature support

Not everything has a coverage yet, but most things work quite well:

[x] Line Breaks
[x] Images
[x] Anchors
[x] Lists (ordered, unordered)
[x] Strong text
[x] Italic text
[x] Headings
[x] Horizontal line
[x] Paragraphs
[x] Inline Code
[x] Code Blocks
[x] Blockquotes
[ ] Tables (Markdown Extra Feature)

Usage

const html2md = require('html2md');

let myMarkdown = html2md('<h1>Hello World</h1>');

console.log(myMarkdown); // # Hello World

Demo

  1. Clone this repository and move into the folder.
  2. Add your own sites to sites.json.
  3. Run node demo.js and watch the outputed files.

html2md's People

Contributors

mofux avatar

Watchers

James Cloos avatar Ryan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.