Skip to content

Commit f67cc8d

Browse files
docs(readme): fix and update README.md
1 parent e40fa19 commit f67cc8d

File tree

1 file changed

+69
-45
lines changed

1 file changed

+69
-45
lines changed

README.md

Lines changed: 69 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ HTML to React parser that works on both the server (Node.js) and the client (bro
1515
HTMLReactParser(string[, options])
1616
```
1717

18-
It converts an HTML string to one or more [React elements](https://reactjs.org/docs/react-api.html#creating-react-elements). There's also an option to [replace an element](#replacedomnode) with your own.
19-
20-
Example:
18+
The parser converts an HTML string to one or more [React elements](https://reactjs.org/docs/react-api.html#creating-react-elements):
2119

2220
```js
2321
const parse = require('html-react-parser');
2422
parse('<div>text</div>'); // equivalent to `React.createElement('div', {}, 'text')`
2523
```
2624

25+
To replace an element with a custom element, check out the [replace option](#replacedomnode).
26+
27+
Demos:
28+
2729
[CodeSandbox](https://codesandbox.io/s/940pov1l4w) | [Repl.it](https://repl.it/@remarkablemark/html-react-parser) | [JSFiddle](https://jsfiddle.net/remarkablemark/7v86d800/) | [Examples](https://github.com/remarkablemark/html-react-parser/tree/master/examples)
2830

2931
<details>
@@ -75,7 +77,7 @@ $ yarn add html-react-parser
7577

7678
```html
7779
<!-- HTMLReactParser depends on React -->
78-
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
80+
<script src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
7981
<script src="https://unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script>
8082
<script>
8183
window.HTMLReactParser(/* string */);
@@ -106,7 +108,7 @@ Parse multiple elements:
106108
parse('<li>Item 1</li><li>Item 2</li>');
107109
```
108110

109-
Since adjacent elements are parsed as an array, make sure to render them under a parent node:
111+
Make sure to render parsed adjacent elements under a parent element:
110112

111113
```jsx
112114
<ul>
@@ -135,13 +137,13 @@ parse(
135137

136138
#### replace(domNode)
137139

138-
The `replace` callback allows you to swap an element with another React element.
140+
The `replace` option allows you to replace an element with another React element.
139141

140-
The first argument is an object with the same output as [htmlparser2](https://github.com/fb55/htmlparser2/tree/v3.10.1)'s [domhandler](https://github.com/fb55/domhandler#example):
142+
The `replace` callback's 1st argument is a [domhandler](https://github.com/fb55/domhandler#example) node:
141143

142144
```js
143145
parse('<br>', {
144-
replace: function (domNode) {
146+
replace: domNode => {
145147
console.dir(domNode, { depth: null });
146148
}
147149
});
@@ -150,32 +152,33 @@ parse('<br>', {
150152
Console output:
151153

152154
```js
153-
{ type: 'tag',
154-
name: 'br',
155-
attribs: {},
156-
children: [],
157-
next: null,
155+
Element {
156+
parent: null,
158157
prev: null,
159-
parent: null }
158+
next: null,
159+
startIndex: null,
160+
endIndex: null,
161+
children: [],
162+
name: 'br',
163+
attribs: {}
164+
}
160165
```
161166

162-
The element is replaced only if a _valid_ React element is returned:
167+
The element is replaced if a **valid** React element is returned:
163168

164-
```js
169+
```jsx
165170
parse('<p id="replace">text</p>', {
166171
replace: domNode => {
167172
if (domNode.attribs && domNode.attribs.id === 'replace') {
168-
return React.createElement('span', {}, 'replaced');
173+
return <span>replaced</span>;
169174
}
170175
}
171176
});
172177
```
173178

174-
Here's an [example](https://repl.it/@remarkablemark/html-react-parser-replace-example) that modifies an element but keeps the children:
179+
The following [example](https://repl.it/@remarkablemark/html-react-parser-replace-example) modifies the element along with its children:
175180

176181
```jsx
177-
import React from 'react';
178-
import { renderToStaticMarkup } from 'react-dom/server';
179182
import parse, { domToReact } from 'html-react-parser';
180183

181184
const html = `
@@ -188,7 +191,9 @@ const html = `
188191

189192
const options = {
190193
replace: ({ attribs, children }) => {
191-
if (!attribs) return;
194+
if (!attribs) {
195+
return;
196+
}
192197

193198
if (attribs.id === 'main') {
194199
return <h1 style={{ fontSize: 42 }}>{domToReact(children, options)}</h1>;
@@ -204,58 +209,77 @@ const options = {
204209
}
205210
};
206211

207-
console.log(renderToStaticMarkup(parse(html, options)));
212+
parse(html, options);
213+
```
214+
215+
HTML output:
216+
217+
<!-- prettier-ignore-start -->
218+
219+
```html
220+
<h1 style="font-size:42px">
221+
<span style="color:hotpink">
222+
keep me and make me pretty!
223+
</span>
224+
</h1>
208225
```
209226

210-
Use the exported attributesToProps method to convert DOM attributes to React Props:
227+
<!-- prettier-ignore-end -->
228+
229+
Convert DOM attributes to React props with `attributesToProps`:
211230

212231
```jsx
213-
import React from 'react';
214232
import parse, { attributesToProps } from 'html-react-parser';
215233

216234
const html = `
217-
<hr class="prettify" style="background:#fff;text-align:center" />
235+
<main class="prettify" style="background: #fff; text-align: center;" />
218236
`;
219237

220238
const options = {
221-
replace: node => {
222-
if (node.attribs && node.name === 'hr') {
223-
const props = attributesToProps(node.attribs);
224-
return <hr {...props} />;
239+
replace: domNode => {
240+
if (domNode.attribs && domNode.name === 'main') {
241+
const props = attributesToProps(domNode.attribs);
242+
return <div {...props} />;
225243
}
226244
}
227245
};
246+
247+
parse(html, options);
228248
```
229249

230-
Output:
250+
HTML output:
231251

232252
```html
233-
<h1 style="font-size:42px">
234-
<span style="color:hotpink"> keep me and make me pretty! </span>
235-
</h1>
253+
<div class="prettify" style="background:#fff;text-align:center"></div>
236254
```
237255

238-
Here's an [example](https://repl.it/@remarkablemark/html-react-parser-56) that excludes an element:
256+
[Exclude](https://repl.it/@remarkablemark/html-react-parser-56) an element from rendering by replacing it with `<React.Fragment>`:
239257

240258
```jsx
241259
parse('<p><br id="remove"></p>', {
242-
replace: ({ attribs }) => attribs && attribs.id === 'remove' && <Fragment />
260+
replace: ({ attribs }) => attribs && attribs.id === 'remove' && <></>
243261
});
244262
```
245263

264+
HTML output:
265+
266+
```html
267+
<p></p>
268+
```
269+
246270
### library
247271

248-
The `library` option allows you to specify which component library is used to create elements. React is used by default if this option is not specified.
272+
This option specifies the library that creates elements. The default library is **React**.
249273

250-
Here's an example showing how to use Preact:
274+
To use Preact:
251275

252276
```js
253277
parse('<br>', {
254278
library: require('preact')
255279
});
256280
```
257281

258-
Or, using a custom library:
282+
Or a custom library:
259283

260284
```js
261285
parse('<br>', {
@@ -275,7 +299,7 @@ parse('<br>', {
275299

276300
### htmlparser2
277301

278-
The default options passed to [htmlparser2](https://github.com/fb55/htmlparser2/tree/v3.10.1) are:
302+
The default [htmlparser2 options](https://github.com/fb55/htmlparser2/wiki/Parser-options) are:
279303

280304
```js
281305
{
@@ -284,9 +308,9 @@ The default options passed to [htmlparser2](https://github.com/fb55/htmlparser2/
284308
}
285309
```
286310

287-
Since [v0.12.0](https://github.com/remarkablemark/html-react-parser/tree/v0.12.0), you can override the default options by passing [htmlparser2 options](https://github.com/fb55/htmlparser2/wiki/Parser-options).
311+
Since [v0.12.0](https://github.com/remarkablemark/html-react-parser/tree/v0.12.0), the htmlparser2 options can be overridden.
288312

289-
Here's an example in which [`decodeEntities`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-decodeentities) and [`xmlMode`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-xmlmode) are enabled:
313+
The following example enables [`decodeEntities`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-decodeentities) and [`xmlMode`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-xmlmode):
290314

291315
```js
292316
parse('<p /><p />', {
@@ -297,7 +321,7 @@ parse('<p /><p />', {
297321
});
298322
```
299323

300-
> **Warning**: `htmlparser2` is only applicable on the _server-side_ (Node.js) and not applicable on the _client-side_ (browser). By overriding `htmlparser2` options, there's a chance that universal rendering breaks. Do this at your own _risk_.
324+
> **WARNING**: `htmlparser2` options do not apply on the _client-side_ (browser). The options only apply on the _server-side_ (Node.js). By overriding `htmlparser2` options, universal rendering can break. Do this at your own risk.
301325
302326
### trim
303327

@@ -307,19 +331,19 @@ Normally, whitespace is preserved:
307331
parse('<br>\n'); // [React.createElement('br'), '\n']
308332
```
309333

310-
By enabling the `trim` option, whitespace text nodes will be skipped:
334+
Enable the `trim` option to remove whitespace:
311335

312336
```js
313337
parse('<br>\n', { trim: true }); // React.createElement('br')
314338
```
315339

316-
This addresses the warning:
340+
This fixes the warning:
317341

318342
```
319343
Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of <table>. Make sure you don't have any extra whitespace between tags on each line of your source code.
320344
```
321345

322-
However, this option may strip out intentional whitespace:
346+
However, intentional whitespace may be stripped out:
323347

324348
```js
325349
parse('<p> </p>', { trim: true }); // React.createElement('p')

0 commit comments

Comments
 (0)