This commit is contained in:
author
2025-11-08 21:00:06 +08:00
parent 95a4182500
commit fc176f1b7d
1793 changed files with 2099698 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 David Moreno García
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+31
View File
@@ -0,0 +1,31 @@
# Code plugin for GitBook
Code blocks are cool but can be cooler. This plugin adds lines numbers for multi-line blocks and a copy button to easily copy the content of your block.
## Cool, can I see it working?
The next image shows a single line code block:
![single line](https://github.com/davidmogar/gitbook-plugin-code/blob/resources/images/single.png?raw=true)
When displaying code with multiple lines, line numbers will be added:
![multi line](https://github.com/davidmogar/gitbook-plugin-code/blob/resources/images/multi.png?raw=true)
## How can I use this plugin?
You only have to edit your book.json and modify it adding something like this:
```json
"plugins" : [ "code" ],
```
This will set up everything for you. If you want to get rid of the copy buttons use add this section too:
```json
"pluginsConfig": {
"code": {
"copyButtons": false
}
}
```
+37
View File
@@ -0,0 +1,37 @@
#code-textarea {
height: 0;
position: fixed;
top: -1000px;
width: 0;
}
.code-wrapper {
position: relative;
}
.code-wrapper i {
color: #c1c7cd;
cursor: pointer;
font-size: 12px;
font-weight: bold;
position: absolute;
right: 1em;
top: 1em;
}
.code-wrapper pre {
background: #f7f8f9;
border-radius: 3px;
counter-reset: line;
font-size: 15px;
}
.code-wrapper pre > code > span.code-line:before {
counter-increment: line;
color: #c1c7cd;
content: counter(line);
display: inline-block;
font-size: 12px;
margin-right: 1.5em;
width: 1em;
}
+91
View File
@@ -0,0 +1,91 @@
require(['gitbook', 'jQuery'], function(gitbook, $) {
const TERMINAL_HOOK = '**[terminal]'
var pluginConfig = {};
var timeouts = {};
function addCopyButton(wrapper) {
wrapper.append(
$('<i class="fa fa-clone t-copy"></i>')
.click(function() {
copyCommand($(this));
})
);
}
function addCopyTextarea() {
/* Add also the text area that will allow to copy */
$('body').append('<textarea id="code-textarea" />');
}
function copyCommand(button) {
pre = button.parent();
textarea = $('#code-textarea');
textarea.val(pre.text());
textarea.focus();
textarea.select();
document.execCommand('copy');
pre.focus();
updateCopyButton(button);
}
function initializePlugin(config) {
pluginConfig = config.code;
}
function format_code_block(block) {
/*
* Add line numbers for multiline blocks.
*/
code = block.children('code');
lines = code.html().split('\n');
if (lines[lines.length - 1] == '') {
lines.splice(-1, 1);
}
if (lines.length > 1) {
console.log(lines);
lines = lines.map(line => '<span class="code-line">' + line + '</span>');
console.log(lines);
code.html(lines.join('\n'));
}
// Add wrapper to pre element
wrapper = block.wrap('<div class="code-wrapper"></div>');
if (pluginConfig.copyButtons) {
addCopyButton(wrapper);
}
}
function updateCopyButton(button) {
id = button.attr('data-command');
button.removeClass('fa-clone').addClass('fa-check');
// Clear timeout
if (id in timeouts) {
clearTimeout(timeouts[id]);
}
timeouts[id] = window.setTimeout(function() {
button.removeClass('fa-check').addClass('fa-clone');
}, 1000);
}
gitbook.events.bind('start', function(e, config) {
initializePlugin(config);
if (pluginConfig.copyButtons) {
addCopyTextarea();
}
});
gitbook.events.bind('page.change', function() {
$('pre').each(function() {
format_code_block($(this));
});
});
});
+13
View File
@@ -0,0 +1,13 @@
module.exports = {
website: {
assets: './assets',
js: [
'plugin.js'
],
css: [
'plugin.css'
]
}
};
+63
View File
@@ -0,0 +1,63 @@
{
"_from": "gitbook-plugin-code",
"_id": "gitbook-plugin-code@0.1.0",
"_inBundle": false,
"_integrity": "sha512-TE8CUFlN7da4SiN2LYx8qk+FX5w2O2IicEcn48sPTpA8gj9qKQqpYpGEdwgsDEev11shEjCZhDwFN//GQ7bdnA==",
"_location": "/gitbook-plugin-code",
"_phantomChildren": {},
"_requested": {
"type": "tag",
"registry": true,
"raw": "gitbook-plugin-code",
"name": "gitbook-plugin-code",
"escapedName": "gitbook-plugin-code",
"rawSpec": "",
"saveSpec": null,
"fetchSpec": "latest"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "https://registry.npmmirror.com/gitbook-plugin-code/-/gitbook-plugin-code-0.1.0.tgz",
"_shasum": "811a9600baef7558ae193cee7d5810001667e97b",
"_spec": "gitbook-plugin-code",
"_where": "D:\\Book",
"author": {
"name": "David Moreno-García",
"email": "david.mogar@gmail.com"
},
"bugs": {
"url": "https://github.com/davidmogar/gitbook-plugin-code/issues"
},
"bundleDependencies": false,
"deprecated": false,
"description": "Gitbook plugin to change style of code blocks",
"engines": {
"gitbook": ">2.5.0"
},
"gitbook": {
"properties": {
"copyButtons": {
"default": true,
"title": "Adds buttons to copy the commands",
"type": "boolean"
}
}
},
"homepage": "https://github.com/davidmogar/gitbook-plugin-code",
"keywords": [
"gitbook",
"github",
"code",
"plugin"
],
"license": "MIT",
"main": "index.js",
"name": "gitbook-plugin-code",
"repository": {
"type": "git",
"url": "git+https://github.com/davidmogar/gitbook-plugin-code.git"
},
"version": "0.1.0"
}