|
1 | 1 | ;(function () {
|
2 | 2 | 'use strict'
|
3 |
| - var commandContinuationRx = /\\\s*$/ |
4 |
| - |
5 | 3 | ;[].slice.call(document.querySelectorAll('.doc pre.highlight, .doc .literalblock pre')).forEach(function (pre) {
|
6 | 4 | var code, language, lang, copy, toast, toolbox
|
7 | 5 | if (pre.classList.contains('highlight')) {
|
|
51 | 49 | copy.addEventListener('click', writeToClipboard.bind(copy, code))
|
52 | 50 | })
|
53 | 51 |
|
| 52 | + function extractCommands (text) { |
| 53 | + var cmdRx = /^\$ (\S[^\\\n]*(\\\n(?!\$ )[^\\\n]*)*)(?=\n|$)/gm |
| 54 | + var cleanupRx = /( )? *\\\n */g |
| 55 | + var cmds = [] |
| 56 | + var m |
| 57 | + while ((m = cmdRx.exec(text))) cmds.push(m[1].replace(cleanupRx, '$1')) |
| 58 | + return cmds.join(' && ') |
| 59 | + } |
| 60 | + |
54 | 61 | function writeToClipboard (code) {
|
55 | 62 | var text = code.innerText
|
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 |
| - } |
| 63 | + if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = extractCommands(text) |
79 | 64 | window.navigator.clipboard.writeText(text).then(
|
80 | 65 | function () {
|
81 | 66 | this.classList.add('clicked')
|
|
0 commit comments