Skip to content

Commit 52aba63

Browse files
committed
hopefully merging everythign correctly
2 parents a04bf9c + 39b58ca commit 52aba63

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

codepen-data.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ var types = {
66
html: function htmlType(text) {
77

88
var result;
9+
var HTML = text;
910

1011
text.replace(scriptRegExp, function(match, attrs, code) {
1112

1213
var matchTest = attrs.match(moduleTest);
1314

14-
if (matchTest && !srcTest.test(attrs)) {
15+
// This has a src="". We look for codepen-external
16+
if(srcTest.test(attrs)) {
1517

16-
var HTML = text.replace(match, "").trim();
18+
}
19+
// It doesn't have a src, so we assume this has a body
20+
else if (matchTest) {
21+
22+
HTML = HTML.replace(match, "").trim();
1723
var CSS;
1824
var styleResults = HTML.match(styleRegExp);
1925
if (styleResults) {

index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ var assign = Object.assign || function(d, s) {
1111
function cleanCodePenData(data) {
1212
if(docObject.codepen) {
1313
docObject.codepen.forEach(function(replacement){
14+
if(data.html) {
15+
data.html = data.html.split(replacement[0]).join(replacement[1]);
16+
}
1417
if(data.js) {
1518
data.js = data.js.split(replacement[0]).join(replacement[1]);
1619
}
@@ -19,20 +22,8 @@ function cleanCodePenData(data) {
1922
}
2023

2124
function createCodePen(data) {
22-
if(data.html) {
23-
// HTML needs to be escaped because put this in the page
24-
data = assign({}, data);
25-
data.html = data.html.replace(/&/g,"&")
26-
.replace(/</g,"&lt;")
27-
.replace(/>/g,"&gt;");
28-
}
29-
30-
var JSONstring =
31-
JSON.stringify(data)
32-
// Quotes will screw up the JSON
33-
.replace(/"/g, "&quot;")
34-
.replace(/'/g, "&apos;");
3525

26+
var JSONstring = JSON.stringify(data);
3627

3728
var form = '<form action="https://codepen.io/pen/define" method="POST" target="_blank">' +
3829
'<input type="hidden" name="data">' +
@@ -128,6 +119,12 @@ module.exports = function() {
128119
if(data.html) {
129120
data.html = data.html.trim();
130121
}
122+
if (data.js && data.js.indexOf('import') > -1) {
123+
var jsAsModule = "<script type=\"module\">\n" + data.js + "\n</script>";
124+
data.html = data.html ? data.html + "\n\n" + jsAsModule : jsAsModule;
125+
data.js = "";
126+
data.editors = "1001";// HTML, Result, & Console
127+
}
131128
if(data) {
132129
cleanCodePenData(data);
133130
if(window.CREATE_CODE_PEN) {
@@ -146,6 +143,10 @@ module.exports = function() {
146143

147144
var jsCode = el.querySelector("[data-for=js] code");
148145
var jsText = jsCode ? jsCode.textContent.trim() : "";
146+
if (jsText) {
147+
htmlText += "\n<script type=\"module\">\n" + jsText + "\n</script>";
148+
jsText = "";
149+
}
149150

150151
var cssText = getStylesFromIframe( el.querySelector("iframe") );
151152

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bit-docs-html-codepen-link",
3-
"version": "1.3.2",
3+
"version": "1.3.5",
44
"description": "add a codepen link ",
55
"main": "index.js",
66
"scripts": {
@@ -28,7 +28,7 @@
2828
"devDependencies": {
2929
"bit-docs-generate-html": "^0.1.0",
3030
"connect": "^2.14.4",
31-
"mocha": "^2.5.3",
31+
"mocha": "^6.0.2",
3232
"zombie": "^4.3.0"
3333
}
3434
}

test.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,17 @@ describe("bit-docs-html-codepen-link", function() {
6060
codePen.click();
6161
});
6262
assert.deepEqual(createCallData, [{
63-
html: '<my-app></my-app>',
64-
js: 'import { Component } from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nComponent',
63+
html: '<my-app></my-app>\n\n<script type="module">\nimport { Component } from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nComponent\n</script>',
64+
js: '',
6565
js_module: true,
66-
editors: '1011',
66+
editors: '1001',
6767
css: 'my-app {color: "green";}'
6868
},
6969
{
70-
js: 'import {DefineMap} from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nconsole.log( myCounter.count ) //-> 1',
70+
html: '<script type="module">\nimport {DefineMap} from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nconsole.log( myCounter.count ) //-> 1\n</script>',
71+
js: '',
7172
js_module: true,
72-
editors: '0011'
73+
editors: '1001'
7374
}
7475
]);
7576

@@ -114,4 +115,18 @@ describe("bit-docs-html-codepen-link", function() {
114115
assert.equal(data.js.trim(), 'const element = <h1>Hello, world!</h1>;');
115116
assert.equal(data.js_pre_processor, 'babel');
116117
});
118+
119+
it.skip("is able to create external js", function(){
120+
var data = codepenData.html(`
121+
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.2.1/rxjs.umd.js" codepen-external></script>
122+
<script src="https://foo.com" codepen-external></script>
123+
<script type="typescript">
124+
const {Observable} = rxjs;
125+
</script>
126+
`);
127+
assert.equal(data.js.trim(), 'const {Observable} = rxjs;');
128+
assert.equal(data.js_pre_processor, 'typescript');
129+
assert.equal(data.js_external,'https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.2.1/rxjs.umd.js;https://foo.com');
130+
assert.equal(data.html, undefined, "no html")
131+
});
117132
});

0 commit comments

Comments
 (0)