Skip to content

Commit 16ec104

Browse files
committed
CodeCopy: Remove # and $ from bash code blocks
For v1 CodeCopy only. v2 will require an different implementation since the code is generated at build time instead of run time.
1 parent e35bebd commit 16ec104

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

assets/js/code-copy.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,53 @@ function CopyCode(clipboard) {
77

88
button.addEventListener('click', async () => {
99
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-
);
10+
let codeText = codeBlock.textContent
11+
.replace(/^\s*\d+\s/gm, '') // Remove line numbers
12+
.replace(/^\s*|\s*$/g, ''); // Trim whitespace at top/bottom
1513

14+
// Find nested <code> element
15+
const codeElement = codeBlock.querySelector('code');
16+
if (codeElement) {
17+
const classAttr = codeElement.getAttribute('class') || '';
18+
const dataLangAttr = codeElement.getAttribute('data-lang') || '';
19+
20+
if (
21+
classAttr.includes('language-bash') ||
22+
classAttr.includes('language-console') ||
23+
dataLangAttr === 'bash' ||
24+
dataLangAttr === 'console'
25+
) {
26+
codeText = codeText
27+
.split('\n')
28+
.map((line) => {
29+
let cleanedLine = line.trim();
30+
31+
// Remove `$` (non-root) or `#` (root) command indicators
32+
if (/^[$#]\s?/.test(cleanedLine)) {
33+
cleanedLine = cleanedLine.replace(/^[$#]\s?/, ''); // Remove `$` or `#`
34+
}
35+
36+
// Remove inline comments that come *after* a command
37+
const withoutComments = cleanedLine.replace(/\s+#.*/, '');
38+
39+
return withoutComments;
40+
})
41+
.filter((line) => line.trim() !== '') // Remove empty lines
42+
.join('\n');
43+
}
44+
} else {
45+
console.warn('No nested <code> element found in:', codeBlock);
46+
}
47+
48+
await clipboard.writeText(codeText);
1649
button.blur(); /* Chrome fix */
1750
button.innerHTML = '<i class="fas fa-check"></i> Copied!';
1851
setTimeout(() => {
1952
button.innerHTML = '<i class="fas fa-copy"></i> Copy';
2053
}, 2000);
2154
} catch (error) {
2255
button.innerHTML = '<i class="fas fa-exclamation"></i> Error';
23-
console.error(error);
56+
console.error('Copy error:', error);
2457
}
2558
});
2659

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
description: Test pages for nginx-hugo-theme
3+
title: Test pages for nginx-hugo-theme
4+
weight: 100
5+
---
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
description: Test Product code blocks
3+
title: Test Product code blocks
4+
weight: 200
5+
---
6+
7+
## Example using `#`
8+
9+
```console
10+
# chown -R unit:unit /path/to/app/ # User and group that Unit's router runs as by default
11+
```
12+
13+
## Example using `$`
14+
15+
```console
16+
$ cd :nxt_ph:`/path/to/app/ <Path to the application directory; use a real path in your configuration>`
17+
$ :nxt_hint:`python3 --version <Make sure your virtual environment version matches the module version>`
18+
Python :nxt_hint:`3.Y.Z <Major version, minor version, and revision number>`
19+
$ python3 -m venv :nxt_hint:`venv <Arbitrary name of the virtual environment>`
20+
$ source :nxt_hint:`venv <Name of the virtual environment from the previous command>`/bin/activate
21+
$ pip install |app-pip-package|
22+
$ deactivate
23+
```

exampleSite/layouts/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ <h1>Homepage Example</h1>
66
<li>
77
<a href="{{ relref . "nginx" }}">NGINX and NGINX Plus</a>
88
</li>
9+
<li>
10+
<a href="{{ relref . "test-product" }}">Test Product</a>
11+
</li>
912
</ul>
1013
</p>
1114

0 commit comments

Comments
 (0)