- Add configurable theme system with 4 presets (studio, minimal, editorial, classic) - Theme controls fonts, colors, accent, photo frames, grain, and header dot - Resolve themes from gallery.yaml: string shorthand or full object override - Inject theme as CSS custom properties on <html> element - Dynamic Google Fonts loading based on theme config - OG image route reads accent/background from theme - Refactor Studio overrides into conditional CSS via data-attributes Documentation: - Add docs/gallery-config.md with comprehensive configuration reference - Add docs/theming.md with preset reference and custom theme guide - Slim README to link to detailed docs Fix CSS regression: - Fix passepartout frame overflow:visible breaking next/image fill rendering - Remove duplicate .photo-grid__item block that stripped overflow:hidden Testing: - Add e2e/photo-grid.spec.ts with 3 regression tests: - Grid items render with real dimensions - overflow:hidden CSS guard (catches the exact bug fixed) - Images load with real pixel data (not blurhash) Tooling: - Add scripts/screenshots.ts for automated theme screenshot generation - Handles dev server lifecycle, theme cycling, FadeIn + lazy image handling
368 lines
11 KiB
TypeScript
368 lines
11 KiB
TypeScript
/**
|
|
* Typed configuration loader.
|
|
* Secrets come from env vars; gallery structure comes from content/gallery.yaml.
|
|
*/
|
|
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import yaml from 'js-yaml';
|
|
import { env } from './env';
|
|
|
|
// ── Env helpers (now handled by lib/env.ts via Zod) ───────────
|
|
|
|
// ── Types ──────────────────────────────────────────────────────
|
|
|
|
/** A subpage groups multiple Immich albums under one URL path. */
|
|
export interface SubpageConfig {
|
|
name: string;
|
|
slug: string;
|
|
albumIds: string[];
|
|
/** Optional password — if set, visitors must authenticate to view. */
|
|
password?: string;
|
|
/** Per-subpage grid overrides (merged with global grid config). */
|
|
grid?: Partial<GridConfig>;
|
|
}
|
|
|
|
/** Footer configuration for social links. */
|
|
export interface FooterConfig {
|
|
name?: string;
|
|
instagram?: string;
|
|
email?: string;
|
|
website?: string;
|
|
}
|
|
|
|
/** Theme configuration — controls the visual identity of the gallery. */
|
|
export interface ThemeConfig {
|
|
preset: string;
|
|
accent: string;
|
|
fonts: { heading: string; body: string; caption: string };
|
|
radius: number;
|
|
photoFrame: 'none' | 'passepartout' | 'shadow';
|
|
grain: boolean;
|
|
headerDot: boolean;
|
|
heroStyle: 'split' | 'fullbleed' | 'minimal';
|
|
}
|
|
|
|
// ── Theme Presets ──────────────────────────────────────────────
|
|
|
|
const THEME_PRESETS: Record<string, ThemeConfig> = {
|
|
studio: {
|
|
preset: 'studio',
|
|
accent: '#e60012',
|
|
fonts: { heading: 'Playfair Display', body: 'DM Sans', caption: 'EB Garamond' },
|
|
radius: 0,
|
|
photoFrame: 'passepartout',
|
|
grain: true,
|
|
headerDot: true,
|
|
heroStyle: 'split',
|
|
},
|
|
minimal: {
|
|
preset: 'minimal',
|
|
accent: '#666666',
|
|
fonts: { heading: 'Inter', body: 'Inter', caption: 'JetBrains Mono' },
|
|
radius: 0,
|
|
photoFrame: 'none',
|
|
grain: false,
|
|
headerDot: false,
|
|
heroStyle: 'split',
|
|
},
|
|
editorial: {
|
|
preset: 'editorial',
|
|
accent: '#1a1a1a',
|
|
fonts: { heading: 'Libre Baskerville', body: 'Source Sans 3', caption: 'Source Sans 3' },
|
|
radius: 4,
|
|
photoFrame: 'shadow',
|
|
grain: false,
|
|
headerDot: false,
|
|
heroStyle: 'split',
|
|
},
|
|
classic: {
|
|
preset: 'classic',
|
|
accent: '#8b6914',
|
|
fonts: { heading: 'Cormorant Garamond', body: 'Lato', caption: 'Lato' },
|
|
radius: 8,
|
|
photoFrame: 'none',
|
|
grain: false,
|
|
headerDot: false,
|
|
heroStyle: 'split',
|
|
},
|
|
};
|
|
|
|
/** Raw YAML structure (before validation). */
|
|
interface GalleryYaml {
|
|
hero?: string | string[];
|
|
albums?: string[];
|
|
exifOnHover?: boolean;
|
|
theme?:
|
|
| string
|
|
| {
|
|
preset?: string;
|
|
accent?: string;
|
|
fonts?: { heading?: string; body?: string; caption?: string };
|
|
radius?: number;
|
|
photoFrame?: string;
|
|
grain?: boolean;
|
|
headerDot?: boolean;
|
|
heroStyle?: string;
|
|
};
|
|
grid?: {
|
|
columns?: number;
|
|
gap?: number;
|
|
aspectRatio?: string;
|
|
layout?: string;
|
|
};
|
|
footer?: {
|
|
name?: string;
|
|
instagram?: string;
|
|
email?: string;
|
|
website?: string;
|
|
};
|
|
subpages?: Array<{
|
|
name: string;
|
|
albums: string[];
|
|
password?: string;
|
|
grid?: {
|
|
columns?: number;
|
|
gap?: number;
|
|
aspectRatio?: string;
|
|
layout?: string;
|
|
};
|
|
}>;
|
|
}
|
|
|
|
// ── YAML loading ───────────────────────────────────────────────
|
|
|
|
function loadGalleryYaml(): GalleryYaml {
|
|
const yamlPath = path.join(process.cwd(), 'content', 'gallery.yaml');
|
|
|
|
if (!fs.existsSync(yamlPath)) {
|
|
throw new Error(
|
|
`Gallery config not found: ${yamlPath}\n` +
|
|
`Copy content/gallery.yaml.example to content/gallery.yaml and add your album IDs.`,
|
|
);
|
|
}
|
|
|
|
const raw = fs.readFileSync(yamlPath, 'utf8');
|
|
const data = yaml.load(raw) as GalleryYaml;
|
|
|
|
if (!data || typeof data !== 'object') {
|
|
throw new Error('gallery.yaml is empty or invalid');
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
// ── UUID validation ────────────────────────────────────────────
|
|
|
|
const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
|
|
function validateUuid(id: string, context: string): string {
|
|
const trimmed = id.trim();
|
|
if (!UUID_REGEX.test(trimmed)) {
|
|
throw new Error(`Invalid UUID in ${context}: "${trimmed}"`);
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
// ── Slugify ────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Convert an album name into a URL-friendly slug.
|
|
* "Kloster Chorin" → "kloster-chorin"
|
|
*/
|
|
export function slugify(name: string): string {
|
|
return name
|
|
.toLowerCase()
|
|
.normalize('NFD')
|
|
.replace(/[\u0300-\u036f]/g, '') // strip diacritics (ü→u, é→e)
|
|
.replace(/[^a-z0-9]+/g, '-') // non-alphanumeric → hyphens
|
|
.replace(/^-+|-+$/g, ''); // trim leading/trailing hyphens
|
|
}
|
|
|
|
// ── Config type ────────────────────────────────────────────────
|
|
|
|
/** Grid layout configuration. */
|
|
export interface GridConfig {
|
|
columns: number;
|
|
gap: number;
|
|
aspectRatio: string;
|
|
layout: 'masonry' | 'uniform';
|
|
}
|
|
|
|
export interface AppConfig {
|
|
immich: { apiUrl: string; apiKey: string };
|
|
albums: string[];
|
|
standaloneAlbums: string[];
|
|
subpages: SubpageConfig[];
|
|
siteTitle: string;
|
|
siteSubtitle: string;
|
|
/** One or more hero image asset IDs for the homepage carousel. */
|
|
heroImages: string[];
|
|
/** Show EXIF (camera/lens) on photo grid hover. Default: true. */
|
|
exifOnHover: boolean;
|
|
/** Photo grid layout configuration. */
|
|
grid: GridConfig;
|
|
/** Theme configuration. */
|
|
theme: ThemeConfig;
|
|
/** Footer social links config. */
|
|
footer: FooterConfig | null;
|
|
cacheTtl: number;
|
|
rateLimitRpm: number;
|
|
}
|
|
|
|
// ── Theme resolution ───────────────────────────────────────────
|
|
|
|
const VALID_PHOTO_FRAMES = ['none', 'passepartout', 'shadow'];
|
|
const VALID_HERO_STYLES = ['split', 'fullbleed', 'minimal'];
|
|
|
|
function resolveTheme(raw?: GalleryYaml['theme']): ThemeConfig {
|
|
// No theme key → default preset
|
|
if (!raw) return { ...THEME_PRESETS.studio };
|
|
|
|
// String shorthand: `theme: minimal`
|
|
if (typeof raw === 'string') {
|
|
const preset = THEME_PRESETS[raw];
|
|
if (!preset) {
|
|
throw new Error(
|
|
`Unknown theme preset "${raw}". Valid presets: ${Object.keys(THEME_PRESETS).join(', ')}`,
|
|
);
|
|
}
|
|
return { ...preset };
|
|
}
|
|
|
|
// Object: merge with base preset
|
|
const baseName = raw.preset ?? 'studio';
|
|
const base = THEME_PRESETS[baseName];
|
|
if (!base) {
|
|
throw new Error(
|
|
`Unknown theme preset "${baseName}". Valid presets: ${Object.keys(THEME_PRESETS).join(', ')}`,
|
|
);
|
|
}
|
|
|
|
return {
|
|
preset: baseName,
|
|
accent: raw.accent ?? base.accent,
|
|
fonts: {
|
|
heading: raw.fonts?.heading ?? base.fonts.heading,
|
|
body: raw.fonts?.body ?? base.fonts.body,
|
|
caption: raw.fonts?.caption ?? base.fonts.caption,
|
|
},
|
|
radius: raw.radius ?? base.radius,
|
|
photoFrame: VALID_PHOTO_FRAMES.includes(raw.photoFrame ?? '')
|
|
? (raw.photoFrame as ThemeConfig['photoFrame'])
|
|
: base.photoFrame,
|
|
grain: raw.grain ?? base.grain,
|
|
headerDot: raw.headerDot ?? base.headerDot,
|
|
heroStyle: VALID_HERO_STYLES.includes(raw.heroStyle ?? '')
|
|
? (raw.heroStyle as ThemeConfig['heroStyle'])
|
|
: base.heroStyle,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Build a Google Fonts URL for the theme's fonts.
|
|
* Deduplicates fonts and includes key weights.
|
|
*/
|
|
export function getGoogleFontsUrl(theme: ThemeConfig): string {
|
|
const weights = '300;400;500;600';
|
|
const families = [...new Set([theme.fonts.heading, theme.fonts.body, theme.fonts.caption])];
|
|
const params = families.map((f) => `family=${f.replace(/ /g, '+')}:wght@${weights}`).join('&');
|
|
return `https://fonts.googleapis.com/css2?${params}&display=swap`;
|
|
}
|
|
|
|
// ── Config builder ─────────────────────────────────────────────
|
|
|
|
let _config: AppConfig | null = null;
|
|
|
|
export function getConfig(): AppConfig {
|
|
if (_config) return _config;
|
|
|
|
const apiUrl = env.IMMICH_API_URL;
|
|
const apiKey = env.IMMICH_API_KEY;
|
|
const gallery = loadGalleryYaml();
|
|
|
|
// Resolve theme: string shorthand → preset, object → merged with preset base
|
|
const theme = resolveTheme(gallery.theme);
|
|
|
|
// Parse standalone albums
|
|
const standaloneAlbumIds = (gallery.albums ?? []).map((id) =>
|
|
validateUuid(id, 'gallery.yaml albums'),
|
|
);
|
|
|
|
if (standaloneAlbumIds.length === 0 && (!gallery.subpages || gallery.subpages.length === 0)) {
|
|
throw new Error('gallery.yaml must define at least one album or subpage');
|
|
}
|
|
|
|
// Parse subpages
|
|
const subpages: SubpageConfig[] = (gallery.subpages ?? []).map((sp) => {
|
|
if (!sp.name || !sp.albums || sp.albums.length === 0) {
|
|
throw new Error(
|
|
`Subpage "${sp.name || '(unnamed)'}" must have a name and at least one album`,
|
|
);
|
|
}
|
|
return {
|
|
name: sp.name,
|
|
slug: slugify(sp.name),
|
|
albumIds: sp.albums.map((id) => validateUuid(id, `subpage "${sp.name}"`)),
|
|
password: sp.password,
|
|
...(sp.grid
|
|
? {
|
|
grid: {
|
|
...(sp.grid.columns != null ? { columns: sp.grid.columns } : {}),
|
|
...(sp.grid.gap != null ? { gap: sp.grid.gap } : {}),
|
|
...(sp.grid.aspectRatio != null ? { aspectRatio: sp.grid.aspectRatio } : {}),
|
|
...(sp.grid.layout != null
|
|
? {
|
|
layout: (sp.grid.layout === 'uniform'
|
|
? 'uniform'
|
|
: 'masonry') as GridConfig['layout'],
|
|
}
|
|
: {}),
|
|
},
|
|
}
|
|
: {}),
|
|
};
|
|
});
|
|
|
|
// Collect ALL album IDs (standalone + subpage) — deduplicated
|
|
const subpageAlbumIds = new Set(subpages.flatMap((sp) => sp.albumIds));
|
|
const allAlbumIds = [...new Set([...standaloneAlbumIds, ...subpageAlbumIds])];
|
|
|
|
_config = {
|
|
immich: {
|
|
apiUrl: `${apiUrl}/api`,
|
|
apiKey,
|
|
},
|
|
albums: allAlbumIds,
|
|
standaloneAlbums: standaloneAlbumIds.filter((id) => !subpageAlbumIds.has(id)),
|
|
subpages,
|
|
siteTitle: env.SITE_TITLE,
|
|
siteSubtitle: env.SITE_SUBTITLE,
|
|
heroImages: gallery.hero
|
|
? (Array.isArray(gallery.hero) ? gallery.hero : [gallery.hero]).map((id) =>
|
|
validateUuid(id, 'gallery.yaml hero'),
|
|
)
|
|
: [],
|
|
exifOnHover: gallery.exifOnHover !== false,
|
|
grid: {
|
|
columns: gallery.grid?.columns ?? 3,
|
|
gap: gallery.grid?.gap ?? 12,
|
|
aspectRatio: gallery.grid?.aspectRatio ?? '1',
|
|
layout: (gallery.grid?.layout === 'uniform' ? 'uniform' : 'masonry') as GridConfig['layout'],
|
|
},
|
|
theme,
|
|
footer: gallery.footer
|
|
? {
|
|
name: gallery.footer.name,
|
|
instagram: gallery.footer.instagram,
|
|
email: gallery.footer.email,
|
|
website: gallery.footer.website,
|
|
}
|
|
: null,
|
|
cacheTtl: env.CACHE_TTL * 1000,
|
|
rateLimitRpm: env.RATE_LIMIT_RPM,
|
|
};
|
|
|
|
return _config;
|
|
}
|