11const DEFAULT_PUBLIC_ASSET_VERSION = "20260601" ;
22
3- export const THEME_OVERRIDE_SCRIPT = ` <script data-v8s-theme-override>
4- (() => {
5- const theme = new URLSearchParams(window.location.search).get("theme");
6- if (theme !== "light" && theme !== "dark") return;
7-
8- document.documentElement.dataset.theme = theme;
9-
10- const applyThemeImages = () => {
11- document.querySelectorAll('picture source[media*="prefers-color-scheme"][srcset]').forEach((source) => {
12- const image = source.parentElement?.querySelector("img");
13- const candidate =
14- theme === "dark"
15- ? source.getAttribute("srcset")?.split(",")[0]?.trim()?.split(/\\s+/)[0]
16- : image?.getAttribute("src");
17- if (image && candidate) image.src = candidate;
18- });
19- };
20-
21- if (document.readyState === "loading") {
22- document.addEventListener("DOMContentLoaded", applyThemeImages, { once: true });
23- } else {
24- applyThemeImages();
25- }
26- })();
27- </script>` ;
3+ export const THEME_OVERRIDE_SCRIPT = themeOverrideScript ( ) ;
284
295export function normalizeHtmlHead ( html , options = { } ) {
306 const assetVersion = options . assetVersion || DEFAULT_PUBLIC_ASSET_VERSION ;
317 let normalized = normalizePublicAssetVersions ( html , assetVersion ) ;
8+ normalized = replaceInlineThemeOverride ( normalized , assetVersion ) ;
329
3310 if ( ! normalized . includes ( 'rel="icon"' ) ) {
3411 normalized = insertBeforeHeadClose ( normalized , ' <link rel="icon" type="image/svg+xml" href="/favicon.svg">\n' ) ;
@@ -38,15 +15,28 @@ export function normalizeHtmlHead(html, options = {}) {
3815 normalized = insertBeforeHeadClose ( normalized , ' <link rel="apple-touch-icon" href="/apple-touch-icon.png">\n' ) ;
3916 }
4017
41- if ( ! normalized . includes ( "data- v8s-theme-override " ) ) {
42- normalized = insertBeforeFirstStylesheet ( normalized , `${ THEME_OVERRIDE_SCRIPT } \n` ) ;
18+ if ( ! normalized . includes ( "/ v8s-theme.js " ) ) {
19+ normalized = insertBeforeFirstStylesheet ( normalized , `${ themeOverrideScript ( assetVersion ) } \n` ) ;
4320 }
4421
4522 return normalized ;
4623}
4724
4825export function normalizePublicAssetVersions ( html , assetVersion = DEFAULT_PUBLIC_ASSET_VERSION ) {
49- return html . replace ( / ( h r e f = [ " ' ] \/ v 8 s - s t y l e \. c s s ) (?: \? v = \d + ) ? ( [ " ' ] ) / g, `$1?v=${ assetVersion } $2` ) ;
26+ return html
27+ . replace ( / ( h r e f = [ " ' ] \/ v 8 s - s t y l e \. c s s ) (?: \? v = \d + ) ? ( [ " ' ] ) / g, `$1?v=${ assetVersion } $2` )
28+ . replace ( / ( s r c = [ " ' ] \/ v 8 s - t h e m e \. j s ) (?: \? v = \d + ) ? ( [ " ' ] ) / g, `$1?v=${ assetVersion } $2` ) ;
29+ }
30+
31+ export function themeOverrideScript ( assetVersion = DEFAULT_PUBLIC_ASSET_VERSION ) {
32+ return ` <script src="/v8s-theme.js?v=${ assetVersion } "></script>` ;
33+ }
34+
35+ function replaceInlineThemeOverride ( html , assetVersion ) {
36+ return html . replace (
37+ / \s * < s c r i p t d a t a - v 8 s - t h e m e - o v e r r i d e > [ \s \S ] * ?< \/ s c r i p t > \n ? / ,
38+ `\n${ themeOverrideScript ( assetVersion ) } \n`
39+ ) ;
5040}
5141
5242export function insertBeforeHeadClose ( html , insertion ) {
0 commit comments