File tree Expand file tree Collapse file tree 1 file changed +34
-2
lines changed Expand file tree Collapse file tree 1 file changed +34
-2
lines changed Original file line number Diff line number Diff line change 11import HTMLtoJSX from 'htmltojsx' ;
22
3- export default function htmlToJsx ( html , indent ) {
3+ const prepareHtml = ( html ) => {
4+ const tree = [ ] ;
5+
6+ return html . replace (
7+ / ( < ( [ a - z \. ] + ) | \/ > | < \/ | > ) / gi,
8+ ( match , tagFragment , tagName ) => {
9+ // If we have a tag name, this is an opening tag
10+ if ( tagName ) {
11+ tagFragment = tagFragment [ 0 ] ;
12+ }
13+
14+ switch ( tagFragment ) {
15+ case "<" :
16+ tree . push ( tagName ) ;
17+ break ;
18+ case "/>" :
19+ match = `></${ tree . pop ( ) } >` ;
20+ break ;
21+ case ">" :
22+ break ;
23+ case "</" :
24+ tree . pop ( ) ;
25+ break ;
26+ }
27+
28+ return match ;
29+ }
30+ ) ;
31+ } ;
32+
33+ export default ( html , indent ) => {
34+ html = prepareHtml ( html ) ;
35+
436 const jsxConverter = new HTMLtoJSX ( { createClass : false } ) ;
537
638 let jsx = jsxConverter . convert ( html ) ;
@@ -16,4 +48,4 @@ export default function htmlToJsx(html, indent) {
1648 return jsx
1749 . replace ( / \n \s { 8 } / g, `\n${ indent } ` ) // Indent for pretty inspector output 🎉
1850 . replace ( / \n \s * $ / g, '' ) ; // Remove the trailing blank line
19- }
51+ } ;
You can’t perform that action at this time.
0 commit comments