Files
linkspin/tools/stamp-theme-head.cjs
martbost b84eed3be5 Theme switch: auto | light | dark, stored in localStorage ls.theme and on the account, applied before first paint on every page
themehead.js is the one source of the tiny inline head script (and of AUTO_DEFAULT, the one constant Marty decides later: 0 = nothing stored keeps the page's current look, light for Control Room and public pages, dark for the sapphire pages; 1 = follow the device). The script sets data-theme on <html> from ls.theme (auto follows prefers-color-scheme and tracks changes live) and exposes window.LSTheme. tools/stamp-theme-head.cjs writes it into every static page before the first stylesheet with data-theme-base on <html>; blog, leaderboard, what's-new, the badge page, the unsubscribe page and the wall 404 read the module directly. The site CSP allows inline scripts by hash only, so themehead.CSP_HASH joins script-src (the script was silently blocked until then).

Account side: accounts.theme (JSON store and MySQL, alterSafe column), in the public account JSON and /api/me; POST /api/my/profile {theme} rides the existing profile save path and never touches the username. cr-shell.js renders a Light / Dark / Auto segmented control in the topbar of every Control Room screen and paints every .theme-seg on the page (the Profile pane gains an Appearance card with the same control); a click applies at once, saves locally and to the account when signed in; at sign-in the account value wins over the local one, and a local choice is pushed up when the account has none yet. view.html stops reading an undefined --bg so its light variant has a light stage. ?v= tags bumped on every changed stylesheet and script (20261101t / ls20261101t).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-01 10:44:40 -06:00

24 lines
1.6 KiB
JavaScript

// Stamp the theme head script (themehead.js) into every static page under public/, before the first stylesheet, and
// put data-theme-base on the <html> tag (dark for site.css pages, light for cr.css / pub.css pages). Re-runnable: an
// existing copy between the markers is replaced. Run after changing AUTO_DEFAULT in themehead.js:
// node tools/stamp-theme-head.cjs
const fs = require('fs'), path = require('path');
const themehead = require(path.join(__dirname, '..', 'themehead.js'));
const dir = path.join(__dirname, '..', 'public');
const OPEN = '<!--theme-head-->', CLOSE = '<!--/theme-head-->';
let n = 0;
for (const f of fs.readdirSync(dir).filter(f => f.endsWith('.html'))) {
const p = path.join(dir, f);
let html = fs.readFileSync(p, 'utf8');
if (!/<link rel="stylesheet" href="\/assets\/(cr|pub|site)\.css/.test(html)) continue; // verification files, frames
const base = /<link rel="stylesheet" href="\/assets\/site\.css/.test(html) && !/<link rel="stylesheet" href="\/assets\/(cr|pub)\.css/.test(html) ? 'dark' : 'light';
// <html> attribute
html = html.replace(/<html([^>]*?)( data-theme-base="[a-z]+")?>/, (m, a) => '<html' + a + themehead.htmlAttr(base) + '>');
const block = OPEN + themehead.tag() + CLOSE;
if (html.includes(OPEN)) html = html.replace(new RegExp(OPEN + '[\\s\\S]*?' + CLOSE), block);
else html = html.replace(/<link rel="stylesheet"/, block + '\n<link rel="stylesheet"'); // before the first stylesheet (fonts included: the script must run before any CSS)
fs.writeFileSync(p, html);
n++; console.log('stamped', f, base);
}
console.log(n + ' pages');