From cf16dd3e43e110df3c013531bdf5e339c6f5f614 Mon Sep 17 00:00:00 2001 From: curben Date: Sat, 25 May 2019 19:46:35 +0930 Subject: [PATCH] refactor: move creation of 'Copy' button from client to server * https://github.com/cheeriojs/cheerio#append-content-content-- * cheerio doesn't have .createElement() function - https://code.tutsplus.com/tutorials/jquery-14-released-the-15-new-features-you-must-know--net-8590 --- themes/typing/scripts/copy-button.js | 14 ++++++++++++++ themes/typing/source/js/typing.js | 11 ----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 themes/typing/scripts/copy-button.js diff --git a/themes/typing/scripts/copy-button.js b/themes/typing/scripts/copy-button.js new file mode 100644 index 0000000..a31528f --- /dev/null +++ b/themes/typing/scripts/copy-button.js @@ -0,0 +1,14 @@ +// Add "Copy" button to code snippet + +const cheerio = require('cheerio') + +hexo.extend.filter.register('after_render:html', (str) => { + const $ = cheerio.load(str) + + // Avoid duplicate button + if ($('button').parent().hasClass('code')) return + + $('.code').append('') + + return $.html() +}) diff --git a/themes/typing/source/js/typing.js b/themes/typing/source/js/typing.js index 987e3ba..bca9696 100644 --- a/themes/typing/source/js/typing.js +++ b/themes/typing/source/js/typing.js @@ -6,17 +6,6 @@ searchClick.onclick = function() { '&group_id=&project_id=8306723&search_code=true&repository_ref=master') } -// Add "Copy" button to code snippet -const code = document.getElementsByClassName('code') - -Array.from(code).forEach((element) => { - const button = document.createElement('button') - button.className = 'copy-button' - button.textContent = 'Copy' - - element.appendChild(button) -}) - /* * Copy button and Cloudinary functions. * Following functions only execute after