@@ -20,7 +20,7 @@ The parser converts an HTML string to [React element(s)](https://facebook.github
20
20
``` js
21
21
var Parser = require (' html-react-parser' );
22
22
var 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!')`
24
24
);
25
25
require (' react-dom' ).render (reactElement, document .getElementById (' root' ));
26
26
```
@@ -72,8 +72,7 @@ ReactDOM.render(
72
72
73
73
The ` replace ` method allows you to swap an element with your own React element.
74
74
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 ) .
77
76
78
77
``` js
79
78
Parser (' <p id="replace">text</p>' , {
@@ -94,52 +93,78 @@ Parser('<p id="replace">text</p>', {
94
93
});
95
94
```
96
95
97
- Working example:
96
+ Simple example:
98
97
99
98
``` js
100
99
var Parser = require (' html-react-parser' );
101
100
var React = require (' react' );
102
101
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
+ );
104
109
105
110
var reactElement = Parser (html, {
106
111
replace : function (domNode ) {
107
- if (domNode .attribs && domNode .attribs .id === ' main ' ) {
112
+ if (domNode .attribs && domNode .attribs .id === ' replace ' ) {
108
113
return React .createElement (' span' , {
109
- style: { fontSize: ' 42px' } },
110
- ' replaced!' );
114
+ style: { fontSize: ' 42px' }
115
+ }, ' replaced!' );
111
116
// equivalent jsx syntax:
112
117
// return <span style={{ fontSize: '42px' }}>replaced!</span>;
113
118
}
114
119
}
115
120
});
116
121
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>
119
131
```
120
132
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):
122
134
123
135
``` js
124
136
var 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
126
137
var React = require (' react' );
127
138
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
+ );
129
151
130
152
var parserConfig = {
131
153
replace : function (domNode ) {
132
154
var parsedChildren;
133
155
if (domNode .attribs ) {
134
156
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);
136
159
return React .createElement (' span' , {
137
160
style: { fontSize: ' 42px' }
138
161
}, parsedChildren);
139
162
// equivalent jsx syntax:
140
163
// return <span style={{ fontSize: '42px' }}>{parsedChildren}</span>;
164
+
141
165
} 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);
143
168
return React .createElement (' span' , {
144
169
style: { color: ' hotpink' }
145
170
}, parsedChildren);
@@ -150,21 +175,31 @@ var parserConfig = {
150
175
}
151
176
};
152
177
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>
157
189
```
158
190
159
191
## Testing
160
192
161
193
``` sh
162
194
$ npm test
195
+ $ npm run lint
163
196
```
164
197
165
198
## Special Thanks
166
199
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.
168
203
169
204
## License
170
205
0 commit comments