Skip to content

Commit d472733

Browse files
committed
add support frame
1 parent dada839 commit d472733

File tree

4 files changed

+59
-102
lines changed

4 files changed

+59
-102
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- "stable"

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Build Status](https://travis-ci.org/Mavrin/gitbook-plugin-jsfiddle.svg)](https://travis-ci.org/Mavrin/gitbook-plugin-jsfiddle)
2+
13
JSFiddle integration for GitBook
24
==============
35

src/replace.js

Lines changed: 20 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,3 @@
1-
/*require(["gitbook", "jquery"], function (gitbook, $) {
2-
var matcher;
3-
var localConfig = {jsfiddle:{}};
4-
5-
function getQuery(querystring) {
6-
var query = {};
7-
8-
var pairs = querystring.split('&'),
9-
length = pairs.length,
10-
keyval = [],
11-
i = 0;
12-
13-
for (; i < length; i++) {
14-
keyval = pairs[i].split('=', 2);
15-
try {
16-
keyval[0] = decodeURIComponent(keyval[0]); // key
17-
keyval[1] = decodeURIComponent(keyval[1]); // value
18-
} catch (e) {
19-
}
20-
21-
if (query[keyval[0]] === undefined) {
22-
query[keyval[0]] = keyval[1];
23-
} else {
24-
query[keyval[0]] += ',' + keyval[1];
25-
}
26-
}
27-
28-
return query;
29-
}
30-
31-
// <iframe width="100%" height="300" src="http://jsfiddle.net/taucharts/hmvwg1mn/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
32-
function embed(link, config) {
33-
var iframe = document.createElement('iframe'),
34-
url = link.href.replace(/\?.+/, '') + 'embedded/' + config.tabs.join(',') + '/';
35-
36-
iframe.src = url;
37-
var $frame = $(iframe);
38-
$frame.attr('allowfullscreen', 'allowfullscreen');
39-
$frame.attr('frameborder', 0);
40-
iframe._src = url; // support for google slide embed
41-
iframe.className = link.className; // inherit all the classes from the link
42-
iframe.id = link.id; // also inherit, giving more style control to the user
43-
iframe.style.border = '1px solid #aaa';
44-
45-
var query = getQuery(link.search);
46-
var widht = query.width || config.width || '100%';
47-
var height = query.height || config.height || '100%';
48-
$frame.attr('width', widht);
49-
$frame.attr('height', height);
50-
link.parentNode.replaceChild(iframe, link);
51-
52-
var onmessage = function (event) {
53-
event || (event = window.event);
54-
// * 1 to coerse to number, and + 2 to compensate for border
55-
iframe.style.height = (event.data.height * 1 + 2) + 'px';
56-
};
57-
58-
if (window.addEventListener) {
59-
window.addEventListener('message', onmessage, false);
60-
} else {
61-
window.attachEvent('onmessage', onmessage);
62-
}
63-
}
64-
65-
function embedAllLink(config) {
66-
localConfig.jsfiddle = config.jsfiddle || {};
67-
localConfig.tabs = config.tabs || ['result'];
68-
$(".book-body a").each(function (index, link) {
69-
if (link.href && matcher.test(link.href)) {
70-
embed(link, localConfig.jsfiddle);
71-
}
72-
});
73-
}
74-
75-
gitbook.events.bind("start", function (e, config) {
76-
matcher = /(http|https):\/\/jsfiddle.net\/.+/;
77-
embedAllLink(config);
78-
});
79-
80-
gitbook.events.bind("page.change", function () {
81-
if (matcher) {
82-
embedAllLink(localConfig);
83-
}
84-
});
85-
});*/
861
var htmlparser = require('htmlparser2');
872
var DomHandler = require('domhandler');
883
var domutils = require('domutils');
@@ -118,20 +33,36 @@ var generateAdditionalParams = function (config) {
11833
if (config.theme) {
11934
params += config.theme + '/';
12035
}
121-
var colors = _.chain(config).omit('href', 'type', 'theme', 'tabs').reduce(function (colors, value, color) {
36+
var colors = _.chain(config).omit('href', 'type', 'theme', 'tabs', 'width', 'height').reduce(function (colors, value, color) {
12237
colors += color + '=' + value + '&';
12338
return colors;
12439
}, '');
40+
colors = colors.replace(/&$/, '');
12541
return params + '?' + colors;
12642
};
12743

44+
var generateUrl = function (config) {
45+
var additionalParam = generateAdditionalParams(config);
46+
var type = config.type == 'frame' ? 'embedded' : 'embed';
47+
return config.href + type + '/' + config.tabs.join(',') + additionalParam;
48+
};
49+
12850
var creator = {
12951
script: function (config) {
130-
var additionalParam = generateAdditionalParams(config);
131-
return '<script async src="' + config.href + 'embed/' + config.tabs.join(',') + additionalParam + '" ></script>';
52+
return '<script async src="' + generateUrl(config) + '" ></script>';
13253
},
13354
frame: function (config) {
134-
55+
return [
56+
'<iframe',
57+
' width=',
58+
'"' + (config.width ? config.width + 'px' : '100%') + '"',
59+
' height=',
60+
'"' + (config.height ? config.height + 'px' : '100%') + '"',
61+
' src="' + generateUrl(config) + '"',
62+
' allowfullscreen="allowfullscreen" frameborder="0"',
63+
'>',
64+
'</iframe>'
65+
].join('');
13566
}
13667
};
13768

tests/replace.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
var test = require('tape');
22
var replace = require('./../src/replace');
3-
/*
4-
<script
5-
async
6-
src="//jsfiddle.net/zalun/NmudS/embed/?fontColor=red&bodyColor=red&accentColor=red&menuColor=red"
7-
></script>*/
8-
test('scanner', function (assert) {
3+
4+
test('replacer', function (assert) {
95
var str = '<div> <a href="//jsfiddle.net/zalun/NmudS/">Yellow</a> </div>';
106

117
assert.equal(
128
replace(str, {
13-
type:'frame',
14-
theme:"dark",
15-
bodyColor:"red",
16-
tabs:['js']
9+
type: 'script',
10+
theme: "dark",
11+
bodyColor: "red",
12+
accentColor: "red",
13+
tabs: ['js']
14+
}),
15+
`<div>
16+
<script async src="//jsfiddle.net/zalun/NmudS/embed/js/dark/?bodyColor=red&accentColor=red"></script>
17+
</div>`.replace(/\s+/g, ' ')
18+
, 'replace on script with params and dark theme');
19+
assert.equal(
20+
replace(str, {
21+
type: 'script',
22+
bodyColor: "red",
23+
accentColor: "red",
24+
tabs: ['js']
25+
}),
26+
`<div>
27+
<script async src="//jsfiddle.net/zalun/NmudS/embed/js/light/?bodyColor=red&accentColor=red"></script>
28+
</div>`.replace(/\s+/g, ' ')
29+
, 'replace on script with params and without theme');
30+
assert.equal(
31+
replace(str, {
32+
type: 'frame',
33+
theme: "dark",
34+
bodyColor: "red",
35+
accentColor: "red",
36+
tabs: ['js', 'result']
1737
}),
1838
`<div>
19-
<script async src="//jsfiddle.net/zalun/NmudS/embed/js,css,html,result/dark/"></script>
20-
</div>`.replace(/\s+/g,' ')
21-
, 'replaced');
39+
<iframe width="100%" height="100%" src="//jsfiddle.net/zalun/NmudS/embedded/js,result/dark/?bodyColor=red&accentColor=red" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
40+
</div>`.replace(/\s+/g, ' ')
41+
, 'replace on frame with params and dark theme');
2242
assert.end();
2343
});

0 commit comments

Comments
 (0)