Skip to content

Commit

Permalink
fix: copy content without line numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
WingLim committed Sep 25, 2021
1 parent a002225 commit cad4850
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
8 changes: 6 additions & 2 deletions assets/ts/copyButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion assets/ts/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ 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 = () => {
new ThemeColorScheme(document.getElementById('dark-mode-button'))
if (enableFootnotes) {
renderFootnotes()
}
renderCopyButton()
renderCopyButton(enableLineNos)
}

window.addEventListener('load', () => {
Expand Down
5 changes: 5 additions & 0 deletions exampleSite/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion layouts/partials/footer/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>

0 comments on commit cad4850

Please sign in to comment.