Files
FixIt/layouts/_partials/plugin/style.html
T
Cell 76f809794f refactor(assets): simplify template pipeline and resource handling
- Simplify `resources.Get` calls to string paths in script pipeline
  callers (plugin/script.html handles resources.Get internally)
- Refactor js-build.html to use $defaultOptions merge pattern matching
  style.html's ToCSS handling for better readability
- Refactor style.html ToCSS with $defaultOptions merge pattern and
  automatic targetPath generation from resource name
- Remove redundant custom $options dicts for targetPath that match
  the new default behavior
- Update partial docs for script.html and style.html
2026-05-29 21:55:09 +08:00

80 lines
3.3 KiB
HTML

{{- /*
Stylesheet tag renderer.
- Accepts direct <link> HTML or builds CSS resources from local paths/content.
- Supports template execution, optional toCSS, minify/fingerprint, and preload mode.
@param {String} .Source stylesheet URL/path or Hugo resource; if string starts with "<link" it's treated as raw HTML
@param {String} [.Content] inline style 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 {Object|Boolean} [.ToCSS] true for defaults, dict for custom toCSS options, false/omit to skip toCSS
@param {Boolean} [.Minify] whether to minify resource
@param {String} [.Fingerprint] fingerprint algorithm
@param {Boolean} [.Crossorigin] whether to add crossorigin=anonymous
@param {Boolean} [.Preload] whether to output preload + noscript fallback
@param {String} [.Integrity] SRI hash value
@param {String} [.Attr] additional attributes
*/ -}}
{{- if strings.HasPrefix .Source "<link" -}}
{{- safeHTML .Source -}}
{{- else -}}
{{- $href := .Source -}}
{{- $integrity := .Integrity -}}
{{- $resource := 0 -}}
{{- if $href | and (not (urls.Parse $href).Host) -}}
{{- $resource = resources.Get $href -}}
{{- end -}}
{{- with .Content -}}
{{- $resource = resources.FromString $.Path . -}}
{{- end -}}
{{- if $resource -}}
{{- $minify := .Minify -}}
{{- with .Template -}}
{{- $resource = resources.ExecuteAsTemplate . $.Context $resource -}}
{{- end -}}
{{- with .ToCSS -}}
{{- $targetPath := replaceRE `\.(template\.)?scss$` ".css" $resource.Name -}}
{{- $targetPath = replace $targetPath "scss" "css" -}}
{{- $defaultOptions := dict
"transpiler" "dartsass"
"enableSourceMap" hugo.IsDevelopment
"outputStyle" (cond hugo.IsDevelopment "expanded" "compressed")
"targetPath" $targetPath
"silenceDeprecations" (slice "import" "global-builtin" "color-functions" "function-units")
-}}
{{- $options := . -}}
{{- if eq . true -}}
{{- $options = dict -}}
{{- end -}}
{{- $options = $options | merge $defaultOptions -}}
{{- $resource = $resource | toCSS $options -}}
{{- $minify = $minify | default hugo.IsProduction -}}
{{- end -}}
{{- if $minify -}}
{{- $resource = $resource | minify -}}
{{- end -}}
{{- with .Fingerprint -}}
{{- $resource = $resource | fingerprint . -}}
{{- $integrity = $resource.Data.Integrity -}}
{{- end -}}
{{- $href = $resource.RelPermalink -}}
{{- end -}}
{{- $attrs := printf `href="%v"` $href -}}
{{- if .Crossorigin -}}
{{- $attrs = ` crossorigin="anonymous"` | add $attrs -}}
{{- end -}}
{{- with $integrity -}}
{{- $attrs = printf ` integrity="%v"` . | add $attrs -}}
{{- end -}}
{{- with .Attr -}}
{{- $attrs = add " " . | add $attrs -}}
{{- end -}}
{{- if .Preload -}}
<link rel="preload" {{ $attrs | safeHTMLAttr }} as="style" onload="this.removeAttribute('onload');this.rel='stylesheet'">
<noscript><link rel="stylesheet" {{ $attrs | safeHTMLAttr }}></noscript>
{{- else -}}
<link rel="stylesheet" {{ $attrs | safeHTMLAttr }}>
{{- end -}}
{{- end -}}