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 🙏

© 2025 – Pkg Stats / Ryan Hefner

html-2-react

v1.1.4

Published

Converts HTML to React elements.

Downloads

11

Readme

html-2-react

npm version

Converts HTML to React elements.

Designed to work within the browser, but should work anywhere.

Dependencies

  • React (of course)
  • jQuery (for parsing the HTML)
  • Gulp (for development only)
  • React Router (when using the React Router mutator)

Usage

Here's an example React class that takes the htmlcontent property, and demonstrates parsing in context.

var Html2React = require('html-2-react');

var MyComponent = React.createClass({
  render: function() {
    var parsed = new Html2React(this.props.htmlcontent),
        reactElems = [];
    if (parsed.success()) {
      reactElems = parsed.toReact();
    }
    return (
      <div>
        {reactElems}
      </div>
    );
  }
});

Usage within browsers

If you use the distribution version of this library from dist/html-2-react.js, and don't want to have to build your app using Browserify or Webpack or whatever, you can use the window.html2React or html2React function instead of using the require statement above. The equivalent is as follows:

var MyComponent = React.createClass({
  render: function() {
    var parsed = new html2React(this.props.htmlcontent),
        reactElems = [];
    if (parsed.success()) {
      reactElems = parsed.toReact();
    }
    return (
      <div>
        {reactElems}
      </div>
    );
  }
});

Mutating elements before converting to React

Before you convert your markup to React elements, it may be useful to mutate some of the elements along the way. To this end, there's a function exposed on Html2React named mutateEach, which will run a function across every element in the parsed tree.

Below, we have an example that uses the built-in reactRouterLink mutator, which converts <a> tags to React Router Link elements.

var parsed = new html2React(this.props.htmlcontent),
    reactElems = [];
if (parsed.success()) {
  parsed.mutateEach(parsed.mutators.reactRouterLink);
  reactElems = parsed.toReact();
}

Writing your own mutator is pretty easy. You'd define one and run it like so:

function myMutator(elem) {
  if (this.isDomNode(elem)) {
    if (elem.nodeName == 'strong') {
      if (!elem.attributes) {
        elem.attributes = {};
      }
      if (!elem.attributes.className) {
        elem.attributes.className = 'beefy';
      }
      else {
        elem.attributes.className += ' bold';
      }
    }
  }
}

var parsed = new html2React(this.props.htmlcontent),
    reactElems = [];
if (parsed.success()) {
  parsed.mutateEach(myMutator);
  reactElems = parsed.toReact();
}

See how you can call isDomNode to determine if a current element is a valid element, before checking against its nodeName?

Whenever you're using a mutator, and are working with a valid DomNode, you can mutate the following properties:

  • nodeName
  • attributes
  • children

If you just have a string, you're dealing with a text node.

Options

  • useDefaultMutators
    html-2-react comes with a set of default mutators that helps automatically transform elements into a more React-friendly form. If set to true, these mutators are run when the elements are first parsed.
    Default: true

Build instructions

gulp dist

@TODO

  • Filtering of dangerous tags such as <script>
  • Write some tests!
  • Write some examples, especially backend ones!

See also