Skip to content

Commit 4dae876

Browse files
committed
add support inline config
1 parent d472733 commit 4dae876

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@ $ npm install gitbook-plugin-jsfiddle --save
1313
"plugins": [ "jsfiddle"],
1414
"pluginsConfig": {
1515
"jsfiddle":{
16+
"type":"script"
1617
"tabs":["result","js","css", "html"],
17-
"height":500,
18-
"widht":500
18+
"height": "500px",
19+
"widht": "500px",
20+
"fontColor": "green"
1921
}
2022
}
2123
}
2224
```
25+
Param `type` can be 'frame' or 'script'.
26+
Also you can use other params read more https://medium.com/jsfiddle-updates/new-jsfiddle-embeds-93ab7a51ee11#.vt34bxchv
27+
If you need override setting for certain fiddle,you can just add this param in hash
28+
`//jsfiddle.net/zalun/NmudS/#fontColor=green&type=frame`
2329
### 3. paste jsfiddle embedded code to you book something like
24-
`[source code](http://jsfiddle.net/zalun/NmudS/)`
30+
`[source code](//jsfiddle.net/zalun/NmudS/)`
2531

26-
will be rendered like [my book](http://api.taucharts.com/tutorials/1min.html) does
32+
will be rendered like [my book](https://api.taucharts.com/tutorials/1min.html) does
2733

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "gitbook-plugin-jsfiddle",
33
"description": "JSFiddle Integration into GitBook",
44
"main": "index.js",
5-
"version": "1.0.0-alpha.2",
5+
"version": "1.0.0-alpha.3",
66
"author": {
77
"name": "Konstantin Krivlenia"
88
},

src/replace.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,19 @@ var htmlToDom = function (html) {
2525
return contentDOM
2626
};
2727

28-
var exctractConfigFromURL = function (href) {
28+
29+
var extractConfigFromURL = function (href) {
30+
var match = /(#)(.+)$/ig.exec(href);
31+
if (match && match[2]) {
32+
return match[2].split('&').reduce(function (params, param) {
33+
var splitParam = param.split('=');
34+
if (splitParam[0] === 'tabs') {
35+
splitParam[1] = splitParam[1].split(',');
36+
}
37+
params[splitParam[0]] = splitParam[1];
38+
return params;
39+
}, {});
40+
}
2941
return {};
3042
};
3143
var generateAdditionalParams = function (config) {
@@ -55,9 +67,9 @@ var creator = {
5567
return [
5668
'<iframe',
5769
' width=',
58-
'"' + (config.width ? config.width + 'px' : '100%') + '"',
70+
'"' + (config.width ? config.width : '100%') + '"',
5971
' height=',
60-
'"' + (config.height ? config.height + 'px' : '100%') + '"',
72+
'"' + (config.height ? config.height : '300px') + '"',
6173
' src="' + generateUrl(config) + '"',
6274
' allowfullscreen="allowfullscreen" frameborder="0"',
6375
'>',
@@ -67,9 +79,10 @@ var creator = {
6779
};
6880

6981
var createEmbedNode = function (href, config) {
70-
var type = config.type || 'script';
71-
72-
return htmlToDom(creator[type](_.defaults({href: href}, config, defautsConfig)))[0];
82+
var normalURL = href.replace(/#.+$/, '');
83+
var configFromUrl = extractConfigFromURL(href);
84+
var mergedConfig = _.defaults({href: normalURL}, configFromUrl, config, defautsConfig);
85+
return htmlToDom(creator[mergedConfig.type](mergedConfig))[0];
7386
};
7487

7588
module.exports = function (rawHtml, config) {

tests/replace.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,21 @@ test('replacer', function (assert) {
3636
tabs: ['js', 'result']
3737
}),
3838
`<div>
39-
<iframe width="100%" height="100%" src="//jsfiddle.net/zalun/NmudS/embedded/js,result/dark/?bodyColor=red&accentColor=red" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
39+
<iframe width="100%" height="300px" src="//jsfiddle.net/zalun/NmudS/embedded/js,result/dark/?bodyColor=red&accentColor=red" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
4040
</div>`.replace(/\s+/g, ' ')
4141
, 'replace on frame with params and dark theme');
42+
assert.equal(
43+
replace(
44+
'<div><a href="https://jsfiddle.net/09bv780j/#tabs=result,css&width=500px&type=script&theme=dark&bodyColor=blue"></a></div>',
45+
{
46+
bodyColor:'red',
47+
type:'frame',
48+
accentColor: 'red',
49+
tabs:['js']
50+
}
51+
),
52+
'<div><script async src="https://jsfiddle.net/09bv780j/embed/result,css/dark/?bodyColor=blue&accentColor=red"></script></div>',
53+
'support inline config'
54+
);
4255
assert.end();
4356
});

0 commit comments

Comments
 (0)