-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodepen-data.js
93 lines (84 loc) · 2.14 KB
/
codepen-data.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var scriptRegExp = /<script\s([^>]+)>([\s\S]*?)<\/script>/ig;
var styleRegExp = /<style>([\s\S]*?)<\/style>/i;
var lessRegExp = /<style type="text\/less">([\s\S]*?)<\/style>/i;
var templateRegExp = /<template\s?([^>]+)?>([\s\S]*?)<\/template>/ig;
var moduleTest = /type=["']([\w\/]+)["']/;
var srcTest = /src=/;
var DEFAULT_EDITORS = "0011";
function typescript(text) {
return {
js: text,
js_pre_processor: "typescript",
editors: DEFAULT_EDITORS
};
}
var types = {
html: function htmlType(text) {
var result;
var HTML = text;
var textWithoutTemplates = text.replace(templateRegExp, "");
textWithoutTemplates.replace(scriptRegExp, function(match, attrs, code) {
var matchTest = attrs.match(moduleTest);
var HTMLwithoutTemplates = HTML.replace(templateRegExp, "");
// This has a src="". We look for codepen-external
if(srcTest.test(attrs)) {
}
// It doesn't have a src, so we assume this has a body
else if (matchTest) {
HTML = HTML.replace(match, "").trim();
var CSS, PRECSS;
var styleResults = HTMLwithoutTemplates.match(styleRegExp);
if (styleResults) {
HTML = HTML.replace(styleResults[0], "").trim();
CSS = styleResults[1].trim();
}
var lessResults = HTMLwithoutTemplates.match(lessRegExp);
if (lessResults) {
HTML = HTML.replace(lessResults[0], "").trim();
CSS = lessResults[1].trim();
PRECSS = 'less';
}
if (types[matchTest[1]]) {
result = types[matchTest[1]](code.trim());
} else {
result = types.js(code.trim());
}
result.editors = "1011";
if (HTML) {
result.html = HTML;
}
if (CSS) {
result.css = CSS;
if (PRECSS) {
result.css_pre_processor = PRECSS;
}
}
}
});
// If there are no scripts the should at least be HTML
if(!result) {
result = {
html: HTML,
editors: DEFAULT_EDITORS
}
}
return result;
},
js: function(text) {
return {
js: text,
js_module: true,
editors: DEFAULT_EDITORS
};
},
typescript: typescript,
ts: typescript,
jsx: function(text) {
return {
js: text,
js_pre_processor: "babel",
editors: DEFAULT_EDITORS
};
}
};
module.exports = types;