Fix the bug where clicking the copy button copies line numbers along with the code when line numbers are enabled in code blocks

This commit is contained in:
underway 2025-02-12 00:34:08 +08:00
parent 9e6b7b22a9
commit a739470364

View File

@ -75,8 +75,18 @@ let Stack = {
const codeBlock = highlight.querySelector('code[data-lang]');
if (!codeBlock) return;
const removeLineNumbers = (text: string): string => {
return text.split('\n')
.map(line => {
const lineNumberMatch = line.match(/^\s*\d+[\s.:]?(.*)/);
return lineNumberMatch ? lineNumberMatch[1] : line;
})
.join('\n');
};
copyButton.addEventListener('click', () => {
navigator.clipboard.writeText(codeBlock.textContent)
const processedCode = removeLineNumbers(codeBlock.textContent);
navigator.clipboard.writeText(processedCode)
.then(() => {
copyButton.textContent = copiedText;
@ -86,7 +96,6 @@ let Stack = {
})
.catch(err => {
alert(err)
console.log('Something went wrong', err);
});
});
});