npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

to-pure-markdown

v3.0.1

Published

Turn HTML into text formatted with Markdown -- no HTML tags allowed in the output.

Downloads

3

Readme

to-pure-markdown

An HTML to Markdown converter written in JavaScript.

The API is as follows:

toMarkdown(stringOfHTML, options);

Note to-markdown v2 runs on Node 4+. For a version compatible with Node 0.10 - 0.12, please use to-markdown v1.x.

Installation

Browser

Download the compiled script located at dist/to-pure-markdown.js.

<script src="PATH/TO/to-pure-markdown.js"></script>
<script>toMarkdown('<h1>Hello world!</h1>')</script>

Node.js

Install the to-pure-markdown module:

$ npm install to-pure-markdown

Then you can use it like below:

var toMarkdown = require('to-pure-markdown');
toMarkdown('<h1>Hello world!</h1>');

(Note it is no longer necessary to call .toMarkdown on the required module as of v1.)

Options

converters (array)

to-pure-markdown can be extended by passing in an array of converters to the options object:

toMarkdown(stringOfHTML, { converters: [converter1, converter2, …] });

A converter object consists of a filter, and a replacement. This example from the source replaces code elements:

{
  filter: 'code',
  replacement: function(content) {
    return '`' + content + '`';
  }
}

filter (string|array|function)

The filter property determines whether or not an element should be replaced. DOM nodes can be selected simply by filtering by tag name, with strings or with arrays of strings:

  • filter: 'p' will select p elements
  • filter: ['em', 'i'] will select em or i elements

Alternatively, the filter can be a function that returns a boolean depending on whether a given node should be replaced. The function is passed a DOM node as its only argument. For example, the following will match any span element with an italic font style:

filter: function (node) {
  return node.nodeName === 'SPAN' && /italic/i.test(node.style.fontStyle);
}

replacement (function)

The replacement function determines how an element should be converted. It should return the markdown string for a given node. The function is passed the node’s content, as well as the node itself (used in more complex conversions). It is called in the context of toMarkdown, and therefore has access to the methods detailed below.

The following converter replaces heading elements (h1-h6):

{
  filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],

  replacement: function(innerHTML, node) {
    var hLevel = node.tagName.charAt(1);
    var hPrefix = '';
    for(var i = 0; i < hLevel; i++) {
      hPrefix += '#';
    }
    return '\n' + hPrefix + ' ' + innerHTML + '\n\n';
  }
}

gfm (boolean)

to-pure-markdown has beta support for GitHub flavored markdown (GFM). Set the gfm option to true:

toMarkdown('<del>Hello world!</del>', { gfm: true });

Methods

The following methods can be called on the toMarkdown object.

isBlock(node)

Returns true/false depending on whether the element is block level.

isVoid(node)

Returns true/false depending on whether the element is void.

trim(string)

Returns the string with leading and trailing whitespace removed.

outer(node)

Returns the content of the node along with the element itself.

Development

First make sure you have node.js/npm installed, then:

$ npm install --dev

Automatically browserify the module when source files change by running:

$ npm start

Tests

To run the tests in the browser, open test/index.html.

To run in node.js:

$ npm test

Credits

This is a fork of the excellent to-markdown by domchriste.