You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+69-45Lines changed: 69 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -15,15 +15,17 @@ HTML to React parser that works on both the server (Node.js) and the client (bro
15
15
HTMLReactParser(string[, options])
16
16
```
17
17
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):
21
19
22
20
```js
23
21
constparse=require('html-react-parser');
24
22
parse('<div>text</div>'); // equivalent to `React.createElement('div', {}, 'text')`
25
23
```
26
24
25
+
To replace an element with a custom element, check out the [replace option](#replacedomnode).
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:
110
112
111
113
```jsx
112
114
<ul>
@@ -135,13 +137,13 @@ parse(
135
137
136
138
#### replace(domNode)
137
139
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.
139
141
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:
141
143
142
144
```js
143
145
parse('<br>', {
144
-
replace:function (domNode) {
146
+
replace:domNode=> {
145
147
console.dir(domNode, { depth:null });
146
148
}
147
149
});
@@ -150,32 +152,33 @@ parse('<br>', {
150
152
Console output:
151
153
152
154
```js
153
-
{ type:'tag',
154
-
name:'br',
155
-
attribs: {},
156
-
children: [],
157
-
next:null,
155
+
Element {
156
+
parent:null,
158
157
prev:null,
159
-
parent:null }
158
+
next:null,
159
+
startIndex:null,
160
+
endIndex:null,
161
+
children: [],
162
+
name:'br',
163
+
attribs: {}
164
+
}
160
165
```
161
166
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:
163
168
164
-
```js
169
+
```jsx
165
170
parse('<p id="replace">text</p>', {
166
171
replace:domNode=> {
167
172
if (domNode.attribs&&domNode.attribs.id==='replace') {
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**.
249
273
250
-
Here's an example showing how to use Preact:
274
+
To use Preact:
251
275
252
276
```js
253
277
parse('<br>', {
254
278
library:require('preact')
255
279
});
256
280
```
257
281
258
-
Or, using a custom library:
282
+
Or a custom library:
259
283
260
284
```js
261
285
parse('<br>', {
@@ -275,7 +299,7 @@ parse('<br>', {
275
299
276
300
### htmlparser2
277
301
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:
279
303
280
304
```js
281
305
{
@@ -284,9 +308,9 @@ The default options passed to [htmlparser2](https://github.com/fb55/htmlparser2/
284
308
}
285
309
```
286
310
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.
288
312
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):
290
314
291
315
```js
292
316
parse('<p /><p />', {
@@ -297,7 +321,7 @@ parse('<p /><p />', {
297
321
});
298
322
```
299
323
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.
301
325
302
326
### trim
303
327
@@ -307,19 +331,19 @@ Normally, whitespace is preserved:
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.
320
344
```
321
345
322
-
However, this option may strip out intentional whitespace:
346
+
However, intentional whitespace may be stripped out:
0 commit comments