import { escapeHtml } from './cmsHtml.ts';
import type { HeroData, HeroImageVariant, HeroSlide } from './hero.ts';
// The neutral HTML of the hero between the CMS markers, and the preload links
// in the head. The host (SLP Modular CMS) writes the same markup from live
// data; the golden files in contract/host-injection/hero are the contract.
//
// Only the FIRST slide is written (it is the LCP image); the client takes the
// rest from the data block. The picture holds one for portrait
// screens (ratio "tall") and the itself is the landscape one (ratio
// "wide"). Selection is by orientation, because the hero is as high as the
// screen. The preload links repeat exactly the same srcset, sizes and media,
// so the browser starts the very file the picture will pick.
//
// Only WebP is used here: a preload cannot say "AVIF if supported", so a
// second format would be fetched twice. Later slides may use AVIF.
const PORTRAIT = '(orientation: portrait)';
const LANDSCAPE = '(orientation: landscape)';
const SIZES = '100vw';
// The srcset of one ratio and format ("tall" or "wide"; WebP unless said)
export const variantSrcset = (
variants: HeroImageVariant[],
ratio: string,
type = 'image/webp',
): string =>
variants
.filter((v) => v.ratio === ratio && v.type === type)
.sort((a, b) => a.width - b.width)
.map((v) => `${escapeHtml(v.url)} ${v.width}w`)
.join(', ');
const percent = (unit: number): string => `${Number((unit * 100).toFixed(2))}%`;
const focusStyle = (slide: HeroSlide): string => {
const { focus } = slide.image;
return focus
? ` style="object-position:${percent(focus.x)} ${percent(focus.y)}"`
: '';
};
function pictureHtml(slide: HeroSlide): string {
const { image } = slide;
const tall = variantSrcset(image.variants, 'tall');
const wide = variantSrcset(image.variants, 'wide');
const source = tall
? ``
: '';
const img =
`';
return `${source}${img}`;
}
function headingHtml(hero: HeroData): string {
const tagline = hero.tagline
? `${escapeHtml(hero.tagline)} `
: '';
// A new line in the title is a line break
const name = escapeHtml(hero.title).replace(/\r?\n/g, ' ');
return `
`;
}
// Between and ; nothing for no hero
export function heroHtml(hero: HeroData | null): string {
if (!hero) return '';
return pictureHtml(hero.slides[0]) + headingHtml(hero) + buttonsHtml(hero);
}
// Between and in the head: one
// link for portrait screens (when a tall variant exists) and one for the
// others. Without a tall variant a single link without media is enough.
export function heroPreloadHtml(hero: HeroData | null): string {
if (!hero) return '';
const { image } = hero.slides[0];
const tall = variantSrcset(image.variants, 'tall');
const wide = variantSrcset(image.variants, 'wide');
const link = (target: string, media: string): string =>
``;
const wideTarget = wide
? `imagesrcset="${wide}" imagesizes="${SIZES}"`
: `href="${escapeHtml(image.src)}"`;
if (!tall) return link(wideTarget, '');
return (
link(`imagesrcset="${tall}" imagesizes="${SIZES}"`, PORTRAIT) +
link(wideTarget, LANDSCAPE)
);
}