Skip to content

Commit

Permalink
Incl. extracted inline styles in head
Browse files Browse the repository at this point in the history
  • Loading branch information
salim-b committed Oct 31, 2023
1 parent 6110655 commit b7bbe87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions layouts/partials/_inline-svg.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
{{ end }}
{{ $replacement = print "$1 " (delimit $attributes " ") "$2" }}
{{ $markup = replaceRE $patternSvgTag $replacement $markup 1 }}

{{/* Extract inline styles and write them to page store to be included in head */}}
{{ $inlineStyles := slice }}
{{ range (findRE `<style(\s+[^>]*)?>[\s\S]*?</style>` $markup) }}
{{ $inlineStyles = append (replaceRE `(^<style(\s+[^>]*)?>|</style>$)` "" .) $inlineStyles }}
{{ $markup = replace $markup . "" 1 }}
{{ end }}
{{ page.Store.Add "inlineStyles" $inlineStyles }}
{{ else }}
{{ warnf "Could not find src '%s'." .src }}
{{ end }}
Expand Down
8 changes: 8 additions & 0 deletions layouts/partials/head/inline-svg-styles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- /* Trigger evaluation of shortcodes */ -}}
{{- $noop := .WordCount -}}
{{- /* Include extracted inline SVG styles as proper resources */ -}}
{{- $css := "" -}}
{{- with .Store.Get "inlineStyles" -}}
{{- $css := delimit . "\n" | resources.FromString "inline-svg.css" | resources.Fingerprint "sha512" -}}
<link rel="stylesheet" href="{{ $css.Permalink | relURL }}" integrity="{{ $css.Data.Integrity }}" crossorigin="anonymous">
{{- end }}

0 comments on commit b7bbe87

Please sign in to comment.