aboutsummaryrefslogtreecommitdiffstats
path: root/src/hooks.server.js
blob: dca4497757bf59aa189004be99a5c41e64b69692 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
export const handle = (async ({ event, resolve }) => {
	let theme = "dark";

	const newTheme = event.url.searchParams.get("theme");
	const cookieTheme = event.cookies.get("colortheme");

	if (newTheme) {
		theme = newTheme;
	} else if (cookieTheme) {
		theme = cookieTheme;
	}

	if (theme) {
		return await resolve(event, {
			transformPageChunk: ({ html }) => {
                const modifiedHtml = html.replace(/<html[^>]*>/, (match) => {
                    // Ensure the class attribute exists or add it if missing
                    return match.includes('class="')
                        ? match.replace(/class="[^"]*"/, `class="${theme}"`)
                        : match.replace('>', ` class="${theme}">`);
                });
                return modifiedHtml;
            }
		});
	}

	return await resolve(event);
})