forked from remarkablemark/html-react-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (38 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Comment, Element, ProcessingInstruction, Text } from 'domhandler';
import htmlToDOM from 'html-dom-parser';
import attributesToProps from './lib/attributes-to-props.js';
import domToReact from './lib/dom-to-react.js';
var domParserOptions = { lowerCaseAttributeNames: false };
/**
* Converts HTML string to React elements.
*
* @param {string} html - HTML string.
* @param {object} [options] - Parser options.
* @param {object} [options.htmlparser2] - htmlparser2 options.
* @param {object} [options.library] - Library for React, Preact, etc.
* @param {Function} [options.replace] - Replace method.
* @returns {JSX.Element|JSX.Element[]|string} - React element(s), empty array, or string.
*/
function HTMLReactParser(html, options) {
if (typeof html !== 'string') {
throw new TypeError('First argument must be a string');
}
if (html === '') {
return [];
}
options = options || {};
return domToReact(
htmlToDOM(html, options.htmlparser2 || domParserOptions),
options
);
}
HTMLReactParser.domToReact = domToReact;
HTMLReactParser.htmlToDOM = htmlToDOM;
HTMLReactParser.attributesToProps = attributesToProps;
// domhandler
HTMLReactParser.Comment = Comment;
HTMLReactParser.Element = Element;
HTMLReactParser.ProcessingInstruction = ProcessingInstruction;
HTMLReactParser.Text = Text;
HTMLReactParser.default = HTMLReactParser;
export default HTMLReactParser;