html-react-parser 4.2.10 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,8 +21,9 @@ To replace an element with another element, check out the [`replace`](#replace)
21
21
 
22
22
  #### Example
23
23
 
24
- ```js
25
- const parse = require('html-react-parser');
24
+ ```ts
25
+ import parse from 'html-react-parser';
26
+
26
27
  parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
27
28
  ```
28
29
 
@@ -43,6 +44,7 @@ parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
43
44
  - [htmlparser2](#htmlparser2)
44
45
  - [trim](#trim)
45
46
  - [Migration](#migration)
47
+ - [v5](#v5)
46
48
  - [v4](#v4)
47
49
  - [v3](#v3)
48
50
  - [v2](#v2)
@@ -100,31 +102,31 @@ yarn add html-react-parser
100
102
 
101
103
  Import ES module:
102
104
 
103
- ```js
105
+ ```ts
104
106
  import parse from 'html-react-parser';
105
107
  ```
106
108
 
107
109
  Or require CommonJS module:
108
110
 
109
- ```js
110
- const parse = require('html-react-parser');
111
+ ```ts
112
+ const parse = require('html-react-parser').default;
111
113
  ```
112
114
 
113
115
  Parse single element:
114
116
 
115
- ```js
117
+ ```ts
116
118
  parse('<h1>single</h1>');
117
119
  ```
118
120
 
119
121
  Parse multiple elements:
120
122
 
121
- ```js
123
+ ```ts
122
124
  parse('<li>Item 1</li><li>Item 2</li>');
123
125
  ```
124
126
 
125
127
  Make sure to render parsed adjacent elements under a parent element:
126
128
 
127
- ```jsx
129
+ ```tsx
128
130
  <ul>
129
131
  {parse(`
130
132
  <li>Item 1</li>
@@ -135,15 +137,15 @@ Make sure to render parsed adjacent elements under a parent element:
135
137
 
136
138
  Parse nested elements:
137
139
 
138
- ```js
140
+ ```ts
139
141
  parse('<body><p>Lorem ipsum</p></body>');
140
142
  ```
141
143
 
142
144
  Parse element with attributes:
143
145
 
144
- ```js
146
+ ```ts
145
147
  parse(
146
- '<hr id="foo" class="bar" data-attr="baz" custom="qux" style="top:42px;">'
148
+ '<hr id="foo" class="bar" data-attr="baz" custom="qux" style="top:42px;">',
147
149
  );
148
150
  ```
149
151
 
@@ -153,11 +155,11 @@ The `replace` option allows you to replace an element with another element.
153
155
 
154
156
  The `replace` callback's first argument is [domhandler](https://github.com/fb55/domhandler#example)'s node:
155
157
 
156
- ```js
158
+ ```ts
157
159
  parse('<br>', {
158
- replace: (domNode) => {
160
+ replace(domNode) {
159
161
  console.dir(domNode, { depth: null });
160
- }
162
+ },
161
163
  });
162
164
  ```
163
165
 
@@ -165,7 +167,7 @@ parse('<br>', {
165
167
  <summary>Console output</summary>
166
168
  <p>
167
169
 
168
- ```js
170
+ ```ts
169
171
  Element {
170
172
  type: 'tag',
171
173
  parent: null,
@@ -184,29 +186,43 @@ Element {
184
186
 
185
187
  The element is replaced if a **valid** React element is returned:
186
188
 
187
- ```jsx
189
+ ```tsx
188
190
  parse('<p id="replace">text</p>', {
189
- replace: (domNode) => {
191
+ replace(domNode) {
190
192
  if (domNode.attribs && domNode.attribs.id === 'replace') {
191
193
  return <span>replaced</span>;
192
194
  }
193
- }
195
+ },
194
196
  });
195
197
  ```
196
198
 
197
199
  #### replace with TypeScript
198
200
 
199
- For TypeScript projects, you may need to check that `domNode` is an instance of domhandler's `Element`:
201
+ For TypeScript, you'll need to check that `domNode` is an instance of domhandler's `Element`:
200
202
 
201
203
  ```tsx
202
204
  import { HTMLReactParserOptions, Element } from 'html-react-parser';
203
205
 
204
206
  const options: HTMLReactParserOptions = {
205
- replace: (domNode) => {
207
+ replace(domNode) {
206
208
  if (domNode instanceof Element && domNode.attribs) {
207
209
  // ...
208
210
  }
209
- }
211
+ },
212
+ };
213
+ ```
214
+
215
+ Or use a type assertion:
216
+
217
+ ```tsx
218
+ import { HTMLReactParserOptions, Element } from 'html-react-parser';
219
+
220
+ const options: HTMLReactParserOptions = {
221
+ replace(domNode) {
222
+ if ((domNode as Element).attribs) {
223
+ // ...
224
+ }
225
+ },
210
226
  };
211
227
  ```
212
228
 
@@ -216,7 +232,7 @@ If you're having issues take a look at our [Create React App example](./examples
216
232
 
217
233
  Replace the element and its children (see [demo](https://replit.com/@remarkablemark/html-react-parser-replace-example)):
218
234
 
219
- ```jsx
235
+ ```tsx
220
236
  import parse, { domToReact } from 'html-react-parser';
221
237
 
222
238
  const html = `
@@ -228,7 +244,7 @@ const html = `
228
244
  `;
229
245
 
230
246
  const options = {
231
- replace: ({ attribs, children }) => {
247
+ replace({ attribs, children }) {
232
248
  if (!attribs) {
233
249
  return;
234
250
  }
@@ -244,7 +260,7 @@ const options = {
244
260
  </span>
245
261
  );
246
262
  }
247
- }
263
+ },
248
264
  };
249
265
 
250
266
  parse(html, options);
@@ -273,7 +289,7 @@ parse(html, options);
273
289
 
274
290
  Convert DOM attributes to React props with `attributesToProps`:
275
291
 
276
- ```jsx
292
+ ```tsx
277
293
  import parse, { attributesToProps } from 'html-react-parser';
278
294
 
279
295
  const html = `
@@ -281,12 +297,12 @@ const html = `
281
297
  `;
282
298
 
283
299
  const options = {
284
- replace: (domNode) => {
300
+ replace(domNode) {
285
301
  if (domNode.attribs && domNode.name === 'main') {
286
302
  const props = attributesToProps(domNode.attribs);
287
303
  return <div {...props} />;
288
304
  }
289
- }
305
+ },
290
306
  };
291
307
 
292
308
  parse(html, options);
@@ -307,9 +323,9 @@ parse(html, options);
307
323
 
308
324
  [Exclude](https://replit.com/@remarkablemark/html-react-parser-56) an element from rendering by replacing it with `<React.Fragment>`:
309
325
 
310
- ```jsx
326
+ ```tsx
311
327
  parse('<p><br id="remove"></p>', {
312
- replace: ({ attribs }) => attribs && attribs.id === 'remove' && <></>
328
+ replace: ({ attribs }) => attribs?.id === 'remove' && <></>,
313
329
  });
314
330
  ```
315
331
 
@@ -330,12 +346,12 @@ The `transform` option allows you to transform each element individually after i
330
346
 
331
347
  The `transform` callback's first argument is the React element:
332
348
 
333
- ```jsx
349
+ ```tsx
334
350
  parse('<br>', {
335
- transform: (reactNode, domNode, index) => {
351
+ transform(reactNode, domNode, index) {
336
352
  // this will wrap every element in a div
337
353
  return <div>{reactNode}</div>;
338
- }
354
+ },
339
355
  });
340
356
  ```
341
357
 
@@ -345,15 +361,15 @@ The `library` option specifies the UI library. The default library is **React**.
345
361
 
346
362
  To use Preact:
347
363
 
348
- ```js
364
+ ```ts
349
365
  parse('<br>', {
350
- library: require('preact')
366
+ library: require('preact'),
351
367
  });
352
368
  ```
353
369
 
354
370
  Or a custom library:
355
371
 
356
- ```js
372
+ ```ts
357
373
  parse('<br>', {
358
374
  library: {
359
375
  cloneElement: () => {
@@ -364,8 +380,8 @@ parse('<br>', {
364
380
  },
365
381
  isValidElement: () => {
366
382
  /* ... */
367
- }
368
- }
383
+ },
384
+ },
369
385
  });
370
386
  ```
371
387
 
@@ -377,11 +393,11 @@ Default [htmlparser2 options](https://github.com/fb55/htmlparser2/wiki/Parser-op
377
393
 
378
394
  To enable [`xmlMode`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-xmlmode):
379
395
 
380
- ```js
396
+ ```ts
381
397
  parse('<p /><p />', {
382
398
  htmlparser2: {
383
- xmlMode: true
384
- }
399
+ xmlMode: true,
400
+ },
385
401
  });
386
402
  ```
387
403
 
@@ -389,30 +405,38 @@ parse('<p /><p />', {
389
405
 
390
406
  By default, whitespace is preserved:
391
407
 
392
- ```js
408
+ ```ts
393
409
  parse('<br>\n'); // [React.createElement('br'), '\n']
394
410
  ```
395
411
 
396
412
  But certain elements like `<table>` will strip out invalid whitespace:
397
413
 
398
- ```js
414
+ ```ts
399
415
  parse('<table>\n</table>'); // React.createElement('table')
400
416
  ```
401
417
 
402
418
  To remove whitespace, enable the `trim` option:
403
419
 
404
- ```js
420
+ ```ts
405
421
  parse('<br>\n', { trim: true }); // React.createElement('br')
406
422
  ```
407
423
 
408
424
  However, intentional whitespace may be stripped out:
409
425
 
410
- ```js
426
+ ```ts
411
427
  parse('<p> </p>', { trim: true }); // React.createElement('p')
412
428
  ```
413
429
 
414
430
  ## Migration
415
431
 
432
+ ### v5
433
+
434
+ Migrated to TypeScript. CommonJS imports require the `.default` key:
435
+
436
+ ```ts
437
+ const parse = require('html-react-parser').default;
438
+ ```
439
+
416
440
  ### v4
417
441
 
418
442
  [htmlparser2](https://github.com/fb55/htmlparser2) has been upgraded to [v9](https://github.com/fb55/htmlparser2/releases/tag/v9.0.0).
@@ -437,11 +461,11 @@ For the `replace` option, you may need to do the following:
437
461
  import { Element } from 'domhandler/lib/node';
438
462
 
439
463
  parse('<br class="remove">', {
440
- replace: (domNode) => {
464
+ replace(domNode) {
441
465
  if (domNode instanceof Element && domNode.attribs.class === 'remove') {
442
466
  return <></>;
443
467
  }
444
- }
468
+ },
445
469
  });
446
470
  ```
447
471
 
@@ -490,8 +514,8 @@ Tags are lowercased by default. To prevent that from happening, pass the [htmlpa
490
514
  ```js
491
515
  const options = {
492
516
  htmlparser2: {
493
- lowerCaseTags: false
494
- }
517
+ lowerCaseTags: false,
518
+ },
495
519
  };
496
520
  parse('<CustomElement>', options); // React.createElement('CustomElement')
497
521
  ```
@@ -527,8 +551,8 @@ Then update your Webpack config to:
527
551
  module.exports = {
528
552
  // ...
529
553
  resolve: {
530
- mainFields: ['browser', 'main', 'module']
531
- }
554
+ mainFields: ['browser', 'main', 'module'],
555
+ },
532
556
  };
533
557
  ```
534
558
 
@@ -552,15 +576,15 @@ Then upgrade to the latest version of [typescript](https://www.npmjs.com/package
552
576
  Run benchmark:
553
577
 
554
578
  ```sh
555
- npm run test:benchmark
579
+ npm run benchmark
556
580
  ```
557
581
 
558
- Output of benchmark run on MacBook Pro 2017:
582
+ Output of benchmark run on MacBook Pro 2021:
559
583
 
560
584
  ```
561
- html-to-react - Single x 415,186 ops/sec ±0.92% (85 runs sampled)
562
- html-to-react - Multiple x 139,780 ops/sec ±2.32% (87 runs sampled)
563
- html-to-react - Complex x 8,118 ops/sec ±2.99% (82 runs sampled)
585
+ html-to-react - Single x 1,018,239 ops/sec ±0.43% (94 runs sampled)
586
+ html-to-react - Multiple x 380,037 ops/sec ±0.61% (97 runs sampled)
587
+ html-to-react - Complex x 35,091 ops/sec ±0.50% (96 runs sampled)
564
588
  ```
565
589
 
566
590
  Run [Size Limit](https://github.com/ai/size-limit):