{{/* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */}} {{/* Only include the copy button script on pages that have code blocks */}} {{ if (findRE "
(function() {
var copyIcon = '';
var checkIcon = '';
var errorIcon = '';
// Add copy buttons to all code blocks
// Hugo generates: ...
// Or without highlighting: ...
document.querySelectorAll('pre > code').forEach(function(codeBlock) {
var pre = codeBlock.parentNode;
// Determine the container (highlight div or pre's parent)
var container = pre.parentNode.classList.contains('highlight')
? pre.parentNode
: pre;
// Create wrapper div for positioning
var wrapper = document.createElement('div');
wrapper.className = 'code-block-wrapper';
container.parentNode.insertBefore(wrapper, container);
wrapper.appendChild(container);
// Create copy button
var button = document.createElement('button');
button.className = 'copy-code-button';
button.type = 'button';
button.setAttribute('aria-label', 'Copy code to clipboard');
button.innerHTML = copyIcon;
wrapper.appendChild(button);
button.addEventListener('click', function() {
// Get the code text
var text = codeBlock.innerText;
// Remove leading $ from each line (common in shell examples)
text = text.split('\n').map(function(line) {
return line.replace(/^\$ /, '');
}).join('\n').trim();
navigator.clipboard.writeText(text).then(function() {
// Chrome doesn't blur automatically, leaving button in focused state
button.blur();
// Show success feedback
button.innerHTML = checkIcon;
button.classList.add('copied');
setTimeout(function() {
button.innerHTML = copyIcon;
button.classList.remove('copied');
}, 2000);
}, function(error) {
// Show error feedback
button.blur();
button.innerHTML = errorIcon;
button.classList.add('error');
setTimeout(function() {
button.innerHTML = copyIcon;
button.classList.remove('error');
}, 2000);
});
});
});
})();
{{ end }}