Skip to content

Commit

Permalink
πŸ› fix no line number case
Browse files Browse the repository at this point in the history
  • Loading branch information
mickm3n committed Jul 28, 2024
1 parent 171d1ca commit ccbc91b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions static/js/copy-code-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ document.addEventListener('DOMContentLoaded', (event) => {
copyButton.innerText = 'Copy';

copyButton.addEventListener('click', () => {
// When there is no line number, need to extract the code from the whole table
if (!codeBlock.querySelector('table tr')) {
navigator.clipboard.writeText(codeBlock.innerText).then(
() => {
copyButton.innerText = 'Copied!';
setTimeout(() => {
copyButton.innerText = 'Copy';
}, 2000);
},
(error) => {
console.error('Failed to copy code: ', error);
}
);
return;
}

// Get all the table rows
const rows = codeBlock.querySelectorAll('table tr');
// Extract only the text content from each row, ignoring the line numbers
Expand Down

0 comments on commit ccbc91b

Please sign in to comment.