Adds the neutral HTML, the data block helpers and the golden files of the host injection

cmsHtml.ts writes the plain list and banner HTML that sits between the CMS
markers; contract/host-injection holds the inputs, the expected HTML and the
rules the CMS host's C# template must follow. cmsData.ts reads, validates and
writes the cms-data block.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-15 16:05:42 +01:00
co-authored by Claude Sonnet 5.5
parent ed4a19d146
commit 16027f5688
28 changed files with 615 additions and 0 deletions
+113
View File
@@ -0,0 +1,113 @@
import type { PricingBanner, PricingPackage } from '@/content/types';
import { formatPrice } from './formatPrice';
import type { OfferingTopBanner } from './offerings';
import { resolveSiteUrl } from './siteUrl';
// The data island of a page: what the CMS host writes into
// <script type="application/json" id="cms-data"> after the <!--CMS:DATA-->
// marker, exactly in the shape of the public API:
// { "siteUrl": "...", "offerings": [...], "banners": { "top": ..., "bottom": ... } }
// The build writes the same block with the data it baked, so the page always
// has one. Everything here is lenient: a block that cannot be used is ignored
// and the page keeps what it was built with.
export interface CmsData {
// Already checked by resolveSiteUrl
siteUrl?: string;
offerings?: unknown;
// undefined: not in the block; null: no active banner
banners?: { top?: unknown; bottom?: unknown };
}
const isObject = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null && !Array.isArray(value);
export function parseCmsData(text: string | null | undefined): CmsData | null {
if (!text) return null;
let raw: unknown;
try {
raw = JSON.parse(text);
} catch {
return null;
}
if (!isObject(raw)) return null;
const banners = isObject(raw.banners)
? { top: raw.banners.top, bottom: raw.banners.bottom }
: undefined;
return {
siteUrl:
typeof raw.siteUrl === 'string' && raw.siteUrl !== ''
? resolveSiteUrl(raw.siteUrl)
: undefined,
offerings: Array.isArray(raw.offerings) ? raw.offerings : undefined,
banners,
};
}
export const CMS_DATA_ID = 'cms-data';
// The block of the page, or null without a usable one (a preview, a test, a
// host that does not inject)
export function readCmsData(
doc: Pick<Document, 'getElementById'>,
): CmsData | null {
return parseCmsData(doc.getElementById(CMS_DATA_ID)?.textContent);
}
// JSON that cannot end the script element or break a line terminator rule:
// < > & and U+2028/2029 as \u escapes (the host does the same)
export function scriptJson(value: unknown): string {
return JSON.stringify(value)
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/&/g, '\\u0026')
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029');
}
// The mapped packages back in the shape of GET /api/v1/offerings, for the
// block the build bakes (the price as the free text the CMS has)
export function rawOfferings(packages: readonly PricingPackage[]): unknown[] {
return packages.map((pkg, index) => ({
slug: pkg.id,
title: pkg.name,
price: formatPrice(pkg.price).replace(/\u00a0/g, ' '),
features: pkg.features,
displayOrder: index,
...(pkg.featured ? { featured: true } : {}),
...(pkg.description ? { description: pkg.description } : {}),
...(pkg.priceNote ? { priceNote: pkg.priceNote } : {}),
...(pkg.ctaLabel ? { ctaLabel: pkg.ctaLabel } : {}),
}));
}
// A banner back in the shape of GET /api/v1/offerings/banner, or null
export function rawBanner(
banner: PricingBanner | OfferingTopBanner | null,
position: 'top' | 'bottom',
): unknown {
if (!banner) return null;
return {
title: banner.title ?? '',
text: banner.text ?? null,
linkLabel: banner.link?.label ?? null,
linkUrl: banner.link?.url ?? null,
position,
};
}
export function bakedCmsData(
siteUrl: string,
packages: readonly PricingPackage[],
banner: PricingBanner | null,
topBanner: OfferingTopBanner | null,
): CmsData {
return {
siteUrl,
offerings: rawOfferings(packages),
banners: {
top: rawBanner(topBanner, 'top'),
bottom: rawBanner(banner, 'bottom'),
},
};
}