UNPKG

944 BJavaScriptView Raw
1var domToReact = require('./lib/dom-to-react');
2var htmlToDOM = require('html-dom-parser');
3
4// decode HTML entities by default for `htmlparser2`
5var domParserOptions = { decodeEntities: true, lowerCaseAttributeNames: false };
6
7/**
8 * Converts HTML string to React elements.
9 *
10 * @param {String} html - The HTML string to parse to React.
11 * @param {Object} [options] - The parser options.
12 * @param {Function} [options.replace] - The replace method.
13 * @return {ReactElement|Array|String} - When parsed with HTML string, returns React elements; otherwise, returns string or empty array.
14 */
15function HTMLReactParser(html, options) {
16 if (typeof html !== 'string') {
17 throw new TypeError('First argument must be a string');
18 }
19 return domToReact(htmlToDOM(html, domParserOptions), options);
20}
21
22HTMLReactParser.domToReact = domToReact;
23HTMLReactParser.htmlToDOM = htmlToDOM;
24
25module.exports = HTMLReactParser;