// A stand-in for what the CMS host does to a prerendered page: it fills the CMS
// markers from live data (the contract is in aidlc-docs/features/
// asf-cms-host-injection/plan.md and contract/host-injection). The real
// injector is C# in the CMS repo; this one lets the preview server and the
// browser tests show how the site behaves when a host rewrites the HTML, so
// hydration, fail-safe behaviour and layout can be checked without a host.
// It uses the site's own mapping and HTML code, so it proves the page copes
// with what the host writes; the golden files prove the host's template
// matches that code.
import { bannerHtml, offeringsHtml } from '../src/lib/cmsHtml.ts';
import { scriptJson } from '../src/lib/cmsData.ts';
import { formatPrice } from '../src/lib/formatPrice.ts';
import { mapBanner, mapOfferings, mapTopBanner } from '../src/lib/offerings.ts';
export interface LiveData {
// The answers as the public API gives them
offerings: unknown;
banners: { top: unknown; bottom: unknown };
}
export interface InjectOptions {
data: LiveData;
// The address the build was written with and the one to show instead
authoredBaseUrl?: string;
publicBaseUrl?: string;
}
// ld+json that cannot close its script element (same rule as the site's)
const jsonLd = (value: unknown) =>
JSON.stringify(value)
.replace(//g, '\\u003e')
.replace(/&/g, '\\u0026');
const REGION =
/[\s\S]*?/g;
const DATA_BLOCK =
/(?:`,
)
.replace(OFFER_JSON_LD, (match, slug: string, json: string) => {
let parsed: unknown;
try {
parsed = JSON.parse(json);
} catch {
return match;
}
editOffers(parsed, prices.get(slug));
return ``;
})
.replace(
PRICE_SPAN,
(match, open: string, slug: string, close: string) => {
const price = prices.get(slug);
return price === undefined
? match
: `${open}${formatPrice(price)}${close}`;
},
);
const { authoredBaseUrl, publicBaseUrl } = options;
if (authoredBaseUrl && publicBaseUrl && authoredBaseUrl !== publicBaseUrl) {
out = out.split(authoredBaseUrl).join(publicBaseUrl);
}
return out;
}