Skip to content
This repository was archived by the owner on Nov 24, 2020. It is now read-only.

Commit b4b6527

Browse files
committed
Initial commit
0 parents  commit b4b6527

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed

.gitignore

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
# Deployed apps should consider commenting this line out:
24+
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
25+
node_modules
26+
27+
# vim swapfile
28+
*.swp

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# codetabs
2+
3+
Include multiple languages code block to your GitBook (for example when documenting an API).
4+
5+
### Installation
6+
7+
Adds the plugin to your `book.json`, then run `gitbook install` if you are building your book locally.
8+
9+
```js
10+
{
11+
"plugins": ["codetabs"]
12+
}
13+
```
14+
15+
### Usage
16+
17+
```md
18+
19+
This is a code block with tabs for each languages:
20+
21+
{% codetabs name="Python", type="py" -%}
22+
msg = "Hello World"
23+
print msg
24+
{%- language name="JavaScript", type="js" -%}
25+
var msg = "Hello World";
26+
console.log(msg);
27+
{%- language name="HTML", type="html" -%}
28+
<b>Hello World</b>
29+
{%- endcodetabs %}
30+
```
31+
32+
### Escaping templating syntax
33+
34+
For languages using syntax like `{{`, `{%`; we have to escape these content:
35+
36+
37+
```md
38+
Here is some angular and react code
39+
40+
{% codetabs name="Python", type="py" -%}
41+
{% raw %}
42+
<h1>Hello {{yourName}}!</h1>
43+
{% endraw %}
44+
{%- language name="React", type="js" -%}
45+
var React = require('react')
46+
{%- endcodetabs %}
47+
```
48+
49+
50+

assets/codetabs.css

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.codetabs {
2+
3+
}
4+
5+
.codetabs .codetabs-header .tab {
6+
display: inline-block;
7+
padding: 3px 6px;
8+
border-bottom: 2px solid transparent;
9+
cursor: pointer;
10+
}
11+
12+
.codetabs .codetabs-header .tab.active {
13+
border-bottom-color: #428bca;
14+
cursor: default;
15+
}
16+
17+
.codetabs .codetabs-body .tab {
18+
display: none;
19+
}
20+
21+
.codetabs .codetabs-body .tab.active {
22+
display: block;
23+
}
24+

assets/codetabs.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require([
2+
'jquery'
3+
], function($) {
4+
$(document).on('click.plugin.codetabs', '.codetabs .codetabs-header .tab', function(e) {
5+
var $btn = $(e.target);
6+
var $tabs = $btn.parents('.codetabs');
7+
var tabId = $btn.data('codetab');
8+
9+
$tabs.find('.tab').removeClass('active');
10+
$tabs.find('.tab[data-codetab="' + tabId + '"]').addClass('active');
11+
});
12+
});

index.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var escape = require('escape-html');
2+
3+
/*
4+
Generate HTML for the tab in the header
5+
6+
@param {Block}
7+
@param {Boolean}
8+
@return {String}
9+
*/
10+
function createTab(block, i, isActive) {
11+
return '<div class="tab' + (isActive? ' active' : '') + '" data-codetab="' + i + '">' + block.kwargs.name + '</div>';
12+
}
13+
14+
/*
15+
Generate HTML for the tab's content
16+
17+
@param {Block}
18+
@param {Boolean}
19+
@return {String}
20+
*/
21+
function createTabBody(block, i, isActive) {
22+
return '<div class="tab' + (isActive? ' active' : '') + '" data-codetab="' + i + '"><pre><code class="lang-' + (block.kwargs.type || block.kwargs.name) + '">'
23+
+ escape(block.body) +
24+
'</code></pre></div>';
25+
}
26+
27+
module.exports = {
28+
book: {
29+
assets: './assets',
30+
css: [
31+
'codetabs.css'
32+
],
33+
js: [
34+
'codetabs.js'
35+
]
36+
},
37+
38+
blocks: {
39+
codetabs: {
40+
blocks: ['language'],
41+
process: function(parentBlock) {
42+
console.log(JSON.stringify(parentBlock, null, 4));
43+
var blocks = [parentBlock].concat(parentBlock.blocks);
44+
var tabsContent = '';
45+
var tabsHeader = '';
46+
47+
blocks.forEach(function(block, i) {
48+
var isActive = (i == 0);
49+
50+
if (!block.kwargs.name) {
51+
throw new Error('Code tab requires a "name" property');
52+
}
53+
54+
tabsHeader += createTab(block, i, isActive);
55+
tabsContent += createTabBody(block, i, isActive);
56+
});
57+
58+
59+
return '<div class="codetabs">' +
60+
'<div class="codetabs-header">' + tabsHeader + '</div>' +
61+
'<div class="codetabs-body">' + tabsContent + '</div>' +
62+
'</div>';
63+
}
64+
}
65+
}
66+
};

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "gitbook-plugin-codetabs",
3+
"version": "0.0.0",
4+
"description": "Multiple languages code blocks for GitBook",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/GitbookIO/plugin-codetabs.git"
12+
},
13+
"keywords": [
14+
"gitbook",
15+
"code"
16+
],
17+
"engines": {
18+
"gitbook": ">=3.0.0-pre.8"
19+
},
20+
"author": "Samy Pesse <[email protected]>",
21+
"license": "Apache-2.0",
22+
"bugs": {
23+
"url": "https://github.com/GitbookIO/plugin-codetabs/issues"
24+
},
25+
"homepage": "https://github.com/GitbookIO/plugin-codetabs#readme",
26+
"dependencies": {
27+
"escape-html": "^1.0.3"
28+
}
29+
}

0 commit comments

Comments
 (0)