@@ -55,7 +55,13 @@ const generateUnformattedCode = (
55
55
if (
56
56
referencedHTML . tag === 'div' ||
57
57
referencedHTML . tag === 'separator' ||
58
- referencedHTML . tag === 'form'
58
+ // Caret Start
59
+ referencedHTML . tag === 'form' ||
60
+ referencedHTML . tag === 'ul' ||
61
+ referencedHTML . tag === 'ol' ||
62
+ referencedHTML . tag === 'menu' ||
63
+ referencedHTML . tag === 'li'
64
+ // Caret End
59
65
) {
60
66
child . children = getEnrichedChildren ( child ) ;
61
67
}
@@ -92,45 +98,51 @@ const generateUnformattedCode = (
92
98
return customizationDetails ;
93
99
} ;
94
100
101
+ // function to dynamically generate a complete html (& also other library type) elements
102
+ const elementGenerator = ( childElement : object , level : number = 2 ) => {
103
+ let innerText = '' ;
104
+ if ( childElement . attributes && childElement . attributes . compText ) innerText = childElement . attributes . compText ;
105
+
106
+ const tabSpacer = level => {
107
+ let tabs = ''
108
+ for ( let i = 0 ; i < level ; i ++ ) tabs += ' ' ;
109
+ return tabs ;
110
+ }
111
+
112
+ const nestable = childElement . tag === 'div' ||
113
+ childElement . tag === 'form' ||
114
+ childElement . tag === 'ol' ||
115
+ childElement . tag === 'ul' ||
116
+ childElement . tag === 'menu' ||
117
+ childElement . tag === 'li' ;
118
+
119
+ if ( childElement . tag === 'img' ) {
120
+ return `<${ childElement . tag } src="" ${ elementTagDetails ( childElement ) } />` ;
121
+ } else if ( childElement . tag === 'a' ) {
122
+ return `<${ childElement . tag } href=""${ elementTagDetails ( childElement ) } >${ innerText } </${ childElement . tag } >` ;
123
+ } else if ( childElement . tag === 'input' ) {
124
+ return `<${ childElement . tag } ${ elementTagDetails ( childElement ) } ></${ childElement . tag } >` ;
125
+ } else if ( nestable && level === 2 ) {
126
+ return `\n${ tabSpacer ( 5 ) } <${ childElement . tag } ${ elementTagDetails ( childElement ) } >${ innerText }
127
+ ${ tabSpacer ( level ) } ${ writeNestedElements ( childElement . children , level + 1 ) }
128
+ ${ tabSpacer ( level - 1 ) } </${ childElement . tag } >` ;
129
+ } else if ( nestable && level > 2 ) {
130
+ return `<${ childElement . tag } ${ elementTagDetails ( childElement ) } >${ innerText }
131
+ ${ tabSpacer ( level ) } ${ writeNestedElements ( childElement . children , level + 1 ) }
132
+ ${ tabSpacer ( level - 1 ) } </${ childElement . tag } >` ;
133
+ } else if ( childElement . tag !== 'separator' ) {
134
+ return `<${ childElement . tag } ${ elementTagDetails ( childElement ) } >${ innerText } </${ childElement . tag } >` ;
135
+ }
136
+ }
137
+
95
138
// write all code that will be under the "return" of the component
96
- const writeNestedElements = ( enrichedChildren : any ) => {
139
+ const writeNestedElements = ( enrichedChildren : any , level : number = 2 ) => {
97
140
return `${ enrichedChildren
98
141
. map ( ( child : any ) => {
99
- console . log ( "What is in this child" , child ) ;
100
- let innerText = '' ;
101
- if ( child . attributes && child . attributes . compText ) innerText = child . attributes . compText ;
102
142
if ( child . type === 'Component' ) {
103
143
return `<${ child . name } ${ elementTagDetails ( child ) } />` ;
104
144
} else if ( child . type === 'HTML Element' ) {
105
- if ( child . tag === 'img' ) {
106
- return `<${ child . tag } src="" ${ elementTagDetails ( child ) } />` ;
107
- } else if ( child . tag === 'a' ) {
108
- return `<${ child . tag } href=""${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
109
- } else if ( child . tag === 'div' ) {
110
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } \n${ writeNestedElements ( child . children ) } </${ child . tag } >` ;
111
- } else if ( child . tag === 'h1' ) {
112
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
113
- } else if ( child . tag === 'h2' ) {
114
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
115
- } else if ( child . tag === 'form' ) {
116
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } \n${ writeNestedElements ( child . children ) } </${ child . tag } >` ;
117
- } else if ( child . tag === 'input' ) {
118
- return `<${ child . tag } ${ elementTagDetails ( child ) } ></${ child . tag } >` ;
119
- } else if ( child . tag === 'label' ) {
120
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
121
- } else if ( child . tag === 'p' ) {
122
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
123
- } else if ( child . tag === 'ol' ) {
124
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } \n${ writeNestedElements ( child . children ) } </${ child . tag } >` ;
125
- } else if ( child . tag === 'ul' ) {
126
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } \n${ writeNestedElements ( child . children ) } </${ child . tag } >` ;
127
- } else if ( child . tag === 'li' ) {
128
- return `<${ child . tag } ${ elementTagDetails ( child ) } ></${ child . tag } >` ;
129
- } else if ( child . tag === 'button' ) {
130
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
131
- } else if ( child . tag !== 'separator' ) {
132
- return `<${ child . tag } ${ elementTagDetails ( child ) } >${ innerText } </${ child . tag } >` ;
133
- }
145
+ return elementGenerator ( child , level ) ;
134
146
}
135
147
// route links are for gatsby.js and next.js feature. if the user creates a route link and then switches projects, generate code for a normal link instead
136
148
else if ( child . type === 'Route Link' ) {
@@ -145,9 +157,9 @@ const generateUnformattedCode = (
145
157
}
146
158
} )
147
159
. filter ( element => ! ! element )
148
- . join ( '\n ' ) } `;
160
+ . join ( '' ) } `;
149
161
} ;
150
-
162
+ // Caret End
151
163
152
164
const enrichedChildren : any = getEnrichedChildren ( currentComponent ) ;
153
165
@@ -180,11 +192,11 @@ const generateUnformattedCode = (
180
192
${ classBased ? `import React, {Component} from 'react';` : '' }
181
193
${ ! stateful && ! classBased ? `import React from 'react';` : '' }
182
194
${ importsMapped }
183
- ${
184
- classBased
185
- ? `class ${ currentComponent . name } extends Component {`
186
- : `const ${ currentComponent . name } = (props): JSX.Element => {`
187
- }
195
+ ${
196
+ classBased
197
+ ? `class ${ currentComponent . name } extends Component {`
198
+ : `const ${ currentComponent . name } = (props): JSX.Element => {`
199
+ }
188
200
${
189
201
stateful && ! classBased
190
202
? `const [value, setValue] = useState<any | undefined>("INITIAL VALUE");`
@@ -195,20 +207,20 @@ const generateUnformattedCode = (
195
207
? `constructor(props) {
196
208
super(props);
197
209
this.state = {}
198
- }`
210
+ }`
199
211
: ``
200
212
}
201
213
202
214
${ classBased ? `render(): JSX.Element {` : `` }
203
215
204
216
return (
205
217
<div className="${ currentComponent . name } " style={props.style}>
206
- ${ writeNestedElements ( enrichedChildren ) }
218
+ ${ writeNestedElements ( enrichedChildren ) }
207
219
</div>
208
220
);
209
221
}
210
- ${ classBased ? `}` : `` }
211
- export default ${ currentComponent . name } ;
222
+ ${ classBased ? `}` : `` }
223
+ export default ${ currentComponent . name } ;
212
224
` ;
213
225
}
214
226
// next.js component code
@@ -219,27 +231,27 @@ const generateUnformattedCode = (
219
231
import Head from 'next/head'
220
232
${ links ? `import Link from 'next/link'` : `` }
221
233
222
- const ${ currentComponent . name } = (props): JSX.Element => {
234
+ const ${ currentComponent . name } = (props): JSX.Element => {
223
235
224
- const [value, setValue] = useState<any | undefined>("INITIAL VALUE");
236
+ const [value, setValue] = useState<any | undefined>("INITIAL VALUE");
225
237
226
238
return (
227
- <>
228
- ${
229
- isRoot
230
- ? `<Head>
231
- <title>${ currentComponent . name } </title>
232
- </Head>`
233
- : ``
234
- }
235
- <div className="${ currentComponent . name } " style={props.style}>
236
- ${ writeNestedElements ( enrichedChildren ) }
237
- </div>
238
- </>
239
- );
239
+ <>
240
+ ${
241
+ isRoot
242
+ ? `<Head>
243
+ <title>${ currentComponent . name } </title>
244
+ </Head>`
245
+ : ``
240
246
}
247
+ <div className="${ currentComponent . name } " style={props.style}>
248
+ ${ writeNestedElements ( enrichedChildren ) }
249
+ </div>
250
+ </>
251
+ );
252
+ }
241
253
242
- export default ${ currentComponent . name } ;
254
+ export default ${ currentComponent . name } ;
243
255
` ;
244
256
} else {
245
257
// gatsby component code
0 commit comments