From 16ec104fb1889adfa05ef15ac5df7ca228655249 Mon Sep 17 00:00:00 2001 From: Jack Hickey <133868041+nginx-jack@users.noreply.github.com> Date: Tue, 25 Mar 2025 10:32:34 +0000 Subject: [PATCH] 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. --- assets/js/code-copy.js | 45 ++++++++++++++++--- exampleSite/content/test-product/_index.md | 5 +++ .../content/test-product/code-blocks.md | 23 ++++++++++ exampleSite/layouts/index.html | 3 ++ 4 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 exampleSite/content/test-product/_index.md create mode 100644 exampleSite/content/test-product/code-blocks.md diff --git a/assets/js/code-copy.js b/assets/js/code-copy.js index f1bfa1e..eed06d8 100644 --- a/assets/js/code-copy.js +++ b/assets/js/code-copy.js @@ -7,12 +7,45 @@ 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 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 element found in:', codeBlock); + } + + await clipboard.writeText(codeText); button.blur(); /* Chrome fix */ button.innerHTML = ' Copied!'; setTimeout(() => { @@ -20,7 +53,7 @@ function CopyCode(clipboard) { }, 2000); } catch (error) { button.innerHTML = ' Error'; - console.error(error); + console.error('Copy error:', error); } }); diff --git a/exampleSite/content/test-product/_index.md b/exampleSite/content/test-product/_index.md new file mode 100644 index 0000000..0561b34 --- /dev/null +++ b/exampleSite/content/test-product/_index.md @@ -0,0 +1,5 @@ +--- +description: Test pages for nginx-hugo-theme +title: Test pages for nginx-hugo-theme +weight: 100 +--- diff --git a/exampleSite/content/test-product/code-blocks.md b/exampleSite/content/test-product/code-blocks.md new file mode 100644 index 0000000..99a6e6d --- /dev/null +++ b/exampleSite/content/test-product/code-blocks.md @@ -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/ ` + $ :nxt_hint:`python3 --version ` + Python :nxt_hint:`3.Y.Z ` + $ python3 -m venv :nxt_hint:`venv ` + $ source :nxt_hint:`venv `/bin/activate + $ pip install |app-pip-package| + $ deactivate +``` diff --git a/exampleSite/layouts/index.html b/exampleSite/layouts/index.html index 3d30d66..2c0398f 100644 --- a/exampleSite/layouts/index.html +++ b/exampleSite/layouts/index.html @@ -6,6 +6,9 @@

Homepage Example

  • NGINX and NGINX Plus
  • +
  • + Test Product +