From 053c4d76f8b4c7fc1a0d1afa7112cdbf0d6b59bd Mon Sep 17 00:00:00 2001 From: Mansoor Barri Date: Tue, 3 Sep 2024 00:50:33 +0100 Subject: [PATCH] fix: copying line numbers and empty spaces --- static/js/main.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/static/js/main.js b/static/js/main.js index b27341fa..0129c5c2 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -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; @@ -213,4 +226,3 @@ document.addEventListener('DOMContentLoaded', () => { }); }); }); -