@@ -121,62 +121,63 @@ Parser('<p id="replace">text</p>', {
121
121
});
122
122
```
123
123
124
- Advanced example (the replaced element's children are kept ):
124
+ Advanced example (keep the replaced children):
125
125
126
126
``` js
127
- var Parser = require (' html-react-parser' );
128
- var React = require (' react' );
129
-
130
- // used for recursively parsing DOM created from the HTML
131
- var domToReact = require (' html-react-parser/lib/dom-to-react' );
132
-
133
- var html = (
134
- ' <div>' +
135
- ' <p id="main">' +
136
- ' <span class="prettify">' +
137
- ' keep me and make me pretty!' +
138
- ' </span>' +
139
- ' </p>' +
140
- ' </div>'
141
- );
127
+ // with ES6 and JSX
128
+
129
+ // converts dom object to React Elements
130
+ import domToReact from ' html-react-parser/lib/dom-to-react' ;
142
131
143
- var parserConfig = {
144
- replace : function (domNode ) {
145
- var parsedChildren;
146
- if (domNode .attribs ) {
147
- if (domNode .attribs .id === ' main' ) {
148
- // continue parsing domNode's children with same config
149
- parsedChildren = domToReact (domNode .children , parserConfig);
150
- return React .createElement (' span' , {
151
- style: { fontSize: ' 42px' }
152
- }, parsedChildren);
153
- // equivalent jsx syntax:
154
- // return <span style={{ fontSize: '42px' }}>{parsedChildren}</span>;
155
-
156
- } else if (domNode .attribs .class === ' prettify' ) {
157
- // continue parsing domNode's children with same config
158
- parsedChildren = domToReact (domNode .children , parserConfig);
159
- return React .createElement (' span' , {
160
- style: { color: ' hotpink' }
161
- }, parsedChildren);
162
- // equivalent jsx syntax:
163
- // return <span style={{ color: 'hotpink' }}>{parsedChildren}</span>;
164
- }
132
+ const html = `
133
+ <div>
134
+ <p id="main">
135
+ <span class="prettify">
136
+ keep me and make me pretty!
137
+ </span>
138
+ </p>
139
+ </div>
140
+ ` ;
141
+
142
+ // parser config
143
+ const options = {
144
+ replace : (domNode ) => {
145
+ // do not replace if element has no attributes
146
+ if (! domNode .attribs ) return ;
147
+
148
+ if (domNode .attribs .id === ' main' ) {
149
+ return (
150
+ < span style= {{ fontSize: ' 42px' }}>
151
+ {domToReact (domNode .children , options)}
152
+ < / span>
153
+ );
154
+
155
+ } else if (domNode .attribs .class === ' prettify' ) {
156
+ return (
157
+ < span style= {{ color: ' hotpink' }}>
158
+ {domToReact (domNode .children , options)}
159
+ < / span>
160
+ );
165
161
}
166
162
}
167
163
};
168
164
169
- require ( ' react-dom ' ). render (
170
- Parser (html, parserConfig ),
165
+ render (
166
+ Parser (html, options ),
171
167
document .getElementById (' root' )
172
168
);
173
- // <div>
174
- // <span style="font-size: 42px;">
175
- // <span class="prettify" style="color: hotpink;">
176
- // keep me and make me pretty!
177
- // </span>
178
- // </span>
179
- // </div>
169
+ ```
170
+
171
+ You will get the following:
172
+
173
+ ``` html
174
+ <div >
175
+ <span style =" font-size : 42px ;" >
176
+ <span class =" prettify" style =" color : hotpink ;" >
177
+ keep me and make me pretty!
178
+ </span >
179
+ </span >
180
+ </div >
180
181
```
181
182
182
183
## Testing
0 commit comments