From 1ca9fdb7603d4a0f71195d94222072a516cbc4fe Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Sat, 31 Aug 2024 22:38:57 +0800 Subject: [PATCH] :recycle: Refactor: javascript output for inline scripts --- layouts/_default/baseof.html | 1 + layouts/partials/assets.html | 12 ++++-- layouts/partials/init/index.html | 2 +- layouts/partials/plugin/script.html | 60 +++++++++++++++++------------ 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 86841d70..d9ed9ced 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -20,6 +20,7 @@
{{- /* Check theme isDark before body rendering */ -}} + {{- /* TODO resources.ExecuteAsTemplate */ -}} {{- $theme := .Site.Params.defaulttheme -}} diff --git a/layouts/partials/assets.html b/layouts/partials/assets.html index dc6af1c8..3d010853 100644 --- a/layouts/partials/assets.html +++ b/layouts/partials/assets.html @@ -262,13 +262,17 @@ {{- with .Store.Get "styleArr" -}} {{- $content := delimit . " " -}} - {{- $path := substr (md5 $content) 0 6 | printf "css/%v" -}} + {{- $path := substr (md5 $.RelPermalink) 0 8 | printf "css/%v" -}} {{- $options := printf "%v.min.css" $path | dict "targetPath" -}} {{- dict "Content" $content "Path" (printf "%v.scss" $path) "ToCSS" $options | dict "Scratch" $.Scratch "Data" | partial "scratch/style.html" -}} {{- end -}} {{- /* Config script */ -}} -{{- $config | jsonify | printf "window.config=%s;" | dict "Content" | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} +{{- $configJS := $config | jsonify | printf "window.config=%s;" -}} +{{- if hugo.IsServer -}} + {{- $configJS = add $configJS "console.log('Page config:', window.config);" -}} +{{- end -}} +{{- $configJS | dict "Content" | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}} {{- /* Theme script */ -}} {{- $options := dict "targetPath" "js/theme.min.js" "minify" hugo.IsProduction -}} @@ -290,7 +294,9 @@ {{- end -}} {{- with .Store.Get "scriptArr" -}} - {{- delimit . "\n" | dict "Content" | dict "Scratch" $.Scratch "Data" | partial "scratch/script.html" -}} + {{- $content := delimit . "\n" -}} + {{- $targetPath := substr (md5 $.RelPermalink) 0 8 | printf "js/%v.js" -}} + {{- dict "Content" $content "Path" $targetPath "Defer" true | dict "Scratch" $.Scratch "Data" | partial "scratch/script.html" -}} {{- end -}} {{- range (.Scratch.Get "this").style -}} diff --git a/layouts/partials/init/index.html b/layouts/partials/init/index.html index 8bba5086..cff82cba 100644 --- a/layouts/partials/init/index.html +++ b/layouts/partials/init/index.html @@ -1,4 +1,4 @@ -{{- .Scratch.Set "version" "v0.3.12-c2bcccc9" -}} +{{- .Scratch.Set "version" "v0.3.12-c49bb28b" -}} {{- .Scratch.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} diff --git a/layouts/partials/plugin/script.html b/layouts/partials/plugin/script.html index 95907884..a694c537 100644 --- a/layouts/partials/plugin/script.html +++ b/layouts/partials/plugin/script.html @@ -1,30 +1,36 @@ -{{- with .Content -}} - +{{- if strings.HasPrefix .Source " + {{- else -}} + {{- with .Content -}} + + {{- end -}} {{- end -}} {{- end -}}