Skip to content
Open
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
1 change: 1 addition & 0 deletions hugo.site.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ custom_js = [
"sidebar.js",
"theme.js",
"tags.js",
"copy-handler.js",
]

[params.variant_weights]
Expand Down
81 changes: 81 additions & 0 deletions static/js/copy-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
document.addEventListener('DOMContentLoaded', function() {
// Custom copy handler for terminal examples
function setupCopyHandler() {
// Listen for copy events on all sl-copy-button elements
document.addEventListener('sl-copy', function(event) {
const copyButton = event.target;

// Get the target element that the copy button is copying from
const fromAttr = copyButton.getAttribute('from');
if (!fromAttr) return;

const targetElement = document.getElementById(fromAttr);
if (!targetElement) return;

// Get the text content
let textToCopy = targetElement.textContent || targetElement.innerText || '';

// Check if this looks like a terminal/shell code block
// Look for lines that start with $ followed by a space
const lines = textToCopy.split('\n');
const modifiedLines = lines.map(line => {
// If line starts with $ followed by whitespace, strip it
if (/^\$\s/.test(line)) {
return line.replace(/^\$\s+/, '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not work for this code:

$ aws eks describe-cluster \
    --name my-cluster

}
return line;
});

// Check if any lines were modified (meaning this was a terminal example)
const wasModified = lines.some((line, index) => line !== modifiedLines[index]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of trying to guess from the content, look at the code block for the language attribute:

<code class="language-yaml" data-lang="yaml">

Then you can replace with confidence.


if (wasModified) {
// Prevent the default copy behavior
event.preventDefault();

// Copy the modified text to clipboard
const modifiedText = modifiedLines.join('\n');

if (navigator.clipboard && navigator.clipboard.writeText) {
// Modern browsers
navigator.clipboard.writeText(modifiedText).then(() => {
// Trigger success feedback if available
if (copyButton.success) {
copyButton.success();
}
}).catch(err => {
console.error('Failed to copy text: ', err);
// Fallback to default behavior
fallbackCopy(modifiedText);
});
} else {
// Fallback for older browsers
fallbackCopy(modifiedText);
}
}
});
}

// Fallback copy method for older browsers
function fallbackCopy(text) {
const textarea = document.createElement('textarea');
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.left = '-999999px';
textarea.style.top = '-999999px';
document.body.appendChild(textarea);
textarea.focus();
textarea.select();

try {
document.execCommand('copy');
} catch (err) {
console.error('Fallback copy failed: ', err);
}

document.body.removeChild(textarea);
}

// Initialize the copy handler
setupCopyHandler();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels unnecessary to wrap the setup copy handler. Why not remove it?

});