mirror of
https://github.com/CaiJimmy/hugo-theme-stack.git
synced 2025-04-28 19:43:31 +08:00
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:
parent
9e6b7b22a9
commit
a739470364
@ -75,8 +75,18 @@ let Stack = {
|
|||||||
const codeBlock = highlight.querySelector('code[data-lang]');
|
const codeBlock = highlight.querySelector('code[data-lang]');
|
||||||
if (!codeBlock) return;
|
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', () => {
|
copyButton.addEventListener('click', () => {
|
||||||
navigator.clipboard.writeText(codeBlock.textContent)
|
const processedCode = removeLineNumbers(codeBlock.textContent);
|
||||||
|
navigator.clipboard.writeText(processedCode)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
copyButton.textContent = copiedText;
|
copyButton.textContent = copiedText;
|
||||||
|
|
||||||
@ -86,7 +96,6 @@ let Stack = {
|
|||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
alert(err)
|
alert(err)
|
||||||
console.log('Something went wrong', err);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user