Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion layouts/partials/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@
// Handle copy logic
button.addEventListener('click', () => {
const lines = block.querySelectorAll('span.cl');
const text = Array.from(lines).map(line => line.textContent).join('');
// Single-line blocks are almost always one command preceded by a
// prompt ("$ ", "> ", "127.0.0.1:6379> "). Strip that prompt so the
// copied text runs as-is. Multiline blocks mix commands and output,
// so leave them untouched.
const text = lines.length === 1
? lines[0].textContent.replace(/^(\s*)(?:\$\s+|>\s+|127\.0\.0\.1:6379>\s+)/, '$1')
: Array.from(lines).map(line => line.textContent).join('');

navigator.clipboard.writeText(text).then(() => {
tooltip.style.display = 'block';
Expand Down
8 changes: 8 additions & 0 deletions static/js/codetabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ function copyCodeToClipboardForCodetabs(button) {
}
}

// Single-line blocks are almost always one command preceded by a prompt
// ("$ ", "> ", "127.0.0.1:6379> "). Strip that prompt so the copied text
// runs as-is. Multiline blocks mix commands and output, so leave them untouched.
const codeLines = code.split('\n').filter(line => line !== '');
if (codeLines.length === 1) {
code = codeLines[0].replace(/^(\s*)(?:\$\s+|>\s+|127\.0\.0\.1:6379>\s+)/, '$1');
}

navigator.clipboard.writeText(code);

// Toggle tooltip
Expand Down
Loading