Skip to content

feat: update copycode js #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions assets/js/code-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,53 @@ function CopyCode(clipboard) {

button.addEventListener('click', async () => {
try {
await clipboard.writeText(
codeBlock.textContent
.replace(/^\s*\d+\s/gm, '') // remove line numbers
.replace(/^\s*|\s*$/g, '') // remove carriage returns at top and bottom of block
);
let codeText = codeBlock.textContent
.replace(/^\s*\d+\s/gm, '') // Remove line numbers
.replace(/^\s*|\s*$/g, ''); // Trim whitespace at top/bottom

// Find nested <code> element
const codeElement = codeBlock.querySelector('code');
if (codeElement) {
const classAttr = codeElement.getAttribute('class') || '';
const dataLangAttr = codeElement.getAttribute('data-lang') || '';

if (
classAttr.includes('language-bash') ||
classAttr.includes('language-console') ||
dataLangAttr === 'bash' ||
dataLangAttr === 'console'
) {
codeText = codeText
.split('\n')
.map((line) => {
let cleanedLine = line.trim();

// Remove `$` (non-root) or `#` (root) command indicators
if (/^[$#]\s?/.test(cleanedLine)) {
cleanedLine = cleanedLine.replace(/^[$#]\s?/, ''); // Remove `$` or `#`
}

// Remove inline comments that come *after* a command
const withoutComments = cleanedLine.replace(/\s+#.*/, '');

return withoutComments;
})
.filter((line) => line.trim() !== '') // Remove empty lines
.join('\n');
}
} else {
console.warn('No nested <code> element found in:', codeBlock);
}

await clipboard.writeText(codeText);
button.blur(); /* Chrome fix */
button.innerHTML = '<i class="fas fa-check"></i> Copied!';
setTimeout(() => {
button.innerHTML = '<i class="fas fa-copy"></i> Copy';
}, 2000);
} catch (error) {
button.innerHTML = '<i class="fas fa-exclamation"></i> Error';
console.error(error);
console.error('Copy error:', error);
}
});

Expand Down
5 changes: 5 additions & 0 deletions exampleSite/content/test-product/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
description: Test pages for nginx-hugo-theme
title: Test pages for nginx-hugo-theme
weight: 100
---
23 changes: 23 additions & 0 deletions exampleSite/content/test-product/code-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
description: Test Product code blocks
title: Test Product code blocks
weight: 200
---

## Example using `#`

```console
# chown -R unit:unit /path/to/app/ # User and group that Unit's router runs as by default
```

## Example using `$`

```console
$ cd :nxt_ph:`/path/to/app/ <Path to the application directory; use a real path in your configuration>`
$ :nxt_hint:`python3 --version <Make sure your virtual environment version matches the module version>`
Python :nxt_hint:`3.Y.Z <Major version, minor version, and revision number>`
$ python3 -m venv :nxt_hint:`venv <Arbitrary name of the virtual environment>`
$ source :nxt_hint:`venv <Name of the virtual environment from the previous command>`/bin/activate
$ pip install |app-pip-package|
$ deactivate
```
3 changes: 3 additions & 0 deletions exampleSite/layouts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ <h1>Homepage Example</h1>
<li>
<a href="{{ relref . "nginx" }}">NGINX and NGINX Plus</a>
</li>
<li>
<a href="{{ relref . "test-product" }}">Test Product</a>
</li>
</ul>
</p>

Expand Down
Loading