diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index 9f49b00c..434b6e75 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -181,6 +181,7 @@ The following configuration options are available: Available configuration options for the `[output.html.playpen]` table: - **editable:** Allow editing the source code. Defaults to `false`. +- **copyable:** Display the copy button on code snippets. Defaults to `true`. - **copy-js:** Copy JavaScript files for the editor to the output directory. Defaults to `true`. - **line-numbers** Display line numbers on editable sections of code. Requires both `editable` and `copy-js` to be `true`. Defaults to `false`. diff --git a/src/config.rs b/src/config.rs index 202fe079..e93283c7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -492,10 +492,12 @@ impl HtmlConfig { pub struct Playpen { /// Should playpen snippets be editable? Default: `false`. pub editable: bool, + /// Display the copy button. Default: `true`. + pub copyable: bool, /// Copy JavaScript files for the editor to the output directory? /// Default: `true`. pub copy_js: bool, - /// Display line numbers on playpen snippets + /// Display line numbers on playpen snippets. Default: `false`. pub line_numbers: bool, } @@ -503,6 +505,7 @@ impl Default for Playpen { fn default() -> Playpen { Playpen { editable: false, + copyable: true, copy_js: true, line_numbers: false, } @@ -638,6 +641,7 @@ mod tests { }; let playpen_should_be = Playpen { editable: true, + copyable: true, copy_js: true, line_numbers: false, }; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index d45ccb6a..2ac04891 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -456,6 +456,9 @@ fn make_data( data.insert("playpen_line_numbers".to_owned(), json!(true)); } } + if html_config.playpen.copyable { + data.insert("playpen_copyable".to_owned(), json!(true)); + } let search = html_config.search.clone(); if cfg!(feature = "search") { diff --git a/src/theme/book.js b/src/theme/book.js index 2102a307..2cfa4821 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -202,25 +202,27 @@ function playpen_text(playpen) { }); }); - Array.from(document.querySelectorAll('pre code')).forEach(function (block) { - var pre_block = block.parentNode; - if (!pre_block.classList.contains('playpen')) { - var buttons = pre_block.querySelector(".buttons"); - if (!buttons) { - buttons = document.createElement('div'); - buttons.className = 'buttons'; - pre_block.insertBefore(buttons, pre_block.firstChild); + if (window.playpen_copyable) { + Array.from(document.querySelectorAll('pre code')).forEach(function (block) { + var pre_block = block.parentNode; + if (!pre_block.classList.contains('playpen')) { + var buttons = pre_block.querySelector(".buttons"); + if (!buttons) { + buttons = document.createElement('div'); + buttons.className = 'buttons'; + pre_block.insertBefore(buttons, pre_block.firstChild); + } + + var clipButton = document.createElement('button'); + clipButton.className = 'fa fa-copy clip-button'; + clipButton.title = 'Copy to clipboard'; + clipButton.setAttribute('aria-label', clipButton.title); + clipButton.innerHTML = ''; + + buttons.insertBefore(clipButton, buttons.firstChild); } - - var clipButton = document.createElement('button'); - clipButton.className = 'fa fa-copy clip-button'; - clipButton.title = 'Copy to clipboard'; - clipButton.setAttribute('aria-label', clipButton.title); - clipButton.innerHTML = ''; - - buttons.insertBefore(clipButton, buttons.firstChild); - } - }); + }); + } // Process playpen code blocks Array.from(document.querySelectorAll(".playpen")).forEach(function (pre_block) { @@ -238,19 +240,21 @@ function playpen_text(playpen) { runCodeButton.title = 'Run this code'; runCodeButton.setAttribute('aria-label', runCodeButton.title); - var copyCodeClipboardButton = document.createElement('button'); - copyCodeClipboardButton.className = 'fa fa-copy clip-button'; - copyCodeClipboardButton.innerHTML = ''; - copyCodeClipboardButton.title = 'Copy to clipboard'; - copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title); - buttons.insertBefore(runCodeButton, buttons.firstChild); - buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild); - runCodeButton.addEventListener('click', function (e) { run_rust_code(pre_block); }); + if (window.playpen_copyable) { + var copyCodeClipboardButton = document.createElement('button'); + copyCodeClipboardButton.className = 'fa fa-copy clip-button'; + copyCodeClipboardButton.innerHTML = ''; + copyCodeClipboardButton.title = 'Copy to clipboard'; + copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title); + + buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild); + } + let code_block = pre_block.querySelector("code"); if (window.ace && code_block.classList.contains("editable")) { var undoChangesButton = document.createElement('button'); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index abb5c837..99ae32ab 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -235,6 +235,12 @@ window.playpen_line_numbers = true; {{/if}} + + {{#if playpen_copyable}} + + {{/if}} {{#if playpen_js}}