1require(['gitbook', 'jQuery'], function(gitbook, $) { 2 3 const TERMINAL_HOOK = '**[terminal]' 4 5 var pluginConfig = {}; 6 var timeouts = {}; 7 8 function addCopyButton(wrapper) { 9 wrapper.append( 10 $('<i class="fa fa-clone t-copy"></i>') 11 .click(function() { 12 copyCommand($(this)); 13 }) 14 ); 15 } 16 17 function addCopyTextarea() { 18 19 /* Add also the text area that will allow to copy */ 20 $('body').append('<textarea id="code-textarea" />'); 21 } 22 23 function copyCommand(button) { 24 pre = button.parent(); 25 textarea = $('#code-textarea'); 26 textarea.val(pre.text()); 27 textarea.focus(); 28 textarea.select(); 29 document.execCommand('copy'); 30 pre.focus(); 31 updateCopyButton(button); 32 } 33 34 function initializePlugin(config) { 35 pluginConfig = config.code; 36 } 37 38 function format_code_block(block) { 39 /* 40 * Add line numbers for multiline blocks. 41 */ 42 code = block.children('code'); 43 lines = code.html().split('\n'); 44 45 if (lines[lines.length - 1] == '') { 46 lines.splice(-1, 1); 47 } 48 49 if (lines.length > 1) { 50 console.log(lines); 51 lines = lines.map(line => '<span class="code-line">' + line + '</span>'); 52 console.log(lines); 53 code.html(lines.join('\n')); 54 } 55 56 // Add wrapper to pre element 57 wrapper = block.wrap('<div class="code-wrapper"></div>'); 58 59 if (pluginConfig.copyButtons) { 60 addCopyButton(wrapper); 61 } 62 } 63 64 function updateCopyButton(button) { 65 id = button.attr('data-command'); 66 button.removeClass('fa-clone').addClass('fa-check'); 67 68 // Clear timeout 69 if (id in timeouts) { 70 clearTimeout(timeouts[id]); 71 } 72 timeouts[id] = window.setTimeout(function() { 73 button.removeClass('fa-check').addClass('fa-clone'); 74 }, 1000); 75 } 76 77 gitbook.events.bind('start', function(e, config) { 78 initializePlugin(config); 79 80 if (pluginConfig.copyButtons) { 81 addCopyTextarea(); 82 } 83 }); 84 85 gitbook.events.bind('page.change', function() { 86 $('pre').each(function() { 87 format_code_block($(this)); 88 }); 89 }); 90 91});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.