Skip to content

Commit a144367

Browse files
committed
feat: update copycode js
1 parent e35bebd commit a144367

File tree

1 file changed

+73
-27
lines changed

1 file changed

+73
-27
lines changed

assets/js/code-copy.js

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,77 @@
11
function CopyCode(clipboard) {
2-
document.querySelectorAll('.highlight').forEach((codeBlock) => {
3-
const button = document.createElement('button');
4-
button.className = 'code-copy';
5-
button.type = 'button';
6-
button.innerHTML = '<i class="fas fa-copy"></i> Copy';
7-
8-
button.addEventListener('click', async () => {
9-
try {
10-
await clipboard.writeText(
11-
codeBlock.textContent
12-
.replace(/^\s*\d+\s/gm, '') // remove line numbers
13-
.replace(/^\s*|\s*$/g, '') // remove carriage returns at top and bottom of block
14-
);
15-
16-
button.blur(); /* Chrome fix */
17-
button.innerHTML = '<i class="fas fa-check"></i> Copied!';
18-
setTimeout(() => {
19-
button.innerHTML = '<i class="fas fa-copy"></i> Copy';
20-
}, 2000);
21-
} catch (error) {
22-
button.innerHTML = '<i class="fas fa-exclamation"></i> Error';
23-
console.error(error);
24-
}
25-
});
26-
27-
codeBlock.parentNode.insertBefore(button, codeBlock);
28-
});
2+
document.querySelectorAll('.highlight').forEach((codeBlock) => {
3+
const button = document.createElement('button');
4+
button.className = 'code-copy';
5+
button.type = 'button';
6+
button.innerHTML = '<i class="fas fa-copy"></i> Copy';
7+
8+
button.addEventListener('click', async () => {
9+
console.log('Copy button pressed for a code block.');
10+
11+
try {
12+
let codeText = codeBlock.textContent
13+
.replace(/^\s*\d+\s/gm, '') // Remove line numbers
14+
.replace(/^\s*|\s*$/g, ''); // Trim whitespace at top/bottom
15+
16+
// Find nested <code> element
17+
const codeElement = codeBlock.querySelector('code');
18+
if (codeElement) {
19+
const classAttr = codeElement.getAttribute('class') || '';
20+
const dataLangAttr = codeElement.getAttribute('data-lang') || '';
21+
22+
if (
23+
classAttr.includes('language-bash') ||
24+
classAttr.includes('language-console') ||
25+
dataLangAttr === 'bash' ||
26+
dataLangAttr === 'console'
27+
) {
28+
console.log('Detected a shell code block:', { classAttr, dataLangAttr });
29+
30+
console.log('Before comment removal:', codeText);
31+
32+
codeText = codeText
33+
.split('\n')
34+
.map(line => {
35+
let cleanedLine = line.trim();
36+
37+
// Remove `$` (non-root) or `#` (root) command indicators
38+
if (/^[$#]\s?/.test(cleanedLine)) {
39+
console.log(`Detected command prompt indicator: ${cleanedLine}`);
40+
cleanedLine = cleanedLine.replace(/^[$#]\s?/, ''); // Remove `$` or `#`
41+
}
42+
43+
// Remove inline comments that come *after* a command
44+
const withoutComments = cleanedLine.replace(/\s+#.*/, '');
45+
if (cleanedLine.includes('#') && cleanedLine !== withoutComments) {
46+
console.log(`Removing inline comment: ${cleanedLine}${withoutComments}`);
47+
}
48+
49+
return withoutComments;
50+
})
51+
.filter(line => line.trim() !== '') // Remove empty lines
52+
.join('\n');
53+
54+
console.log('After comment removal:', codeText);
55+
}
56+
} else {
57+
console.log('No nested <code> element found in:', codeBlock);
58+
}
59+
60+
await clipboard.writeText(codeText);
61+
button.blur(); /* Chrome fix */
62+
button.innerHTML = '<i class="fas fa-check"></i> Copied!';
63+
setTimeout(() => {
64+
button.innerHTML = '<i class="fas fa-copy"></i> Copy';
65+
}, 2000);
66+
console.log('Code successfully copied to clipboard.');
67+
} catch (error) {
68+
button.innerHTML = '<i class="fas fa-exclamation"></i> Error';
69+
console.error('Copy error:', error);
70+
}
71+
});
72+
73+
codeBlock.parentNode.insertBefore(button, codeBlock);
74+
});
2975
}
3076

3177
CopyCode(navigator.clipboard);

0 commit comments

Comments
 (0)