|
| 1 | +import { HTMLReactParserOptions } from 'html-react-parser'; |
| 2 | +import domToReact from 'html-react-parser/lib/dom-to-react'; |
| 3 | +import * as React from 'react'; |
| 4 | +import htmlToDOM from 'html-dom-parser'; |
| 5 | + |
| 6 | +/* $ExpectType ReactElement<any, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => Component<any, any, any>)> | null) | |
| 7 | +(new (props: any) => Component<any, any, any>)> | |
| 8 | +ReactElement<any, string | ((props: any) => ReactElement<any, string | any | (new (props: any) => Component<any, any, any>)> | null) | |
| 9 | +(new (props: any) => Component<any, any, any>)>[] */ |
| 10 | +domToReact(htmlToDOM('<div>text</div>')); |
| 11 | + |
| 12 | +// `options.replace` |
| 13 | + |
| 14 | +// Return React.createElement from `replace` |
| 15 | +domToReact(htmlToDOM('<p id="replace">text</p>'), { |
| 16 | + replace: domNode => { |
| 17 | + if (domNode.attribs && domNode.attribs.id === 'replace') { |
| 18 | + return React.createElement('span', {}, 'replaced'); |
| 19 | + } |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +// Return ReactElement |
| 24 | +const options: HTMLReactParserOptions = { |
| 25 | + replace({ attribs }) { |
| 26 | + return attribs && attribs.id === 'remove' && <React.Fragment />; |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +domToReact(htmlToDOM('<p><br id="remove"></p>'), options); |
| 31 | + |
| 32 | +// Return domhandler node |
| 33 | +domToReact(htmlToDOM('<a id="header" href="#">Heading</a>'), { |
| 34 | + replace: node => { |
| 35 | + if (node.attribs && node.attribs.id === 'header') { |
| 36 | + return { |
| 37 | + type: 'h1', |
| 38 | + props: { children: 'Heading' } |
| 39 | + }; |
| 40 | + } |
| 41 | + } |
| 42 | +}); |
0 commit comments