Skip to content

Commit 79dd78a

Browse files
committed
Allow to copy multiple commands and command with line continuation
1 parent 759f6a5 commit 79dd78a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/js/07-copy-to-clipboard.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
;(function () {
22
'use strict'
3+
var commandContinuationRx = /\\\s*$/
4+
35
;[].slice.call(document.querySelectorAll('.doc pre.highlight, .doc .literalblock pre')).forEach(function (pre) {
46
var code, language, lang, copy, toast, toolbox
57
if (pre.classList.contains('highlight')) {
@@ -51,7 +53,29 @@
5153

5254
function writeToClipboard (code) {
5355
var text = code.innerText
54-
if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = text.split('\n')[0].slice(2)
56+
if (code.dataset.lang === 'console' && text.startsWith('$ ')) {
57+
var lines = text.split('\n')
58+
var currentCommand = ''
59+
var commands = []
60+
var commandContinuationFound = false
61+
for (var i = 0; i < lines.length; i++) {
62+
var line = lines[i]
63+
if (!commandContinuationFound && !line.startsWith('$ ')) {
64+
// ignore, command output
65+
} else {
66+
if (commandContinuationFound) {
67+
currentCommand += '\n' + line
68+
} else if (line.startsWith('$ ')) {
69+
currentCommand = line.slice(2)
70+
}
71+
commandContinuationFound = line.match(commandContinuationRx)
72+
if (!commandContinuationFound) {
73+
commands.push(currentCommand)
74+
}
75+
}
76+
}
77+
text = commands.join('; ')
78+
}
5579
window.navigator.clipboard.writeText(text).then(
5680
function () {
5781
this.classList.add('clicked')

0 commit comments

Comments
 (0)