♻️ Refactor: javascript output for inline scripts

This commit is contained in:
Cell
2024-08-31 22:38:57 +08:00
parent 9e67e259fb
commit 1ca9fdb760
4 changed files with 46 additions and 29 deletions
+1
View File
@@ -20,6 +20,7 @@
</head>
<body data-header-desktop="{{ .Site.Params.header.desktopMode }}" data-header-mobile="{{ .Site.Params.header.mobileMode }}">
{{- /* Check theme isDark before body rendering */ -}}
{{- /* TODO resources.ExecuteAsTemplate */ -}}
{{- $theme := .Site.Params.defaulttheme -}}
<script>(window.localStorage?.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('{{ $theme }}' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '{{ $theme }}' === 'dark')) && document.body.setAttribute('data-theme', 'dark');</script>
+9 -3
View File
@@ -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 -}}
+1 -1
View File
@@ -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" . -}}
+35 -25
View File
@@ -1,30 +1,36 @@
{{- with .Content -}}
<script>{{ . | safeJS }}</script>
{{- if strings.HasPrefix .Source "<script" -}}
{{- safeHTML .Source -}}
{{- else -}}
{{- if strings.HasPrefix .Source "<script" -}}
{{- safeHTML .Source -}}
{{- else -}}
{{- $src := .Source -}}
{{- $integrity := .Integrity -}}
{{- if $src | and (not (urls.Parse $src).Host) -}}
{{- $resource := resources.Get $src -}}
{{- with .Template -}}
{{- $resource = $resource | resources.ExecuteAsTemplate . $.Context -}}
{{- end -}}
{{- if .Minify -}}
{{- $resource = $resource | minify -}}
{{- end -}}
{{- with .Build -}}
{{- $resource = $resource | js.Build . -}}
{{- end -}}
{{- with .Fingerprint -}}
{{- $resource = $resource | fingerprint . -}}
{{- $integrity = $resource.Data.Integrity -}}
{{- end -}}
{{- with $resource.RelPermalink -}}
{{- $src = $resource.RelPermalink -}}
{{- end -}}
{{- /* Script source */ -}}
{{- $src := .Source -}}
{{- $integrity := .Integrity -}}
{{- if $src | and (not (urls.Parse $src).Host) -}}
{{- $resource := resources.Get $src -}}
{{- with .Template -}}
{{- $resource = $resource | resources.ExecuteAsTemplate . $.Context -}}
{{- end -}}
{{- if .Minify -}}
{{- $resource = $resource | minify -}}
{{- end -}}
{{- with .Build -}}
{{- $resource = $resource | js.Build . -}}
{{- end -}}
{{- with .Fingerprint -}}
{{- $resource = $resource | fingerprint . -}}
{{- $integrity = $resource.Data.Integrity -}}
{{- end -}}
{{- with $resource.RelPermalink -}}
{{- $src = . -}}
{{- end -}}
{{- end -}}
{{- /* Script form string */ -}}
{{- if .Content | and .Path -}}
{{- $src = (resources.FromString .Path .Content).RelPermalink -}}
{{- end -}}
{{- /* Script attributes */ -}}
{{- if $src -}}
{{- $attrs := printf `src="%v"` $src -}}
{{- if .Crossorigin -}}
{{- $attrs = ` crossorigin="anonymous"` | add $attrs -}}
@@ -42,5 +48,9 @@
{{- $attrs = add " " . | add $attrs -}}
{{- end -}}
<script {{ $attrs | safeHTMLAttr }}></script>
{{- else -}}
{{- with .Content -}}
<script>{{ . | safeJS }}</script>
{{- end -}}
{{- end -}}
{{- end -}}