Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Merge style sheets for light and dark themes into a single resource
Browse files Browse the repository at this point in the history
This will avoid having inline style, which is annoying for stricter CSP.
It should improve performances, as there is only a single file to load.
As a bonus, I also included resource fingerprinting, which will avoid breaking
the cache when it's not needed, and adds subresource integrity checksum.
  • Loading branch information
forty committed Oct 23, 2020
1 parent 9e524ff commit db7e625
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
1 change: 1 addition & 0 deletions layouts/partials/dark.css → assets/css/dark.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* dark.css */
--font-color: #eee;
--bg-color: #212121;

Expand Down
3 changes: 2 additions & 1 deletion static/css/light.css → assets/css/light.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
:root {
/* light.css */
--font-color: #333;
--bg-color: #fff;

Expand All @@ -11,7 +12,7 @@

--pre-color: #333;
--pre-bg-color: #f1f1f1;

--bq-color: #ccc;
--hr-color: #ccc;

Expand Down
23 changes: 22 additions & 1 deletion static/css/style.css → assets/css/main.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
{{ $light := resources.Get "css/light.css" }}
{{ $dark := resources.Get "css/dark.css" }}

{{ $light.Content }}

{{ if eq site.Params.style "dark-without-switcher" }}
:root {
{{ $dark.Content }}
}
{{ else if eq site.Params.style "auto-without-switcher" }}
@media (prefers-color-scheme: dark) {
:root {
{{ $dark.Content }}
}
}
{{else }}
[data-theme="dark"] {
{{ $dark.Content }}
}
{{ end }}

/* Basic */
html {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
Expand Down Expand Up @@ -123,7 +144,7 @@ th, td {

/* Code */
pre {
color: var(--pre-color)
color: var(--pre-color);
background-color: var(--pre-bg-color);
padding: 1em;
max-width: 100%;
Expand Down
18 changes: 0 additions & 18 deletions layouts/partials/cssColors.html

This file was deleted.

8 changes: 4 additions & 4 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<link rel="icon" type="image/x-icon" href="{{ "favicon.ico" | absURL }}">
<link rel="apple-touch-icon-precomposed" href="{{ "favicon.png" | absURL }}">

<link rel="stylesheet" href="{{ "css/light.css" | absURL }}?rnd={{ now.Unix }}" />
{{ partial "cssColors.html" }}
<link rel="stylesheet" href="{{ "css/style.css" | absURL }}?rnd={{ now.Unix }}" />
{{ $cssTemplate := resources.Get "css/main.css" }}
{{ $style := $cssTemplate | resources.ExecuteAsTemplate "css/style.css" . | resources.ToCSS | resources.Fingerprint "sha256" }}
<link rel="stylesheet" href="{{ $style.Permalink }}" integrity="{{ $style.Data.Integrity }}">

{{ range .Site.Params.customCSS -}}
<link rel="stylesheet" href="{{ . | absURL }}?rnd={{ now.Unix }}">
Expand All @@ -39,4 +39,4 @@
{{ template "_internal/google_analytics_async.html" . }}
{{ end }}

{{ partial "head-extra.html" . }}
{{ partial "head-extra.html" . }}

0 comments on commit db7e625

Please sign in to comment.