From de08e29b003f0422c17441ebaa6aee66f53313af Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Sun, 12 Jun 2022 11:31:32 +0000 Subject: feat: use Hugo's code block render hook to implement code copy button Now it can have i18n support --- assets/ts/codeblock.ts | 28 +++++++++++++++++++++ assets/ts/main.ts | 35 ++------------------------ layouts/_default/_markup/render-codeblock.html | 20 +++++++++++++++ 3 files changed, 50 insertions(+), 33 deletions(-) create mode 100644 assets/ts/codeblock.ts create mode 100644 layouts/_default/_markup/render-codeblock.html diff --git a/assets/ts/codeblock.ts b/assets/ts/codeblock.ts new file mode 100644 index 0000000..08c3328 --- /dev/null +++ b/assets/ts/codeblock.ts @@ -0,0 +1,28 @@ +/** + * Copy button for code blocks +*/ +export default () => { + const copyButtons = document.querySelectorAll('.codeblock-copy'); + copyButtons.forEach(button => { + const codeblockID = button.getAttribute('data-id'), + copyText = button.textContent, + copiedText = button.getAttribute('data-copied-text'); + if (!codeblockID) return; + button.addEventListener('click', (e) => { + e.preventDefault(); + const codeblock = document.getElementById(codeblockID) as HTMLElement; + if (!codeblockID) return; + navigator.clipboard.writeText(codeblock.textContent) + .then(() => { + button.textContent = copiedText; + setTimeout(() => { + button.textContent = copyText; + }, 1000); + }) + .catch(err => { + alert(err) + console.log('Something went wrong', err); + }); + }, false); + }); +} \ No newline at end of file diff --git a/assets/ts/main.ts b/assets/ts/main.ts index e3f5b6a..5370b2c 100644 --- a/assets/ts/main.ts +++ b/assets/ts/main.ts @@ -6,7 +6,7 @@ * @link: https://github.com/CaiJimmy/hugo-theme-stack */ import StackGallery from "ts/gallery"; -import { getColor } from 'ts/color'; +import StackCodeBlock from "ts/codeblock"; import menu from 'ts/menu'; import createElement from 'ts/createElement'; import StackColorScheme from 'ts/colorScheme'; @@ -27,39 +27,8 @@ let Stack = { setupScrollspy(); } - /** - * Add copy button to code block - */ - const highlights = document.querySelectorAll('.article-content div.highlight'); - const copyText = `Copy`, - copiedText = `Copied!`; - - highlights.forEach(highlight => { - const copyButton = document.createElement('button'); - copyButton.innerHTML = copyText; - copyButton.classList.add('copyCodeButton'); - highlight.appendChild(copyButton); - - const codeBlock = highlight.querySelector('code[data-lang]'); - if (!codeBlock) return; - - copyButton.addEventListener('click', () => { - navigator.clipboard.writeText(codeBlock.textContent) - .then(() => { - copyButton.textContent = copiedText; - - setTimeout(() => { - copyButton.textContent = copyText; - }, 1000); - }) - .catch(err => { - alert(err) - console.log('Something went wrong', err); - }); - }); - }); - new StackColorScheme(document.getElementById('dark-mode-toggle')); + StackCodeBlock(); } } diff --git a/layouts/_default/_markup/render-codeblock.html b/layouts/_default/_markup/render-codeblock.html new file mode 100644 index 0000000..052efe3 --- /dev/null +++ b/layouts/_default/_markup/render-codeblock.html @@ -0,0 +1,20 @@ +{{- $class := .Attributes.class | default "" -}} +{{- $lang := .Attributes.lang | default .Type -}} +
+
+ {{ $lang }} + +
+ + {{- if transform.CanHighlight $lang -}} +
{{- highlight .Inner $lang -}}
+ {{- else -}} +
{{- .Inner -}}
+ {{- end -}} +
-- cgit v1.2.3