From 2c9f1af5b2c30a41263500eceb84e338681cc71b Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:36:11 +0800 Subject: [PATCH 1/7] refactor(head): remove unnecessary newScratch --- layouts/partials/head.html | 94 ++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 54 deletions(-) diff --git a/layouts/partials/head.html b/layouts/partials/head.html index e12a7bf4..8ba05708 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -37,83 +37,71 @@ }} {{ end -}} - {{/* Asset bundles */}} - {{ $assets := newScratch }} - {{ $cssScheme := resources.Get (printf "css/schemes/%s.css" (.Site.Params.colorScheme | default "blowfish")) }} - {{ if not $cssScheme }} - {{ $cssScheme = resources.Get "css/schemes/blowfish.css" }} - {{ end }} - {{ $assets.Add "css" (slice $cssScheme) }} - {{ $cssMain := resources.Get "css/compiled/main.css" }} - {{ $assets.Add "css" (slice $cssMain) }} - {{ $cssCustom := resources.Get "css/custom.css" }} - {{ if $cssCustom }} - {{ $assets.Add "css" (slice $cssCustom) }} + {{ $alg := .Site.Params.fingerprintAlgorithm | default "sha512" }} + + {{/* CSS */}} + {{ $cssResources := slice }} + {{ $schemeName := .Site.Params.colorScheme | default "blowfish" }} + {{ $cssScheme := resources.Get (printf "css/schemes/%s.css" $schemeName) | default (resources.Get "css/schemes/blowfish.css") }} + {{ $cssResources = $cssResources | append $cssScheme }} + {{ $cssResources = $cssResources | append (resources.Get "css/compiled/main.css") }} + {{ with resources.Get "css/custom.css" }} + {{ $cssResources = $cssResources | append . }} {{ end }} {{ if not .Site.Params.disableImageZoom | default true }} - {{ $cssZoom := resources.Get "lib/zoom/style.css" }} - {{ $assets.Add "css" (slice $cssZoom) }} + {{ $cssResources = $cssResources | append (resources.Get "lib/zoom/style.css") }} {{ end }} - {{ $bundleCSS := $assets.Get "css" | resources.Concat "css/main.bundle.css" | resources.Minify | resources.Fingerprint - (.Site.Params.fingerprintAlgorithm | default "sha512") - }} + {{ $bundleCSS := $cssResources | resources.Concat "css/main.bundle.css" | resources.Minify | resources.Fingerprint $alg }} - {{ $jsAppearance := resources.Get "js/appearance.js" }} - {{ $jsAppearance = $jsAppearance | resources.ExecuteAsTemplate "js/appearance.js" . | resources.Minify | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }} + {{/* JS loaded immediately */}} + {{ $jsAppearance := resources.Get "js/appearance.js" | resources.ExecuteAsTemplate "js/appearance.js" . | resources.Minify | resources.Fingerprint $alg }} - {{ if site.Params.footer.showScrollToTop | default true }} - {{ $jsToTop := resources.Get "js/scroll-to-top.js" }} - {{ $assets.Add "js" (slice $jsToTop) }} - {{ end }} - {{ if .Site.Params.enableA11y | default false }} - {{ $jsA11y := resources.Get "js/a11y.js" }} - {{ $jsA11y = $jsA11y | resources.Minify | resources.Fingerprint (site.Params.fingerprintAlgorithm | default "sha512") }} + {{ $enableA11y := .Site.Params.enableA11y | default false }} + {{ if $enableA11y }} + {{ $jsA11y := resources.Get "js/a11y.js" | resources.Minify | resources.Fingerprint $alg }} {{ end }} - {{ $shouldIncludeZenMode := or (.Site.Params.enableA11y | default false) (.Params.showZenMode | default (.Site.Params.article.showZenMode | default false)) }} + {{ $showZenMode := .Params.showZenMode | default (.Site.Params.article.showZenMode | default false) }} + {{ $shouldIncludeZenMode := or $enableA11y $showZenMode }} {{ if and .IsPage $shouldIncludeZenMode }} - {{ $jsZenMode := resources.Get "js/zen-mode.js" }} - {{ $jsZenMode = $jsZenMode | resources.Minify | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }} + {{ $jsZenMode := resources.Get "js/zen-mode.js" | resources.Minify | resources.Fingerprint $alg }} {{ end }} + {{ if not .Site.Params.disableImageZoom | default true }} + {{ $zoomJS := resources.Get "lib/zoom/zoom.min.umd.js" | resources.Fingerprint $alg }} + + {{ end }} + + {{/* JS deferred */}} + {{ $jsResources := slice }} {{ if site.Params.footer.showScrollToTop | default true }} - {{ $jsToTop := resources.Get "js/scroll-to-top.js" }} - {{ $assets.Add "js" (slice $jsToTop) }} + {{ $jsResources = $jsResources | append (resources.Get "js/scroll-to-top.js") }} {{ end }} {{ if .Site.Params.enableSearch | default false }} - {{ $jsFuse := resources.Get "lib/fuse/fuse.min.js" }} - {{ $jsSearch := resources.Get "js/search.js" }} - {{ $assets.Add "js" (slice $jsFuse $jsSearch) }} + {{ $jsResources = $jsResources | append (resources.Get "lib/fuse/fuse.min.js") | append (resources.Get "js/search.js") }} {{ end }} {{ if .Site.Params.enableCodeCopy | default false }} - {{ $jsCode := resources.Get "js/code.js" }} - {{ $assets.Add "js" (slice $jsCode) }} + {{ $jsResources = $jsResources | append (resources.Get "js/code.js") }} {{ end }} {{ if .Site.Params.rtl | default false }} - {{ $jsRTL := resources.Get "js/rtl.js" }} - {{ $assets.Add "js" (slice $jsRTL) }} + {{ $jsResources = $jsResources | append (resources.Get "js/rtl.js") }} {{ end }} - {{ $jsMobileMenu := resources.Get "js/mobilemenu.js" }} - {{ $assets.Add "js" (slice $jsMobileMenu) }} - {{ $buttonLikes := resources.Get "js/button-likes.js" }} - {{ $assets.Add "js" (slice $buttonLikes) }} - {{ $katexRender := resources.Get "js/katex-render.js" }} - {{ $assets.Add "js" (slice $katexRender) }} - {{ if $assets.Get "js" }} - {{ $bundleJS := $assets.Get "js" | resources.Concat "js/main.bundle.js" | resources.Minify | resources.Fingerprint - (.Site.Params.fingerprintAlgorithm | default "sha512") - }} + {{ $jsResources = $jsResources | append (resources.Get "js/mobilemenu.js") }} + {{ $jsResources = $jsResources | append (resources.Get "js/button-likes.js") }} + {{ $jsResources = $jsResources | append (resources.Get "js/katex-render.js") }} + {{ if $jsResources }} + {{ $bundleJS := $jsResources | resources.Concat "js/main.bundle.js" | resources.Minify | resources.Fingerprint $alg }} {{ end }} - {{ if not .Site.Params.disableImageZoom | default true }} - {{ $zoomJS := resources.Get "lib/zoom/zoom.min.umd.js" | resources.Fingerprint (.Site.Params.fingerprintAlgorithm | default "sha512") }} - - {{ end }} {{/* Icons */}} {{ if templates.Exists "partials/favicons.html" }} @@ -241,7 +225,9 @@ {{/* Advertisement */}} {{ with .Site.Params.advertisement.adsense }} - + {{ end }} From 7b694613da1a07c3a1d5d755cc1d302ff3b0f224 Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:37:23 +0800 Subject: [PATCH 2/7] refactor(head): remove unnecessary space trim --- layouts/partials/head.html | 60 +++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 8ba05708..2e9105e4 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -8,21 +8,21 @@ {{/* Title */}} - {{ if .IsHome -}} + {{ if .IsHome }} {{ .Site.Title | emojify }} - {{- else -}} + {{ else }} {{ .Title | emojify }} · {{ .Site.Title | emojify }} - {{- end }} + {{ end }} {{/* Metadata */}} - {{ with (.Params.Summary | default .Params.Description) | default .Site.Params.description -}} + {{ with (.Params.Summary | default .Params.Description) | default .Site.Params.description }} - {{- end }} - {{ with .Params.Tags | default .Site.Params.keywords -}} - - {{- end }} + {{ end }} + {{ with .Params.Tags | default .Site.Params.keywords }} + + {{ end }} {{ with .Site.Params.robots }} {{ end }} @@ -30,12 +30,12 @@ {{ end }} - {{ range .AlternativeOutputFormats -}} + {{ range .AlternativeOutputFormats }} {{ printf ` ` .Rel .MediaType.Type .RelPermalink ($.Site.Title | emojify) | safeHTML }} - {{ end -}} + {{ end }} {{ $alg := .Site.Params.fingerprintAlgorithm | default "sha512" }} @@ -143,25 +143,25 @@ {{ template "_internal/opengraph.html" . }} {{ template "_internal/twitter_cards.html" . }} - {{- /* Main page always uses this; fallback elsewhere if no feature image */ -}} - {{- /* See https://gohugo.io/templates/embedded/#open-graph */ -}} - {{- $images := .Resources.ByType "image" -}} - {{- $socialImage := $images.GetMatch "*feature*" -}} + {{ /* Main page always uses this; fallback elsewhere if no feature image */ }} + {{ /* See https://gohugo.io/templates/embedded/#open-graph */ }} + {{ $images := .Resources.ByType "image" }} + {{ $socialImage := $images.GetMatch "*feature*" }} - {{- if not $socialImage -}} - {{- with .Site.Params.defaultSocialImage -}} - {{- if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") -}} - {{- $socialImage = resources.GetRemote . -}} - {{- else -}} - {{- $socialImage = resources.Get . -}} - {{- end -}} - {{- end -}} - {{- end -}} + {{ if not $socialImage }} + {{ with .Site.Params.defaultSocialImage }} + {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} + {{ $socialImage = resources.GetRemote . }} + {{ else }} + {{ $socialImage = resources.Get . }} + {{ end }} + {{ end }} + {{ end }} - {{- with $socialImage -}} + {{ with $socialImage }} - {{- end -}} + {{ end }} {{/* Schema */}} {{ partial "schema.html" . }} @@ -202,7 +202,7 @@ - + {{ end }} {{ end }} From b3bc04cfc3e0e1e35ff74b375fa68ff18cc09aa2 Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:41:43 +0800 Subject: [PATCH 3/7] refactor(head): move meta to the front --- layouts/partials/head.html | 114 ++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 58 deletions(-) diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 2e9105e4..3ca809de 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -37,6 +37,59 @@ }} {{ end }} + {{/* Me */}} + {{ with .Site.Params.Author.name }} + + {{ end }} + {{ with .Site.Params.Author.links }} + {{ range $links := . }} + {{ range $name, $url := $links }} + {{ if not (strings.HasPrefix $url "mailto:") }} + + {{ end }} + {{ end }} + {{ end }} + {{ end }} + + {{/* Social */}} + {{ template "_internal/opengraph.html" . }} + {{ template "_internal/twitter_cards.html" . }} + + {{/* Main page always uses this; fallback elsewhere if no feature image */}} + {{/* See https://gohugo.io/templates/embedded/#open-graph */}} + {{ $images := .Resources.ByType "image" }} + {{ $socialImage := $images.GetMatch "*feature*" }} + {{ if not $socialImage }} + {{ with .Site.Params.defaultSocialImage }} + {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} + {{ $socialImage = resources.GetRemote . }} + {{ else }} + {{ $socialImage = resources.Get . }} + {{ end }} + {{ end }} + {{ end }} + {{ with $socialImage }} + + + {{ end }} + + {{/* Site Verification */}} + {{ with .Site.Params.verification.google }} + + {{ end }} + {{ with .Site.Params.verification.bing }} + + {{ end }} + {{ with .Site.Params.verification.pinterest }} + + {{ end }} + {{ with .Site.Params.verification.yandex }} + + {{ end }} + {{ with .Site.Params.verification.fediverse }} + + {{ end }} + {{ $alg := .Site.Params.fingerprintAlgorithm | default "sha512" }} {{/* CSS */}} @@ -112,6 +165,9 @@ data-copied="{{ i18n "code.copied" }}"> {{ end }} + {{/* Conditional loaded resources */}} + {{ partial "vendor.html" . }} + {{/* Icons */}} {{ if templates.Exists "partials/favicons.html" }} {{ partialCached "favicons.html" .Site }} @@ -122,67 +178,9 @@ {{ end }} - {{/* Site Verification */}} - {{ with .Site.Params.verification.google }} - - {{ end }} - {{ with .Site.Params.verification.bing }} - - {{ end }} - {{ with .Site.Params.verification.pinterest }} - - {{ end }} - {{ with .Site.Params.verification.yandex }} - - {{ end }} - {{ with .Site.Params.verification.fediverse }} - - {{ end }} - - {{/* Social */}} - {{ template "_internal/opengraph.html" . }} - {{ template "_internal/twitter_cards.html" . }} - - {{ /* Main page always uses this; fallback elsewhere if no feature image */ }} - {{ /* See https://gohugo.io/templates/embedded/#open-graph */ }} - {{ $images := .Resources.ByType "image" }} - {{ $socialImage := $images.GetMatch "*feature*" }} - - {{ if not $socialImage }} - {{ with .Site.Params.defaultSocialImage }} - {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} - {{ $socialImage = resources.GetRemote . }} - {{ else }} - {{ $socialImage = resources.Get . }} - {{ end }} - {{ end }} - {{ end }} - - {{ with $socialImage }} - - - {{ end }} - {{/* Schema */}} {{ partial "schema.html" . }} - {{/* Me */}} - {{ with .Site.Params.Author.name }} - - {{ end }} - {{ with .Site.Params.Author.links }} - {{ range $links := . }} - {{ range $name, $url := $links }} - {{ if not (strings.HasPrefix $url "mailto:") }} - - {{ end }} - {{ end }} - {{ end }} - {{ end }} - - {{/* Vendor */}} - {{ partial "vendor.html" . }} - {{/* Analytics */}} {{ partial "analytics/main.html" .Site }} From 374a0f51f71dc6e324bac23a1d3d7f930cc5825f Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:44:02 +0800 Subject: [PATCH 4/7] fix(head): social image meta duplication --- layouts/partials/head.html | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 3ca809de..07b9dd1f 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -54,24 +54,28 @@ {{/* Social */}} {{ template "_internal/opengraph.html" . }} {{ template "_internal/twitter_cards.html" . }} - - {{/* Main page always uses this; fallback elsewhere if no feature image */}} - {{/* See https://gohugo.io/templates/embedded/#open-graph */}} - {{ $images := .Resources.ByType "image" }} - {{ $socialImage := $images.GetMatch "*feature*" }} - {{ if not $socialImage }} + {{/* Use defaultSocialImage if feature image does not exist */}} + {{ $featureImage := "" }} + {{ $pageImages := .Resources.ByType "image" }} + {{ range slice "*featured*" "*cover*" "*thumbnail*" }} + {{ if not $featureImage }} + {{ $featureImage = $pageImages.GetMatch . }} + {{ end }} + {{ end }} + {{ if not $featureImage }} {{ with .Site.Params.defaultSocialImage }} + {{ $socialImage := "" }} {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} {{ $socialImage = resources.GetRemote . }} {{ else }} {{ $socialImage = resources.Get . }} {{ end }} + {{ with $socialImage }} + + + {{ end }} {{ end }} {{ end }} - {{ with $socialImage }} - - - {{ end }} {{/* Site Verification */}} {{ with .Site.Params.verification.google }} From 8b41f894559250951adf8cb84bcf90c794da8f57 Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:48:43 +0800 Subject: [PATCH 5/7] fix(head): only load analytics when IsProduction --- layouts/partials/head.html | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 07b9dd1f..e0e3c316 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -186,7 +186,9 @@ {{ partial "schema.html" . }} {{/* Analytics */}} - {{ partial "analytics/main.html" .Site }} + {{ if hugo.IsProduction }} + {{ partial "analytics/main.html" . }} + {{ end }} {{/* Extend head - eg. for custom analytics scripts, etc. */}} {{ if templates.Exists "partials/extend-head.html" }} From b9a45ea2a98279da9e2cdba95f6ac3eb2729c4ee Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:50:27 +0800 Subject: [PATCH 6/7] fix(head): pass current context to analytics --- layouts/partials/analytics/main.html | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/layouts/partials/analytics/main.html b/layouts/partials/analytics/main.html index 2b3bd197..0736dd17 100644 --- a/layouts/partials/analytics/main.html +++ b/layouts/partials/analytics/main.html @@ -1,12 +1,12 @@ -{{ with site.Params.fathomAnalytics.site }} - {{ partial "analytics/fathom.html" }} +{{ if site.Params.fathomAnalytics.site }} + {{ partial "analytics/fathom.html" . }} {{ end }} -{{ with site.Config.Services.GoogleAnalytics.ID }} - {{ partial "analytics/ga.html" }} +{{ if site.Config.Services.GoogleAnalytics.ID }} + {{ partial "analytics/ga.html" . }} {{ end }} -{{ with site.Params.umamiAnalytics.websiteid }} - {{ partial "analytics/umami.html" }} +{{ if site.Params.umamiAnalytics.websiteid }} + {{ partial "analytics/umami.html" . }} {{ end }} -{{ with site.Params.selineAnalytics.token }} - {{ partial "analytics/seline.html" }} +{{ if site.Params.selineAnalytics.token }} + {{ partial "analytics/seline.html" . }} {{ end }} From c599c3489e9b7c270a663b30ec14c3f01b6098be Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Sat, 4 Oct 2025 07:36:22 +0800 Subject: [PATCH 7/7] fix(head): scroll to top element not found error --- layouts/_default/baseof.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 40965c04..50a06183 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -27,7 +27,7 @@
{{ block "main" . }}{{ end }} - {{ if and (site.Params.footer.showScrollToTop | default true) (gt .WordCount 1) }} + {{ if and (site.Params.footer.showScrollToTop | default true) }} {{- partial "scroll-to-top.html" . -}} {{ end }}