From 6e5903c8a91105bf91d87a4f8638d7efcc85659c Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Sun, 20 Dec 2020 17:25:13 -0700 Subject: [PATCH] predefine regular expressions in JavaScript functions --- src/js/06-copy-to-clipboard.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/js/06-copy-to-clipboard.js b/src/js/06-copy-to-clipboard.js index 155621d..726415d 100644 --- a/src/js/06-copy-to-clipboard.js +++ b/src/js/06-copy-to-clipboard.js @@ -1,5 +1,10 @@ ;(function () { 'use strict' + + var CMD_RX = /^\$ (\S[^\\\n]*(\\\n(?!\$ )[^\\\n]*)*)(?=\n|$)/gm + var LINE_CONTINUATION_RX = /( ) *\\\n *|\\\n( ?) */g + var TRAILING_SPACE_RX = / +$/gm + ;[].slice.call(document.querySelectorAll('.doc pre.highlight, .doc .literalblock pre')).forEach(function (pre) { var code, language, lang, copy, toast, toolbox if (pre.classList.contains('highlight')) { @@ -43,16 +48,14 @@ }) function extractCommands (text) { - var cmdRx = /^\$ (\S[^\\\n]*(\\\n(?!\$ )[^\\\n]*)*)(?=\n|$)/gm - var cleanupRx = /( ) *\\\n *|\\\n( ?) */g var cmds = [] var m - while ((m = cmdRx.exec(text))) cmds.push(m[1].replace(cleanupRx, '$1$2')) + while ((m = CMD_RX.exec(text))) cmds.push(m[1].replace(LINE_CONTINUATION_RX, '$1$2')) return cmds.join(' && ') } function writeToClipboard (code) { - var text = code.innerText.replace(/ *$/gm, '') + var text = code.innerText.replace(TRAILING_SPACE_RX, '') if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = extractCommands(text) window.navigator.clipboard.writeText(text).then( function () { -- 2.11.0