Skip to content

h4rdc0m/react-active-html

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Active HTML for ReactJS

Convert HTML string to React Components

The problem

The most of the CMS provide content as pure html from WYSIWYG editors:

{
    "content": "<a href='/hello'>Hello World</a><img src='image.png' class='main-image' alt='' /><p>Lorem ipsum...</p>"
}

In this case you lose advantage of using React components in the content.

Solution

import activeHtml from 'react-active-html';

class Html extends Component {

    render() {

        const components = {
            // replace <img> tags by custom react component
            img: (attributes) => {
                return (<Image {...attributes} />);
            },
            // replace <a> tags by React Router Link component
            a: (attributes) => {
                return (<Link to={attributes.href} {...attributes} />);
            },
            // add lazy load to all iframes
            iframe: (attributes) => {
                return (
                    <LazyLoad>
                        <iframe {...attributes} />
                    </LazyLoad>
                );
            }
        };

        // convert string property "content" to React components
        let nodes = activeHtml(this.props.content, components);

        return (<div className="html">{nodes}</div>);
    }
}

Installation

Frontend

npm install react-active-html --save-dev

Backend (NodeJS)

npm install react-active-html xmldom --save
global.DOMParser = require('xmldom').DOMParser;

About

Convert HTML string to React Components

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%