Skip to content

Commit b89ca5b

Browse files
Update README advanced example for replace options
Trim the fat and keep the example super concise and readable. Refactor example to ES6 and JSX.
1 parent a8de457 commit b89ca5b

File tree

1 file changed

+48
-47
lines changed

1 file changed

+48
-47
lines changed

README.md

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -121,62 +121,63 @@ Parser('<p id="replace">text</p>', {
121121
});
122122
```
123123

124-
Advanced example (the replaced element's children are kept):
124+
Advanced example (keep the replaced children):
125125

126126
```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';
142131

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+
);
165161
}
166162
}
167163
};
168164

169-
require('react-dom').render(
170-
Parser(html, parserConfig),
165+
render(
166+
Parser(html, options),
171167
document.getElementById('root')
172168
);
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>
180181
```
181182

182183
## Testing

0 commit comments

Comments
 (0)