diff options
| author | Reorx <novoreorx@gmail.com> | 2022-04-12 21:50:44 +0800 |
|---|---|---|
| committer | Reorx <novoreorx@gmail.com> | 2022-04-12 21:50:44 +0800 |
| commit | ff2dc3c45d11c5aad74931df686f9eb3cb72cedc (patch) | |
| tree | 05b44c13692eb2d86abbdd7485dda14bfc14d6f2 /layouts | |
| parent | c5d31c778b572c3952c35e918765901cb223c045 (diff) | |
| parent | 9678b1cb6fe7c999c673ba8261c00e7cce7a7533 (diff) | |
Merge branch 'side-toc'
* side-toc:
adjust head tags order
always render SEO templates
update readme
replace instantpage with instantclick
menu consistant font weight
fork: update footer
add some simple icons with `-simple` suffix
add EnableInstantPage param
tweak toc style
use setTimeout to reduce scroll callback frequency
add param DisableSmoothScroll
enhance toc scroll algorithm
active toc link while scrolling
add side class for toc if TocSide is true
allow customize post page width
Diffstat (limited to 'layouts')
| -rw-r--r-- | layouts/_default/baseof.html | 2 | ||||
| -rw-r--r-- | layouts/partials/footer.html | 91 | ||||
| -rw-r--r-- | layouts/partials/head.html | 19 | ||||
| -rw-r--r-- | layouts/partials/svg.html | 10 | ||||
| -rw-r--r-- | layouts/partials/toc.html | 2 |
5 files changed, 101 insertions, 23 deletions
diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 3d13f07..edd7809 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -14,7 +14,7 @@ {{- end -}} " id="top"> {{- partialCached "header.html" . .Page -}} - <main class="main"> + <main class="main {{- if (eq .Kind `page`) -}}{{- print " post" -}}{{- end -}}"> {{- block "main" . }}{{ end }} </main> {{ partialCached "footer.html" . .Layout .Kind (.Param "hideFooter") (.Param "ShowCodeCopyButtons") -}} diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index d76058f..4e348be 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -4,11 +4,11 @@ <span>{{ site.Copyright | markdownify }}</span> {{- else }} <span>© {{ now.Year }} <a href="{{ "" | absLangURL }}">{{ site.Title }}</a></span> - {{- end }} - <span> + {{- end -}} + <span style="display: inline-block; margin-left: 1em;"> Powered by <a href="https://gohugo.io/" rel="noopener noreferrer" target="_blank">Hugo</a> & - <a href="https://git.io/hugopapermod" rel="noopener" target="_blank">PaperMod</a> + <a href="https://github.com/reorx/hugo-PaperMod" rel="noopener" target="_blank">PaperMod</a> </span> </footer> {{- end }} @@ -24,6 +24,7 @@ {{- partial "extend_footer.html" . }} <script> + (function() { let menu = document.getElementById('menu') if (menu) { menu.scrollLeft = localStorage.getItem("menu-scroll-position"); @@ -32,17 +33,20 @@ } } + const disableSmoothScroll = '{{- if (.Param "DisableSmoothScroll") -}}1{{- end -}}' == '1'; + const enableInstantClick = '{{- if (.Param "EnableInstantClick") -}}1{{- end -}}' == '1'; + // instant click and smooth scroll are mutually exclusive + if (window.matchMedia('(prefers-reduced-motion: reduce)').matches || disableSmoothScroll || enableInstantClick) { + return; + } + // only run this code if smooth scroll should be enabled 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(); - } + document.querySelector(`[id='${decodeURIComponent(id)}']`).scrollIntoView({ + behavior: "smooth" + }); if (id === "top") { history.replaceState(null, null, " "); } else { @@ -50,7 +54,7 @@ } }); }); - + })(); </script> {{- if (not site.Params.disableScrollToTop) }} @@ -133,3 +137,68 @@ }); </script> {{- end }} + +{{- if (and (eq .Kind "page") (.Param "ShowToc") (.Param "TocSide")) }} +<script> +window.addEventListener('DOMContentLoaded', function (event) { + const headings = document.querySelectorAll('h1[id],h2[id],h3[id],h4[id],h5[id]'); + const activeClass = 'active'; + + // Make the first header active + let activeHeading = headings[0]; + getLinkByHeading(activeHeading).classList.add(activeClass); + + const onScroll = () => { + const passedHeadings = []; + for (const h of headings) { + // 5 px as a buffer + if (getOffsetTop(h) < 5) { + passedHeadings.push(h) + } else { + break; + } + } + if (passedHeadings.length > 0) { + newActiveHeading = passedHeadings[passedHeadings.length - 1]; + } else { + newActiveHeading = headings[0]; + } + if (activeHeading != newActiveHeading) { + getLinkByHeading(activeHeading).classList.remove(activeClass); + activeHeading = newActiveHeading; + getLinkByHeading(activeHeading).classList.add(activeClass); + } + } + + let timer = null; + window.addEventListener('scroll', () => { + if (timer !== null) { + clearTimeout(timer) + } + timer = setTimeout(onScroll, 50) + }, false); + + function getLinkByHeading(heading) { + const id = encodeURI(heading.getAttribute('id')).toLowerCase(); + return document.querySelector(`.toc ul li a[href="#${id}"]`); + } + + function getOffsetTop(heading) { + if (!heading.getClientRects().length) { + return 0; + } + let rect = heading.getBoundingClientRect(); + return rect.top + } +}, false); +</script> +{{- end }} + +{{- /* InstantClick.js */}} +{{- if (.Param "EnableInstantClick") }} +{{- $instantclick := slice (resources.Get "js/instantclick.js") | resources.Concat "assets/js/instantclick.js" | minify }} +<script src="{{ $instantclick.RelPermalink }}" data-no-instant + {{- if site.Params.assets.disableFingerprinting }}integrity="{{ $instantclick.Data.Integrity }}"{{- end }} +></script> +<script data-no-instant>InstantClick.init();</script> +{{- end }} diff --git a/layouts/partials/head.html b/layouts/partials/head.html index b921452..99d33ae 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -102,6 +102,15 @@ <link rel="alternate" hreflang="{{ .Lang }}" href="{{ .Permalink }}"> {{ end -}} +{{- /* Misc */}} +{{- if hugo.IsProduction | or (eq site.Params.env "production") }} +{{- template "_internal/google_analytics.html" . }} +{{- end -}} +{{- template "partials/templates/opengraph.html" . }} +{{- template "partials/templates/twitter_cards.html" . }} +{{- template "partials/templates/schema_json.html" . }} + +{{- partial "extend_head.html" . }} <noscript> <style> #theme-toggle, @@ -141,13 +150,3 @@ </style> {{- end }} </noscript> - -{{- 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/svg.html b/layouts/partials/svg.html index 0583736..a1b598e 100644 --- a/layouts/partials/svg.html +++ b/layouts/partials/svg.html @@ -191,6 +191,8 @@ d="M9 19c-5 1.5-5-2.5-7-3m14 6v-3.87a3.37 3.37 0 0 0-.94-2.61c3.14-.35 6.44-1.54 6.44-7A5.44 5.44 0 0 0 20 4.77 5.07 5.07 0 0 0 19.91 1S18.73.65 16 2.48a13.38 13.38 0 0 0-7 0C6.27.65 5.09 1 5.09 1A5.07 5.07 0 0 0 5 4.77a5.44 5.44 0 0 0-1.5 3.78c0 5.42 3.3 6.61 6.44 7A3.37 3.37 0 0 0 9 18.13V22"> </path> </svg> +{{- else if (eq $icon_name "github-simple") -}} +<svg role="img" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg> {{- else if (eq $icon_name "gitlab") -}} <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> @@ -394,6 +396,8 @@ <path d="M4 4a16 16 0 0 1 16 16" /> <circle cx="5" cy="19" r="1" /> </svg> +{{- else if (eq $icon_name "rss-simple") -}} +<svg role="img" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><title>RSS</title><path d="M19.199 24C19.199 13.467 10.533 4.8 0 4.8V0c13.165 0 24 10.835 24 24h-4.801zM3.291 17.415c1.814 0 3.293 1.479 3.293 3.295 0 1.813-1.485 3.29-3.301 3.29C1.47 24 0 22.526 0 20.71s1.475-3.294 3.291-3.295zM15.909 24h-4.665c0-6.169-5.075-11.245-11.244-11.245V8.09c8.727 0 15.909 7.184 15.909 15.91z"/></svg> {{- else if (eq $icon_name "serverfault") -}} <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="currentColor"> <path @@ -469,6 +473,8 @@ <path d="M21.198 2.433a2.242 2.242 0 0 0-1.022.215l-8.609 3.33c-2.068.8-4.133 1.598-5.724 2.21a405.15 405.15 0 0 1-2.849 1.09c-.42.147-.99.332-1.473.901-.728.968.193 1.798.919 2.286 1.61.516 3.275 1.009 4.654 1.472.509 1.793.997 3.592 1.48 5.388.16.36.506.494.864.498l-.002.018s.281.028.555-.038a2.1 2.1 0 0 0 .933-.517c.345-.324 1.28-1.244 1.811-1.764l3.999 2.952.032.018s.442.311 1.09.355c.324.022.75-.04 1.116-.308.37-.27.613-.702.728-1.196.342-1.492 2.61-12.285 2.997-14.072l-.01.042c.27-1.006.17-1.928-.455-2.474a1.654 1.654 0 0 0-1.034-.407z" /> </svg> +{{- else if (eq $icon_name "telegram-simple") -}} +<svg role="img" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><title>Telegram</title><path d="M11.944 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.056 0zm4.962 7.224c.1-.002.321.023.465.14a.506.506 0 0 1 .171.325c.016.093.036.306.02.472-.18 1.898-.962 6.502-1.36 8.627-.168.9-.499 1.201-.82 1.23-.696.065-1.225-.46-1.9-.902-1.056-.693-1.653-1.124-2.678-1.8-1.185-.78-.417-1.21.258-1.91.177-.184 3.247-2.977 3.307-3.23.007-.032.014-.15-.056-.212s-.174-.041-.249-.024c-.106.024-1.793 1.14-5.061 3.345-.48.33-.913.49-1.302.48-.428-.008-1.252-.241-1.865-.44-.752-.245-1.349-.374-1.297-.789.027-.216.325-.437.893-.663 3.498-1.524 5.83-2.529 6.998-3.014 3.332-1.386 4.025-1.627 4.476-1.635z"/></svg> {{- else if (eq $icon_name "twitch") -}} <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> @@ -481,6 +487,8 @@ d="M23 3a10.9 10.9 0 0 1-3.14 1.53 4.48 4.48 0 0 0-7.86 3v1A10.66 10.66 0 0 1 3 4s-4 9 5 13a11.64 11.64 0 0 1-7 2c9 5 20 0 20-11.5a4.5 4.5 0 0 0-.08-.83A7.72 7.72 0 0 0 23 3z"> </path> </svg> +{{- else if (eq $icon_name "twitter-simple") -}} +<svg role="img" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><title>Twitter</title><path d="M23.953 4.57a10 10 0 01-2.825.775 4.958 4.958 0 002.163-2.723c-.951.555-2.005.959-3.127 1.184a4.92 4.92 0 00-8.384 4.482C7.69 8.095 4.067 6.13 1.64 3.162a4.822 4.822 0 00-.666 2.475c0 1.71.87 3.213 2.188 4.096a4.904 4.904 0 01-2.228-.616v.06a4.923 4.923 0 003.946 4.827 4.996 4.996 0 01-2.212.085 4.936 4.936 0 004.604 3.417 9.867 9.867 0 01-6.102 2.105c-.39 0-.779-.023-1.17-.067a13.995 13.995 0 007.557 2.209c9.053 0 13.998-7.496 13.998-13.985 0-.21 0-.42-.015-.63A9.935 9.935 0 0024 4.59z"/></svg> {{- else if (eq $icon_name "unsplash") -}} <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 40"> <path fill="none" stroke="currentColor" stroke-linejoin="round" stroke-miterlimit="10" stroke-width="4" @@ -513,6 +521,8 @@ </path> <polygon points="9.75 15.02 15.5 11.75 9.75 8.48 9.75 15.02"></polygon> </svg> +{{- else if (eq $icon_name "pinboard-simple") -}} +<svg role="img" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><title>Pinboard</title><path d="M13.352 14.585l-4.509 4.614.72-4.062L3.428 7.57 0 7.753 7.58 0v2.953l7.214 6.646 4.513-1.105-4.689 4.982L24 24l-10.648-9.415z"/></svg> {{- else if $icon_name -}} <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> diff --git a/layouts/partials/toc.html b/layouts/partials/toc.html index d9b8b23..02a61e9 100644 --- a/layouts/partials/toc.html +++ b/layouts/partials/toc.html @@ -1,7 +1,7 @@ {{- $headers := findRE "<h[1-6].*?>(.|\n])+?</h[1-6]>" .Content -}} {{- $has_headers := ge (len $headers) 1 -}} {{- if $has_headers -}} -<div class="toc"> +<div class="toc{{- if (.Param "TocSide") -}}{{- print " side" -}}{{- end -}}"> <details {{if (.Param "TocOpen") }} open{{ end }}> <summary accesskey="c" title="(Alt + C)"> <span class="details">{{- i18n "toc" | default "Table of Contents" }}</span> |
