From 567dcf565d86f3e191aac760824ced6a7f3b0f08 Mon Sep 17 00:00:00 2001 From: Wonderfall Date: Thu, 21 Jul 2022 18:55:21 +0200 Subject: [PATCH] remove inline js --- assets/js/papermod.js | 108 +++++++++++++++++++++++++++ layouts/partials/footer.html | 26 +++++++ layouts/partials/head.html | 139 +++++++++++++++++++++++++++++++++++ layouts/partials/script.html | 40 ++++++++++ 4 files changed, 313 insertions(+) create mode 100644 assets/js/papermod.js create mode 100644 layouts/partials/footer.html create mode 100644 layouts/partials/head.html create mode 100644 layouts/partials/script.html diff --git a/assets/js/papermod.js b/assets/js/papermod.js new file mode 100644 index 0000000..641867c --- /dev/null +++ b/assets/js/papermod.js @@ -0,0 +1,108 @@ +import * as params from '@params'; + +function initializeMenu() { + let menu = document.getElementById('menu') + if (menu) { + menu.scrollLeft = localStorage.getItem("menu-scroll-position"); + menu.onscroll = function () { + localStorage.setItem("menu-scroll-position", menu.scrollLeft); + } + } + + document.querySelectorAll('a[href^="#"]').forEach(anchor => { + anchor.addEventListener("click", function (e) { + e.preventDefault(); + var id = this.getAttribute("href").substr(1); + if (!window.matchMedia('(prefers-reduced-motion: reduce)').matches) { + document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView({ + behavior: "smooth" + }); + } else { + document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView(); + } + if (id === "top") { + history.replaceState(null, null, " "); + } else { + history.pushState(null, null, `#${id}`); + } + }); + }); +} + +function scrollToTop() { + var mybutton = document.getElementById("top-link"); + window.onscroll = function () { + if (document.body.scrollTop > 800 || document.documentElement.scrollTop > 800) { + mybutton.style.visibility = "visible"; + mybutton.style.opacity = "1"; + } else { + mybutton.style.visibility = "hidden"; + mybutton.style.opacity = "0"; + } + }; +} + +function themeToggle() { + document.getElementById("theme-toggle").addEventListener("click", () => { + if (document.body.className.includes("dark")) { + document.body.classList.remove('dark'); + localStorage.setItem("pref-theme", 'light'); + } else { + document.body.classList.add('dark'); + localStorage.setItem("pref-theme", 'dark'); + } + }) +} + +function showCodeCopyButtons() { + document.querySelectorAll('pre > code').forEach((codeblock) => { + const container = codeblock.parentNode.parentNode; + + const copybutton = document.createElement('button'); + copybutton.classList.add('copy-code'); + copybutton.innerHTML = '{{- i18n "code_copy" | default "copy" }}'; + + function copyingDone() { + copybutton.innerHTML = '{{- i18n "code_copied" | default "copied!" }}'; + setTimeout(() => { + copybutton.innerHTML = '{{- i18n "code_copy" | default "copy" }}'; + }, 2000); + } + + copybutton.addEventListener('click', (cb) => { + if ('clipboard' in navigator) { + navigator.clipboard.writeText(codeblock.textContent); + copyingDone(); + return; + } + + const range = document.createRange(); + range.selectNodeContents(codeblock); + const selection = window.getSelection(); + selection.removeAllRanges(); + selection.addRange(range); + try { + document.execCommand('copy'); + copyingDone(); + } catch (e) { }; + selection.removeRange(range); + }); + + if (container.classList.contains("highlight")) { + container.appendChild(copybutton); + } else if (container.parentNode.firstChild == container) { + // td containing LineNos + } else if (codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "TABLE") { + // table containing LineNos and code + codeblock.parentNode.parentNode.parentNode.parentNode.parentNode.appendChild(copybutton); + } else { + // code blocks not having highlight as parent class + codeblock.parentNode.appendChild(copybutton); + } + }); +} + +initializeMenu(); +if (params.scrollToTop) scrollToTop(); +if (params.themeToggle) themeToggle(); +if (params.showCodeCopyButtons) showCodeCopyButtons(); \ No newline at end of file diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html new file mode 100644 index 0000000..f62cb5c --- /dev/null +++ b/layouts/partials/footer.html @@ -0,0 +1,26 @@ +{{- if not (.Param "hideFooter") }} + +{{- end }} + +{{- if (not site.Params.disableScrollToTop) }} + + + + + +{{- end }} + +{{- partial "extend_footer.html" . }} + +{{- partial "script.html" . }} \ No newline at end of file diff --git a/layouts/partials/head.html b/layouts/partials/head.html new file mode 100644 index 0000000..96b6bba --- /dev/null +++ b/layouts/partials/head.html @@ -0,0 +1,139 @@ + + + +{{- if hugo.IsProduction | or (eq site.Params.env "production") | and (ne .Params.robotsNoIndex true) }} + +{{- else }} + +{{- end }} + +{{- /* Title */}} +{{ if .IsHome }}{{ else }}{{ if .Title }}{{ .Title }} | {{ end }}{{ end }}{{ site.Title }} + +{{- /* Meta */}} +{{- if .IsHome }} +{{ with site.Params.keywords -}}{{ end }} +{{- else }} + +{{- end }} + + + +{{- if site.Params.analytics.google.SiteVerificationTag }} + +{{- end }} +{{- if site.Params.analytics.yandex.SiteVerificationTag }} + +{{- end }} +{{- if site.Params.analytics.bing.SiteVerificationTag }} + +{{- end }} + +{{- /* Styles */}} + +{{- /* includes */}} +{{- $includes := slice }} +{{- $includes = $includes | append (" " | resources.FromString "assets/css/includes-blank.css")}} + +{{- if not (eq site.Params.assets.disableScrollBarStyle true) }} + {{- $ScrollStyle := (resources.Get "css/includes/scroll-bar.css") }} + {{- $includes = (append $ScrollStyle $includes) }} +{{- end }} + +{{- $includes_all := $includes | resources.Concat "assets/css/includes.css" }} + +{{- $theme_vars := (resources.Get "css/core/theme-vars.css") }} +{{- $reset := (resources.Get "css/core/reset.css") }} +{{- $media := (resources.Get "css/core/zmedia.css") }} +{{- $license_css := (resources.Get "css/core/license.css") }} +{{- $common := (resources.Match "css/common/*.css") | resources.Concat "assets/css/common.css" }} + +{{- /* include `an-old-hope` if hljs is on */}} +{{- $isHLJSdisabled := (site.Params.assets.disableHLJS | default false) }} +{{- $hljs := (cond ($isHLJSdisabled) (".chroma { background-color: unset !important;}" | resources.FromString "assets/css/hljs-blank.css") (resources.Get "css/hljs/an-old-hope.min.css")) }} + +{{- /* order is important */}} +{{- $core := (slice $theme_vars $reset $common $hljs $includes_all $media) | resources.Concat "assets/css/core.css" | resources.Minify }} +{{- $extended := (resources.Match "css/extended/*.css") | resources.Concat "assets/css/extended.css" | resources.Minify }} + +{{- /* bundle all required css */}} +{{- /* Add extended css after theme style */ -}} +{{- $stylesheet := (slice $license_css $core $extended) | resources.Concat "assets/css/stylesheet.css" }} + +{{- if not site.Params.assets.disableFingerprinting }} +{{- $stylesheet := $stylesheet | fingerprint }} + +{{- else }} + +{{- end }} + +{{- /* Favicons */}} + + + + + + + + +{{- /* RSS */}} +{{ range .AlternativeOutputFormats -}} + +{{ end -}} +{{- range .AllTranslations -}} + +{{ end -}} + + + +{{- partial "extend_head.html" . -}} + +{{- /* Misc */}} +{{- if hugo.IsProduction | or (eq site.Params.env "production") }} +{{- template "_internal/google_analytics.html" . }} +{{- template "partials/templates/opengraph.html" . }} +{{- template "partials/templates/twitter_cards.html" . }} +{{- template "partials/templates/schema_json.html" . }} +{{- end -}} diff --git a/layouts/partials/script.html b/layouts/partials/script.html new file mode 100644 index 0000000..bda3a1d --- /dev/null +++ b/layouts/partials/script.html @@ -0,0 +1,40 @@ +{{- /* Search */}} +{{- if (eq .Layout `search`) -}} + +{{- $fastsearch := resources.Get "js/fastsearch.js" | js.Build (dict "params" (dict "fuseOpts" site.Params.fuseOpts)) | resources.Minify }} +{{- $fusejs := resources.Get "js/fuse.basic.min.js" }} +{{- $license_js := resources.Get "js/license.js" }} +{{- if not site.Params.assets.disableFingerprinting }} +{{- $search := (slice $fusejs $license_js $fastsearch ) | resources.Concat "assets/js/search.js" | fingerprint }} + +{{- else }} +{{- $search := (slice $fusejs $fastsearch ) | resources.Concat "assets/js/search.js" }} + +{{- end }} +{{- end -}} + +{{- /* Highlight.js */}} +{{- $isHLJSdisabled := (site.Params.assets.disableHLJS | default .Params.disableHLJS ) }} +{{- if (and (eq .Kind "page") (ne .Layout "archives") (ne .Layout "search") (not $isHLJSdisabled)) }} +{{- if not site.Params.assets.disableFingerprinting }} +{{- $highlight := slice (resources.Get "js/highlight.min.js") | resources.Concat "assets/js/highlight.js" | fingerprint }} + +{{- else }} +{{- $highlight := slice (resources.Get "js/highlight.min.js") | resources.Concat "assets/js/highlight.js" }} + +{{- end }} +{{- end }} + +{{- /* PaperMod.js */}} +{{- $scrollToTop := (not site.Params.disableScrollToTop | default .Params.disableScrollToTop ) }} +{{- $themeToggle := (not site.Params.disableThemeToggle | default .Params.disableThemeToggle ) }} +{{- $showCodeCopyButtons := ((and (eq .Kind "page") (ne .Layout "archives") (ne .Layout "search") (site.Params.ShowCodeCopyButtons)) | default .Params.ShowCodeCopyButtons ) }} +{{- $papermod := resources.Get "js/papermod.js" | js.Build (dict "params" (dict "scrollToTop" $scrollToTop "themeToggle" $themeToggle "showCodeCopyButtons" $showCodeCopyButtons)) | resources.Minify }} +{{- if not site.Params.assets.disableFingerprinting }} +{{- $papermod_js := (slice $papermod) | resources.Concat "assets/js/papermod.js" | fingerprint }} + +{{- else }} +{{- $papermod_js := (slice $papermod) | resources.Concat "assets/js/papermod.js" }} + +{{- end }} \ No newline at end of file