Skip to content

Commit 45a006a

Browse files
committed
basics working
1 parent 629f053 commit 45a006a

File tree

9 files changed

+334
-0
lines changed

9 files changed

+334
-0
lines changed

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# Compiled binary addons (http://nodejs.org/api/addons.html)
20+
build/Release
21+
22+
# Dependency directory
23+
# Commenting this out is preferred by some people, see
24+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
25+
node_modules
26+
27+
# Users Environment Variables
28+
.lock-wscript
29+
30+
docs/
31+
dist/
32+
temp/
33+
.vscode/settings.json
34+
package-lock.json

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

bit-docs.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var tags = require("./tags");
2+
3+
module.exports = function(bitDocs){
4+
var pkg = require("./package.json");
5+
var deps = {};
6+
7+
deps[pkg.name] = pkg.version;
8+
9+
bitDocs.register("html", {
10+
dependencies: deps
11+
});
12+
13+
bitDocs.register("tags", tags);
14+
};

codepen-data.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var scriptRegExp = /<script\s([^>]+)>([\s\S]*?)<\/script>/i;
2+
var moduleTest = /type=["']module["']/;
3+
var types = {
4+
html: function htmlType(text){
5+
// test if a module script tag exists
6+
var results = text.match(scriptRegExp);
7+
if(results) {
8+
var attrs = results[1];
9+
if(moduleTest.test(attrs)) {
10+
11+
var HTML = text.replace(results[0],"").trim();
12+
13+
return {
14+
html: HTML,
15+
js: results[2],
16+
js_module: true,
17+
editors: "1011"
18+
};
19+
}
20+
}
21+
},
22+
js: function (text){
23+
return {
24+
js: text,
25+
js_module: true,
26+
editors: "0011"
27+
};
28+
}
29+
};
30+
31+
module.exports = types;

index.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
require("bit-docs-prettify");
2+
3+
require("prismjs/plugins/line-highlight/prism-line-highlight");
4+
require("prismjs/plugins/line-highlight/prism-line-highlight.css");
5+
6+
7+
8+
9+
var types = require("./codepen-data");
10+
var languageHTML = /language-(\w+)/;
11+
12+
function createCodePen(data) {
13+
14+
var JSONstring =
15+
JSON.stringify(data)
16+
// Quotes will screw up the JSON
17+
.replace(/"/g, "&​quot;") // careful copy and pasting, I had to use a zero-width space here to get markdown to post this.
18+
.replace(/'/g, "&apos;");
19+
20+
21+
var form = '<form action="https://codepen.io/pen/define" method="POST" target="_blank">' +
22+
'<input type="hidden" name="data" value=\'' +
23+
JSONstring +
24+
'\'>' +
25+
'</form>';
26+
27+
var div = document.createElement("div");
28+
div.innerHTML = form;
29+
document.body.appendChild(div);
30+
div.firstChild.submit();
31+
setTimeout(function(){
32+
document.body.removeChild(div);
33+
},10);
34+
}
35+
36+
37+
var matches = document.body.matches || document.body.msMatchesSelector;
38+
39+
function findPre(start) {
40+
while(start) {
41+
if(start.nodeName === "PRE") {
42+
return start;
43+
}
44+
if(start.querySelector) {
45+
var pre = start.querySelector("pre");
46+
if(pre) {
47+
return pre;
48+
}
49+
}
50+
51+
// needs to be previousSibling for zombie
52+
start = start.previousSibling;
53+
}
54+
}
55+
56+
module.exports = function() {
57+
58+
document.body.addEventListener("click", function(ev){
59+
60+
if(matches.call(ev.target, ".codepen")){
61+
62+
var preElement = findPre(ev.target);
63+
64+
if(preElement) {
65+
var codeElement = preElement.querySelector("code");
66+
var language = codeElement.className.match(languageHTML)[1];
67+
var text = codeElement.textContent;
68+
69+
var data = types[language](text);
70+
if(docObject.codepen) {
71+
docObject.codepen.forEach(function(replacement){
72+
if(data.js) {
73+
data.js = data.js.split(replacement[0]).join(replacement[1]);
74+
}
75+
});
76+
}
77+
if(data.js) {
78+
data.js = data.js.trim();
79+
}
80+
if(data.html) {
81+
data.html = data.html.trim();
82+
}
83+
if(data) {
84+
if(window.CREATE_CODE_PEN) {
85+
CREATE_CODE_PEN(data);
86+
} else {
87+
createCodePen(data);
88+
}
89+
90+
} else {
91+
console.warn("Unable to create a codepen for this demo");
92+
}
93+
94+
}
95+
}
96+
});
97+
};

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "bit-docs-html-codepen-link",
3+
"version": "1.0.0",
4+
"description": "add a codepen link ",
5+
"main": "index.js",
6+
"scripts": {
7+
"preversion": "npm test",
8+
"test": "mocha test.js --reporter spec",
9+
"release:pre": "npm version prerelease && npm publish --tag=pre",
10+
"release:patch": "npm version patch && npm publish",
11+
"release:minor": "npm version minor && npm publish",
12+
"release:major": "npm version major && npm publish"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/bit-docs/bit-docs-html-codepen-link.git"
17+
},
18+
"keywords": [
19+
"bit-docs"
20+
],
21+
"author": "Bitovi",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/bit-docs/bit-docs-html-codepen-link/issues"
25+
},
26+
"homepage": "https://github.com/bit-docs/bit-docs-html-codepen-link#readme",
27+
"dependencies": {
28+
"bit-docs-prettify": "^0.2.2-8",
29+
"prismjs": "^1.11.0"
30+
},
31+
"devDependencies": {
32+
"bit-docs-generate-html": "^0.1.0",
33+
"connect": "^2.14.4",
34+
"mocha": "^2.5.3",
35+
"zombie": "^4.3.0"
36+
}
37+
}

tags.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
exports.codepen = {
3+
add: function(line, curTagData, scope, docMap, defaultWriteProp, options) {
4+
var html = "<div class='codepen'></div>";
5+
var useCurData = validCurData && (typeof curData.description === "string") && !curData.body;
6+
7+
// copies codepen options on to the docObject so they are accessible by the script
8+
if(options.codepen) {
9+
this.codepen = options.codepen;
10+
}
11+
12+
if(useCurData) {
13+
curData.description += html;
14+
} else {
15+
this.body += html;
16+
}
17+
}
18+
};

test-demo.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<style>
2+
.highlight {background-color: #90FEFB;}
3+
.expand {height: 20px; background-color: red;}
4+
.codepen:before {
5+
content: "Try it in your browser!"
6+
}
7+
</style>
8+
9+
## ESModule
10+
11+
```html
12+
<my-app></my-app>
13+
14+
<script type="module">
15+
import { Component } from "can";
16+
Component
17+
</script>
18+
```
19+
<div class='codepen'></div>
20+
21+
## Straight JS
22+
23+
```js
24+
import {DefineMap} from "can";
25+
console.log( myCounter.count ) //-> 1
26+
```
27+
<div class='codepen'></div>

test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
var assert = require("assert");
2+
var generate = require("bit-docs-generate-html/generate");
3+
var path = require("path");
4+
var fs = require("fs");
5+
6+
var Browser = require("zombie");
7+
var connect = require("connect");
8+
9+
var open = function(url, callback, done) {
10+
var server = connect().use(connect.static(path.join(__dirname, "temp"))).listen(8081);
11+
var browser = new Browser();
12+
browser.visit("http://localhost:8081/" + url)
13+
.then(function() {
14+
callback(browser, function() {
15+
server.close();
16+
});
17+
}).catch(function(e) {
18+
server.close();
19+
done(e);
20+
});
21+
};
22+
23+
describe("bit-docs-html-codepen-link", function() {
24+
it("basics works", function(done) {
25+
this.timeout(60000);
26+
27+
var docMap = Promise.resolve({
28+
index: {
29+
name: "index",
30+
demo: "path/to/demo.html",
31+
body: fs.readFileSync(__dirname+"/test-demo.md", "utf8"),
32+
codepen: [
33+
["can", "//unpkg.com/can@^5.0.0-pre.1/core.mjs"]
34+
]
35+
}
36+
});
37+
38+
generate(docMap, {
39+
html: {
40+
dependencies: {
41+
"bit-docs-html-codepen-link": __dirname
42+
}
43+
},
44+
dest: path.join(__dirname, "temp"),
45+
parent: "index",
46+
forceBuild: true,
47+
minifyBuild: false
48+
}).then(function() {
49+
open("index.html",function(browser, close) {
50+
var doc = browser.window.document;
51+
var createCallData = [];
52+
browser.window.CREATE_CODE_PEN = function(data){
53+
createCallData.push(data);
54+
};
55+
var codePens = doc.querySelectorAll('.codepen');
56+
57+
Array.from(codePens).forEach(function(codePen){
58+
codePen.click();
59+
});
60+
assert.deepEqual(createCallData,[
61+
{ html: '<my-app></my-app>',
62+
js: 'import { Component } from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nComponent',
63+
js_module: true,
64+
editors: '1011' },
65+
{ js: 'import {DefineMap} from "//unpkg.com/can@^5.0.0-pre.1/core.mjs";\nconsole.log( myCounter.count ) //-> 1',
66+
js_module: true,
67+
editors: '0011' }
68+
]);
69+
70+
close();
71+
done();
72+
}, done);
73+
}, done);
74+
});
75+
});

0 commit comments

Comments
 (0)