Skip to content

Commit

Permalink
fix: copying line numbers and empty spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mansoorbarri committed Sep 2, 2024
1 parent 320b1ad commit 053c4d7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,20 @@ document.addEventListener('DOMContentLoaded', () => {
if (!codeBlock) return;

copyButton.addEventListener('click', () => {
navigator.clipboard.writeText(codeBlock.textContent)
// Create a deep clone of the code block
const codeBlockClone = codeBlock.cloneNode(true);

// Remove line number elements from the clone
const lineNumbers = codeBlockClone.querySelectorAll('.ln');
lineNumbers.forEach(ln => ln.remove());

// Get the text content, splitting by lines, trimming each line, and joining back
const codeText = codeBlockClone.textContent
.split('\n') // Split into lines
.map(line => line.trim()) // Trim each line
.join('\n'); // Join lines back with newline

navigator.clipboard.writeText(codeText)
.then(() => {
copyButton.textContent = copiedText;

Expand All @@ -213,4 +226,3 @@ document.addEventListener('DOMContentLoaded', () => {
});
});
});

0 comments on commit 053c4d7

Please sign in to comment.