Skip to content

Commit

Permalink
♻️ Refactor: javascript output for inline scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Lruihao committed Aug 31, 2024
1 parent 9e67e25 commit 1ca9fdb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
1 change: 1 addition & 0 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
12 changes: 9 additions & 3 deletions layouts/partials/assets.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand All @@ -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 -}}
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/init/index.html
Original file line number Diff line number Diff line change
@@ -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" . -}}
Expand Down
60 changes: 35 additions & 25 deletions layouts/partials/plugin/script.html
Original file line number Diff line number Diff line change
@@ -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 -}}
Expand All @@ -42,5 +48,9 @@
{{- $attrs = add " " . | add $attrs -}}
{{- end -}}
<script {{ $attrs | safeHTMLAttr }}></script>
{{- else -}}
{{- with .Content -}}
<script>{{ . | safeJS }}</script>
{{- end -}}
{{- end -}}
{{- end -}}

0 comments on commit 1ca9fdb

Please sign in to comment.