Skip to content

Commit

Permalink
feat: auto detect lineNos (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
WingLim authored Sep 27, 2021
1 parent 8ec313c commit c16e649
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 11 deletions.
4 changes: 2 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(enableLineNos: boolean) {
export let renderCopyButton = function() {
codeBlocks.forEach(codeBlock => {
const copyButton = document.createElement('button')
copyButton.innerHTML = copyText
Expand All @@ -16,7 +16,7 @@ export let renderCopyButton = function(enableLineNos: boolean) {
// This theme's code block has line number, so the second is where the
// real code locate
let codeIndex = 0
if (enableLineNos) {
if (pre.length == 2) {
codeIndex = 1
}
const code = pre[codeIndex].textContent;
Expand Down
4 changes: 1 addition & 3 deletions assets/ts/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ 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(enableLineNos)
renderCopyButton()
}

window.addEventListener('load', () => {
Expand Down
5 changes: 0 additions & 5 deletions exampleSite/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ 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 false
enableLineNos: true

siteName: "Hugo Tania is Amazing"
siteDesc: "Hugo is Absurdly Fast!"
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/footer/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@
{{ $features := resources.Get "ts/features.ts" | js.Build $opts | fingerprint }}
<script defer src="{{ $features.RelPermalink }}"
data-enable-footnotes="{{ .Site.Params.enableFootnotes | default true }}"
data-enable-lineNos="{{ .Site.Params.enableLineNos | default false }}"></script>
></script>
</footer>

0 comments on commit c16e649

Please sign in to comment.