Skip to content

Commit f87d8f1

Browse files
committed
add support for using less in codepen examples
1 parent bd2fb26 commit f87d8f1

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

codepen-data.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var scriptRegExp = /<script\s([^>]+)>([\s\S]*?)<\/script>/ig;
22
var styleRegExp = /<style>([\s\S]*?)<\/style>/i;
3+
var lessRegExp = /<style type="text\/less">([\s\S]*?)<\/style>/i;
34
var templateRegExp = /<template\s?([^>]+)?>([\s\S]*?)<\/template>/ig;
45
var moduleTest = /type=["']([\w\/]+)["']/;
56
var srcTest = /src=/;
@@ -22,12 +23,18 @@ var types = {
2223
// It doesn't have a src, so we assume this has a body
2324
else if (matchTest) {
2425
HTML = HTML.replace(match, "").trim();
25-
var CSS;
26+
var CSS, PRECSS;
2627
var styleResults = HTMLwithoutTemplates.match(styleRegExp);
2728
if (styleResults) {
2829
HTML = HTML.replace(styleResults[0], "").trim();
2930
CSS = styleResults[1].trim();
3031
}
32+
var lessResults = HTMLwithoutTemplates.match(lessRegExp);
33+
if (lessResults) {
34+
HTML = HTML.replace(lessResults[0], "").trim();
35+
CSS = lessResults[1].trim();
36+
PRECSS = 'less';
37+
}
3138
if (types[matchTest[1]]) {
3239
result = types[matchTest[1]](code.trim());
3340
} else {
@@ -39,6 +46,10 @@ var types = {
3946
}
4047
if (CSS) {
4148
result.css = CSS;
49+
50+
if (PRECSS) {
51+
result.css_pre_processor = PRECSS;
52+
}
4253
}
4354
}
4455
});

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ describe("bit-docs-html-codepen-link", function() {
115115
assert.equal(data.js_pre_processor, 'babel');
116116
});
117117

118+
it("is able to parse less in html", function() {
119+
var data = codepenData.html(`
120+
<style type="text/less">
121+
@custom-color: #454545;
122+
</style>
123+
<span>Hello.</span>
124+
<script type="module">
125+
function greeter(person) { return "Hello, " + person; }
126+
</script>
127+
`);
128+
console.log(JSON.stringify(data));
129+
assert.equal(data.css.trim(), '@custom-color: #454545;');
130+
assert.equal(data.css_pre_processor, 'less');
131+
});
132+
133+
118134
it.skip("is able to create external js", function(){
119135
var data = codepenData.html(`
120136
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.2.1/rxjs.umd.js" codepen-external></script>

0 commit comments

Comments
 (0)