diff options
author | WingLim <winglim@qq.com> | 2021-09-25 11:52:42 +0300 |
---|---|---|
committer | WingLim <winglim@qq.com> | 2021-09-25 11:52:42 +0300 |
commit | cad48509a4fa1ebd09468d5530dfe38e3d7c48dc (patch) | |
tree | 1deda96fb6a796c70e1638edea670ce20a3c042d | |
parent | a0022251d97947d47d769291061dd72e9cadd3a1 (diff) |
fix: copy content without line numbers
-rw-r--r-- | assets/ts/copyButton.ts | 8 | ||||
-rw-r--r-- | assets/ts/features.ts | 4 | ||||
-rw-r--r-- | exampleSite/config.yaml | 5 | ||||
-rw-r--r-- | layouts/partials/footer/footer.html | 4 |
4 files changed, 17 insertions, 4 deletions
diff --git a/assets/ts/copyButton.ts b/assets/ts/copyButton.ts index 03cd6e6..360951b 100644 --- a/assets/ts/copyButton.ts +++ b/assets/ts/copyButton.ts @@ -5,7 +5,7 @@ const codeBlocks = document.querySelectorAll('.article-post .highlight'); const copyText = `Copy`, copiedText = `Copied!`; -export let renderCopyButton = function() { +export let renderCopyButton = function(enableLineNos: boolean) { codeBlocks.forEach(codeBlock => { const copyButton = document.createElement('button') copyButton.innerHTML = copyText @@ -15,7 +15,11 @@ export let renderCopyButton = function() { const pre = codeBlock.getElementsByTagName('pre'); // This theme's code block has line number, so the second is where the // real code locate - const code = pre[1].textContent; + let codeIndex = 0 + if (enableLineNos) { + codeIndex = 1 + } + const code = pre[codeIndex].textContent; copyButton.addEventListener('click', () => { navigator.clipboard.writeText(code) diff --git a/assets/ts/features.ts b/assets/ts/features.ts index ad27b9a..56c0b62 100644 --- a/assets/ts/features.ts +++ b/assets/ts/features.ts @@ -3,8 +3,10 @@ import { renderCopyButton } from "ts/copyButton" import { renderFootnotes } from "ts/footnotes" let enableFootnotes = false +let enableLineNos = false if (document.currentScript) { enableFootnotes = document.currentScript.dataset.enableFootnotes == 'true' + enableLineNos = document.currentScript.dataset.enableLinenos == 'true' } const init = () => { @@ -12,7 +14,7 @@ const init = () => { if (enableFootnotes) { renderFootnotes() } - renderCopyButton() + renderCopyButton(enableLineNos) } window.addEventListener('load', () => { diff --git a/exampleSite/config.yaml b/exampleSite/config.yaml index 9670464..46c7fee 100644 --- a/exampleSite/config.yaml +++ b/exampleSite/config.yaml @@ -16,6 +16,11 @@ params: # Enable float footnotes. # Default to true enableFootnotes: true + + # Enable line numbers, it need to set the same value with + # markup.hightlight.lineNos + # Default to true + enableLineNos: true siteName: "Hugo Tania is Amazing" siteDesc: "Hugo is Absurdly Fast!" author: "Hugo Tania" diff --git a/layouts/partials/footer/footer.html b/layouts/partials/footer/footer.html index a473296..efe8e9c 100644 --- a/layouts/partials/footer/footer.html +++ b/layouts/partials/footer/footer.html @@ -35,5 +35,7 @@ {{- end }} {{- $opts := dict "minify" hugo.IsProduction -}} {{ $features := resources.Get "ts/features.ts" | js.Build $opts | fingerprint }} - <script defer src="{{ $features.RelPermalink }}" data-enable-footnotes="{{ .Site.Params.enableFootnotes | default true }}"></script> + <script defer src="{{ $features.RelPermalink }}" + data-enable-footnotes="{{ .Site.Params.enableFootnotes | default true }}" + data-enable-lineNos="{{ .Site.Params.enableLineNos | default false }}"></script> </footer> |