Skip to content

Commit 04e99f0

Browse files
committed
additional codepen conf import
1 parent 65f8bec commit 04e99f0

File tree

1 file changed

+65
-31
lines changed

1 file changed

+65
-31
lines changed

src/main.ts

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { config } from 'livecodes';
1717
import * as cheerio from 'cheerio';
1818

1919
export default class LivecodesPlugin extends Plugin {
20+
2021
settings!: LivecodesSettings;
2122
manifest: PluginManifest;
2223
plugin: LivecodesPlugin;
@@ -153,7 +154,7 @@ export default class LivecodesPlugin extends Plugin {
153154
}
154155
let regex = /https:\/\/codepen\.io\/[a-zA-Z0-9_\-]{1,50}\/pen\/[a-zA-z0-9]{1,50}/g;
155156
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');
157158
return;
158159
}
159160
showNotice(`Fetching pen from ${cpUrl}`, 10000, 'loading');
@@ -182,57 +183,90 @@ export default class LivecodesPlugin extends Plugin {
182183
let itemJson = JSON.parse(penJson.__item);
183184
console.log('itemJson');
184185
console.log(itemJson);
185-
186186
await saveAsModal(this.app, "New livecodes playground", "Save as:", (itemJson.title !== '') ? itemJson.title : 'Untitled', "e.g. New Playground", false)
187187
.then(async (fName:string) => {
188188
if (fName?.length === 0) {
189189
return;
190190
}
191+
191192
cnf.title = fName;
192193
cnf.description = (itemJson.description !== '') ? itemJson.description : '';
193194
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;
196218
}
197219
else {
198220
cnf.style.language = 'css'
199221
}
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+
}
200232
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
204235
if (itemJson.js_library !== '') {
205236
switch (itemJson.js_library) {
206237
case "jquery":
207238
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>';
208239
break;
209-
210-
default:
211-
break;
212240
}
213241
}
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';
223248
}
224249
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;
233264
}
234265
}
235-
cnf.script.content = itemJson.js;
266+
if (itemJson.html_classes !== '') {
267+
cnf.htmlAttrs = `lang="en" class="${itemJson.html_classes}"`
268+
}
269+
236270
let newPlayground:Partial<config> = {...blankPlayground, ...cnf};
237271
newPlayground.appUrl = this.settings.appUrl;
238272
newPlayground.fontFamily = this.settings.fontFamily;
@@ -445,8 +479,8 @@ export default class LivecodesPlugin extends Plugin {
445479
}
446480

447481
private checkForCodeblocks(
448-
editor: Editor
449-
): boolean {
482+
editor: Editor
483+
): boolean {
450484
const PATTERN = /^([A-Za-z \t]*)```([A-Za-z]*)?\n([\s\S]*?)```([A-Za-z \t]*)*$/gm;
451485
let markdown = editor.getValue();
452486
let matches;
@@ -985,7 +1019,7 @@ export default class LivecodesPlugin extends Plugin {
9851019
onClickCodeblock(event: MouseEvent) {
9861020
let target = event.target as HTMLElement;
9871021
let nodeType = target.localName;
988-
if (nodeType !== 'code' && !(target.parentElement instanceof HTMLPreElement)) {
1022+
if (nodeType !== 'code' && !(target.parentElement instanceof HTMLPreElement)) {
9891023
return;
9901024
}
9911025
let lang = 'text';

0 commit comments

Comments
 (0)