diff --git a/assets/ts/copyButton.ts b/assets/ts/copyButton.ts index 03cd6e60..360951bb 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 ad27b9a1..56c0b620 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 96704645..46c7feee 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 a4732969..efe8e9cb 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 }} - +