Files
FixIt/layouts/_partials/plugin/script.html
T
Cell d076ebf2e1 fix(layouts): fix Content+Path code path in style/script plugin partials
- style.html: Content+Path now uses to-css.html for proper SCSS compilation
- script.html: Content+Path now uses js-build.html with Build/Minify/Fingerprint
- Both paths now update integrity hash for SRI support
- script.html: restructured fallback logic to match style.html pattern
2026-06-27 21:57:46 +08:00

88 lines
3.4 KiB
HTML

{{- /*
Script tag renderer.
- Accepts direct script HTML, a pre-built resource, or builds from source path.
- Supports template execution, minify/build/fingerprint pipeline, and extra attrs.
@param {String} [.Source] script source URL/path; if starts with "<script" it's treated as raw HTML
@param {resource.Resource} [.Resource] pre-built resource (skips build pipeline)
@param {String} [.Content] inline content to write as generated resource
@param {String} [.Path] target path for resource created from Content
@param {Object} [.Template] target path for resource created from template execution
@param {Object} [.Context] template context for ExecuteAsTemplate
@param {Boolean} [.Minify] whether to minify resource
@param {Object} [.Build] js.Build options (Build.minify is normalized to .Minify for path semantics)
@param {String} [.Fingerprint] fingerprint algorithm
@param {Boolean} [.Async] whether to add async attribute
@param {Boolean} [.Defer] whether to add defer attribute
@param {Boolean} [.Crossorigin] whether to add crossorigin=anonymous
@param {String} [.Integrity] SRI hash value
@param {String} [.Attr] additional attributes
*/ -}}
{{- if strings.HasPrefix (.Source | default "") "<script" -}}
{{- safeHTML .Source -}}
{{- else -}}
{{- $src := "" -}}
{{- $integrity := .Integrity -}}
{{- /* Resource (pre-built) */ -}}
{{- with .Resource -}}
{{- $src = .RelPermalink -}}
{{- with .Data.Integrity -}}
{{- $integrity = . -}}
{{- end -}}
{{- end -}}
{{- /* Source build */ -}}
{{- if .Source | and (not .Resource) | and (not (urls.Parse .Source).Host) -}}
{{- $resource := resources.Get .Source -}}
{{- with .Template -}}
{{- $resource = resources.ExecuteAsTemplate . $.Context $resource -}}
{{- end -}}
{{- $resource = dict "Resource" $resource "Build" .Build "Minify" .Minify "Fingerprint" .Fingerprint | partial "function/js-build.html" -}}
{{- with .Fingerprint -}}
{{- $integrity = $resource.Data.Integrity -}}
{{- end -}}
{{- with $resource.RelPermalink -}}
{{- $src = . -}}
{{- end -}}
{{- end -}}
{{- /* Script from string */ -}}
{{- if .Content | and .Path -}}
{{- $contentResource := resources.FromString .Path .Content -}}
{{- $contentResource = dict "Resource" $contentResource "Build" .Build "Minify" .Minify "Fingerprint" .Fingerprint | partial "function/js-build.html" -}}
{{- with .Fingerprint -}}
{{- $integrity = $contentResource.Data.Integrity -}}
{{- end -}}
{{- $src = $contentResource.RelPermalink -}}
{{- end -}}
{{- /* Fallback to source */ -}}
{{- $src = $src | default .Source -}}
{{- /* Script attributes */ -}}
{{- if $src -}}
{{- $attrs := printf `src="%v"` $src -}}
{{- if .Crossorigin -}}
{{- $attrs = ` crossorigin="anonymous"` | add $attrs -}}
{{- end -}}
{{- with $integrity -}}
{{- $attrs = printf ` integrity="%v"` . | add $attrs -}}
{{- end -}}
{{- if .Async -}}
{{- $attrs = " async" | add $attrs -}}
{{- end -}}
{{- if .Defer -}}
{{- $attrs = " defer" | add $attrs -}}
{{- end -}}
{{- with .Attr -}}
{{- $attrs = add " " . | add $attrs -}}
{{- end -}}
<script {{ $attrs | safeHTMLAttr }}></script>
{{- else -}}
{{- with .Content -}}
<script{{ with $.Attr }} {{ . | safeHTMLAttr }}{{ end }}>{{ . | safeJS }}</script>
{{- end -}}
{{- end -}}
{{- end -}}