Skip to content

Commit 2b608d7

Browse files
author
konstantin
committed
embed jsfiddle to gitbook
1 parent 9b3dee6 commit 2b608d7

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed

Diff for: .gitignore

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### JetBrains template
3+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
4+
5+
*.iml
6+
7+
## Directory-based project format:
8+
.idea/
9+
# if you remove the above rule, at least ignore the following:
10+
11+
# User-specific stuff:
12+
# .idea/workspace.xml
13+
# .idea/tasks.xml
14+
# .idea/dictionaries
15+
16+
# Sensitive or high-churn files:
17+
# .idea/dataSources.ids
18+
# .idea/dataSources.xml
19+
# .idea/sqlDataSources.xml
20+
# .idea/dynamic.xml
21+
# .idea/uiDesigner.xml
22+
23+
# Gradle:
24+
# .idea/gradle.xml
25+
# .idea/libraries
26+
27+
# Mongo Explorer plugin:
28+
# .idea/mongoSettings.xml
29+
30+
## File-based project format:
31+
*.ipr
32+
*.iws
33+
34+
## Plugin-specific files:
35+
36+
# IntelliJ
37+
out/
38+
39+
# mpeltonen/sbt-idea plugin
40+
.idea_modules/
41+
42+
# JIRA plugin
43+
atlassian-ide-plugin.xml
44+
45+
# Crashlytics plugin (for Android Studio and IntelliJ)
46+
com_crashlytics_export_strings.xml
47+
crashlytics.properties
48+
crashlytics-build.properties
49+
50+

Diff for: README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
JSFiddle integration for GitBook
2+
==============
3+
4+
### 1. You can use install it via **NPM** and save it to package.json:
5+
```
6+
$ npm install gitbook-plugin-jsfiddle --save
7+
```
8+
### 2. add the plugin to `book.json` config
9+
```
10+
{
11+
"plugins": [ "jsfiddle"]
12+
}
13+
```
14+
### 3. paste jsfiddle embedded code to you book something like
15+
`[source code](http://jsfiddle.net/zalun/NmudS/embedded/result,js,html,css/)`
16+
17+
will be rendered like [my book](http://api.taucharts.com/tutorials/1min.html) does
18+

Diff for: book/plugin.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
require(["gitbook", "jquery"], function (gitbook, $) {
2+
var matcher = /(http|https):\/\/jsfiddle.net\/.+/;
3+
4+
function getQuery(querystring) {
5+
var query = {};
6+
7+
var pairs = querystring.split('&'),
8+
length = pairs.length,
9+
keyval = [],
10+
i = 0;
11+
12+
for (; i < length; i++) {
13+
keyval = pairs[i].split('=', 2);
14+
try {
15+
keyval[0] = decodeURIComponent(keyval[0]); // key
16+
keyval[1] = decodeURIComponent(keyval[1]); // value
17+
} catch (e) {
18+
}
19+
20+
if (query[keyval[0]] === undefined) {
21+
query[keyval[0]] = keyval[1];
22+
} else {
23+
query[keyval[0]] += ',' + keyval[1];
24+
}
25+
}
26+
27+
return query;
28+
}
29+
30+
function embed(link) {
31+
var iframe = document.createElement('iframe'),
32+
url = link.href;
33+
iframe.src = url;
34+
iframe._src = url; // support for google slide embed
35+
iframe.className = link.className; // inherit all the classes from the link
36+
iframe.id = link.id; // also inherit, giving more style control to the user
37+
iframe.style.border = '1px solid #aaa';
38+
39+
var query = getQuery(link.search);
40+
iframe.style.width = query.width || '100%';
41+
iframe.style.minHeight = query.height || '300px';
42+
if (query.height) {
43+
iframe.style.maxHeight = query.height;
44+
}
45+
link.parentNode.replaceChild(iframe, link);
46+
47+
var onmessage = function (event) {
48+
event || (event = window.event);
49+
// * 1 to coerse to number, and + 2 to compensate for border
50+
iframe.style.height = (event.data.height * 1 + 2) + 'px';
51+
};
52+
53+
if (window.addEventListener) {
54+
window.addEventListener('message', onmessage, false);
55+
} else {
56+
window.attachEvent('onmessage', onmessage);
57+
}
58+
}
59+
60+
function embedAllLink() {
61+
$(".book-body a").each(function (index, link) {
62+
if (link.href && matcher.test(link.href)) {
63+
embed(link);
64+
}
65+
});
66+
}
67+
68+
gitbook.events.bind("start", function (e, config) {
69+
embedAllLink();
70+
});
71+
72+
gitbook.events.bind("page.change", function () {
73+
embedAllLink();
74+
});
75+
});

Diff for: index.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
book: {
3+
assets: "./book",
4+
js: [
5+
"plugin.js"
6+
]
7+
}
8+
};

Diff for: package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "gitbook-plugin-jsfiddle",
3+
"description": "JSFiddle Integration into GitBook",
4+
"main": "index.js",
5+
"version": "0.0.1",
6+
"author": {
7+
"name": "Konstantin Krivlenia"
8+
},
9+
"keywords": [
10+
"gitbook",
11+
"jsfiddle"
12+
],
13+
"engines": {
14+
"gitbook": "*"
15+
},
16+
"homepage": "https://github.com/Mavrin/gitbook-plugin-jsfiddle",
17+
"repository": {
18+
"type": "git",
19+
"url": "https://github.com/Mavrin/gitbook-plugin-jsfiddle.git"
20+
},
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/Mavrin/gitbook-plugin-jsfiddle/issues"
24+
}
25+
}

0 commit comments

Comments
 (0)