@@ -20,7 +20,7 @@ The parser converts an HTML string to [React element(s)](https://facebook.github
2020``` js
2121var Parser = require (' html-react-parser' );
2222var reactElement = (
23- Parser (' <p>Hello, world!</p>' ) // equivalent to `React.createElement('p', {}, 'Hello, world!')`
23+ Parser (' <p>Hello, world!</p>' ) // same as `React.createElement('p', {}, 'Hello, world!')`
2424);
2525require (' react-dom' ).render (reactElement, document .getElementById (' root' ));
2626```
@@ -72,8 +72,7 @@ ReactDOM.render(
7272
7373The ` replace ` method allows you to swap an element with your own React element.
7474
75- The method has 1 parameter:
76- 1 . ` domNode ` : An object which shares the same schema as the output from [ htmlparser2.parseDOM] ( https://github.com/fb55/domhandler#example ) .
75+ The first argument is ` domNode ` , which is an object which shares the same schema as the output from [ htmlparser2.parseDOM] ( https://github.com/fb55/domhandler#example ) .
7776
7877``` js
7978Parser (' <p id="replace">text</p>' , {
@@ -94,52 +93,78 @@ Parser('<p id="replace">text</p>', {
9493});
9594```
9695
97- Working example:
96+ Simple example:
9897
9998``` js
10099var Parser = require (' html-react-parser' );
101100var React = require (' react' );
102101
103- var html = ' <div><p id="main">replace me</p></div>' ;
102+ var html = (
103+ ' <div>' +
104+ ' <p id="replace">'
105+ ' replace me' +
106+ ' </p>' +
107+ ' </div>'
108+ );
104109
105110var reactElement = Parser (html, {
106111 replace : function (domNode ) {
107- if (domNode .attribs && domNode .attribs .id === ' main ' ) {
112+ if (domNode .attribs && domNode .attribs .id === ' replace ' ) {
108113 return React .createElement (' span' , {
109- style: { fontSize: ' 42px' } },
110- ' replaced!' );
114+ style: { fontSize: ' 42px' }
115+ }, ' replaced!' );
111116 // equivalent jsx syntax:
112117 // return <span style={{ fontSize: '42px' }}>replaced!</span>;
113118 }
114119 }
115120});
116121
117- require (' react-dom' ).render (reactElement, document .getElementById (' root' ));
118- // <div><span style="font-size: 42px;">replaced!</span></div>
122+ require (' react-dom' ).render (
123+ reactElement,
124+ document .getElementById (' root' )
125+ );
126+ // <div>
127+ // <span style="font-size: 42px;">
128+ // replaced!
129+ // </span>
130+ // </div>
119131```
120132
121- Advanced usage with replace, keeping children (useful if child elements also needs to be replaced or just kept):
133+ Advanced example (the replaced element's children are kept):
122134
123135``` js
124136var Parser = require (' html-react-parser' );
125- var domToReact = require (' html-react-parser/lib/dom-to-react' ); // used for recursively parsing DOM created from the HTML
126137var React = require (' react' );
127138
128- var html = ' <div><p id="main"><span class="prettify">keep me and make me pretty!</span></p></div>' ;
139+ // used for recursively parsing DOM created from the HTML
140+ var domToReact = require (' html-react-parser/lib/dom-to-react' );
141+
142+ var html = (
143+ ' <div>' +
144+ ' <p id="main">' +
145+ ' <span class="prettify">' +
146+ ' keep me and make me pretty!' +
147+ ' </span>' +
148+ ' </p>' +
149+ ' </div>'
150+ );
129151
130152var parserConfig = {
131153 replace : function (domNode ) {
132154 var parsedChildren;
133155 if (domNode .attribs ) {
134156 if (domNode .attribs .id === ' main' ) {
135- parsedChildren = domToReact (domNode .children , parserConfig); // continue parsing domNode's children with same config
157+ // continue parsing domNode's children with same config
158+ parsedChildren = domToReact (domNode .children , parserConfig);
136159 return React .createElement (' span' , {
137160 style: { fontSize: ' 42px' }
138161 }, parsedChildren);
139162 // equivalent jsx syntax:
140163 // return <span style={{ fontSize: '42px' }}>{parsedChildren}</span>;
164+
141165 } else if (domNode .attribs .class === ' prettify' ) {
142- parsedChildren = domToReact (domNode .children , parserConfig); // continue parsing domNode's children with same config
166+ // continue parsing domNode's children with same config
167+ parsedChildren = domToReact (domNode .children , parserConfig);
143168 return React .createElement (' span' , {
144169 style: { color: ' hotpink' }
145170 }, parsedChildren);
@@ -150,21 +175,31 @@ var parserConfig = {
150175 }
151176};
152177
153- var reactElement = Parser (html, parserConfig);
154-
155- require (' react-dom' ).render (reactElement, document .getElementById (' root' ));
156- // <div><span style="font-size: 42px;"><span class="prettify" style="color: hotpink;">keep me and make me pretty!</span></span></div>
178+ require (' react-dom' ).render (
179+ Parser (html, parserConfig),
180+ document .getElementById (' root' )
181+ );
182+ // <div>
183+ // <span style="font-size: 42px;">
184+ // <span class="prettify" style="color: hotpink;">
185+ // keep me and make me pretty!
186+ // </span>
187+ // </span>
188+ // </div>
157189```
158190
159191## Testing
160192
161193``` sh
162194$ npm test
195+ $ npm run lint
163196```
164197
165198## Special Thanks
166199
167- To [ benox3] ( https://github.com/benox3 ) and [ tdlm] ( https://github.com/tdlm ) for their feedback and review.
200+ - To [ htmlparser2] ( https://github.com/fb55/htmlparser2 ) .
201+ - To all the [ contributors] ( https://github.com/remarkablemark/html-react-parser/graphs/contributors ) .
202+ - To [ benox3] ( https://github.com/benox3 ) and [ tdlm] ( https://github.com/tdlm ) for their feedback and review.
168203
169204## License
170205
0 commit comments