@@ -17,6 +17,7 @@ import { config } from 'livecodes';
17
17
import * as cheerio from 'cheerio' ;
18
18
19
19
export default class LivecodesPlugin extends Plugin {
20
+
20
21
settings ! : LivecodesSettings ;
21
22
manifest : PluginManifest ;
22
23
plugin : LivecodesPlugin ;
@@ -153,7 +154,7 @@ export default class LivecodesPlugin extends Plugin {
153
154
}
154
155
let regex = / h t t p s : \/ \/ c o d e p e n \. i o \/ [ a - z A - Z 0 - 9 _ \- ] { 1 , 50 } \/ p e n \/ [ a - z A - z 0 - 9 ] { 1 , 50 } / g;
155
156
if ( ! regex . test ( cpUrl ) ) {
156
- showNotice ( 'Error: Unable to validate as codepen.io URL. Click this message to dismiss' , 0 , 'error' ) ;
157
+ showNotice ( 'Error: Unable to validate as codepen.io URL. See the developer console for error details. Click this message to dismiss' , 0 , 'error' ) ;
157
158
return ;
158
159
}
159
160
showNotice ( `Fetching pen from ${ cpUrl } ` , 10000 , 'loading' ) ;
@@ -182,57 +183,90 @@ export default class LivecodesPlugin extends Plugin {
182
183
let itemJson = JSON . parse ( penJson . __item ) ;
183
184
console . log ( 'itemJson' ) ;
184
185
console . log ( itemJson ) ;
185
-
186
186
await saveAsModal ( this . app , "New livecodes playground" , "Save as:" , ( itemJson . title !== '' ) ? itemJson . title : 'Untitled' , "e.g. New Playground" , false )
187
187
. then ( async ( fName :string ) => {
188
188
if ( fName ?. length === 0 ) {
189
189
return ;
190
190
}
191
+
191
192
cnf . title = fName ;
192
193
cnf . description = ( itemJson . description !== '' ) ? itemJson . description : '' ;
193
194
cnf . tags = ( itemJson . tags . length ) ? itemJson . tags : [ ] ;
194
- if ( itemJson . css_pre_processor === "scss" ) {
195
- cnf . style . language = 'scss' ;
195
+
196
+ let extStylesheets :any [ ] = [ ] ;
197
+ let extScripts :any [ ] = [ ] ;
198
+ if ( itemJson . resources . length ) {
199
+ itemJson . resources . forEach ( ( resource : any ) => {
200
+ if ( resource . resource_type === 'css' ) {
201
+ extStylesheets = [ ...extStylesheets , resource . url ] ;
202
+ }
203
+ if ( resource . resource_type === 'js' ) {
204
+ extScripts = [ ...extScripts , resource . url ] ;
205
+ }
206
+ } ) ;
207
+ if ( extStylesheets . length ) {
208
+ cnf . stylesheets = extStylesheets ;
209
+ }
210
+ if ( extScripts . length ) {
211
+ cnf . scripts = extScripts ;
212
+ }
213
+ }
214
+
215
+ // style lang
216
+ if ( itemJson . css_pre_processor !== '' && [ 'scss' , 'less' , 'stylus' ] . contains ( itemJson . css_pre_processor ) ) {
217
+ cnf . style . language = itemJson . css_pre_processor ;
196
218
}
197
219
else {
198
220
cnf . style . language = 'css'
199
221
}
222
+ if ( itemJson . css_starter !== '' ) {
223
+ switch ( itemJson . css_starter ) {
224
+ case "reset" :
225
+ cnf . cssPreset = 'reset-css' ;
226
+ break ;
227
+ case "normalize" :
228
+ cnf . cssPreset = 'normalize.css' ;
229
+ break ;
230
+ }
231
+ }
200
232
cnf . style . content = itemJson . css ;
201
- cnf . markup . language = 'html' ;
202
- cnf . markup . content = "<!-- source: " + cpUrl + " -->\n\n" + itemJson . html ;
203
- cnf . script . language = 'javascript' ;
233
+
234
+ // js library
204
235
if ( itemJson . js_library !== '' ) {
205
236
switch ( itemJson . js_library ) {
206
237
case "jquery" :
207
238
cnf . head = blankPlayground . head + '\n<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>' ;
208
239
break ;
209
-
210
- default :
211
- break ;
212
240
}
213
241
}
214
- if ( itemJson . js_pre_processor !== '' ) {
215
- switch ( itemJson . js_pre_processor ) {
216
- case "coffeescript" :
217
- cnf . script . language = itemJson . js_pre_processor ;
218
- break ;
219
-
220
- default :
221
- break ;
222
- }
242
+
243
+ // script lang
244
+ if ( itemJson . js_pre_processor !== '' && [ 'coffeescript' , 'typescript' , 'livescript' , 'babel' ] . contains ( itemJson . js_pre_processor ) ) {
245
+ cnf . script . language = itemJson . js_pre_processor ;
246
+ } else {
247
+ cnf . script . language = 'javascript' ;
223
248
}
224
249
cnf . script . content = itemJson . js ;
225
- if ( itemJson . html_pre_processor === "pug" ) {
226
- switch ( itemJson . html_pre_processor ) {
227
- case "pug" :
228
- cnf . markup . language = itemJson . html_pre_processor ;
229
- break ;
230
-
231
- default :
232
- break ;
250
+
251
+ // markup lang
252
+ if ( itemJson . html_pre_processor !== '' && [ 'pug' , 'markdown' ] . contains ( itemJson . html_pre_processor ) ) {
253
+ cnf . markup . language = itemJson . html_pre_processor ;
254
+ } else {
255
+ cnf . markup . language = 'html' ;
256
+ }
257
+ cnf . markup . content = "<!-- source: " + cpUrl + " -->\n\n" + itemJson . html ;
258
+
259
+ if ( itemJson . head !== '' ) {
260
+ if ( cnf . head !== undefined ) {
261
+ cnf . head = cnf . head + '\n' + itemJson . head ;
262
+ } else {
263
+ cnf . head = blankPlayground . head + '\n' + itemJson . head ;
233
264
}
234
265
}
235
- cnf . script . content = itemJson . js ;
266
+ if ( itemJson . html_classes !== '' ) {
267
+ cnf . htmlAttrs = `lang="en" class="${ itemJson . html_classes } "`
268
+ }
269
+
236
270
let newPlayground :Partial < config > = { ...blankPlayground , ...cnf } ;
237
271
newPlayground . appUrl = this . settings . appUrl ;
238
272
newPlayground . fontFamily = this . settings . fontFamily ;
@@ -445,8 +479,8 @@ export default class LivecodesPlugin extends Plugin {
445
479
}
446
480
447
481
private checkForCodeblocks (
448
- editor : Editor
449
- ) : boolean {
482
+ editor : Editor
483
+ ) : boolean {
450
484
const PATTERN = / ^ ( [ A - Z a - z \t ] * ) ` ` ` ( [ A - Z a - z ] * ) ? \n ( [ \s \S ] * ?) ` ` ` ( [ A - Z a - z \t ] * ) * $ / gm;
451
485
let markdown = editor . getValue ( ) ;
452
486
let matches ;
@@ -985,7 +1019,7 @@ export default class LivecodesPlugin extends Plugin {
985
1019
onClickCodeblock ( event : MouseEvent ) {
986
1020
let target = event . target as HTMLElement ;
987
1021
let nodeType = target . localName ;
988
- if ( nodeType !== 'code' && ! ( target . parentElement instanceof HTMLPreElement ) ) {
1022
+ if ( nodeType !== 'code' && ! ( target . parentElement instanceof HTMLPreElement ) ) {
989
1023
return ;
990
1024
}
991
1025
let lang = 'text' ;
0 commit comments