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:
@@ -14,3 +14,6 @@ skills-lock.json
|
||||
|
||||
# Copied package (source: SlpSoftware src/cms-client); keeps its own formatting
|
||||
src/cms-client
|
||||
|
||||
# Golden files of the host injection contract: exact text, never reformatted
|
||||
contract/host-injection/*.expected.html
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
# Host injection contract (ASF site and CMS host)
|
||||
|
||||
The ASF build leaves markers in the prerendered pages; the CMS host fills them from live data. This folder holds the golden files for the two list regions. For every `NAME.input.json` there is a `NAME.expected.html`: the host's template must give exactly that text (the file ends with one newline that is not part of the HTML) for that input. The ASF repo tests its own implementation (`src/lib/cmsHtml.ts`) against the same files; to change the contract, change the code and run `UPDATE_GOLDEN=1 pnpm test src/lib/cmsHtml.test.ts`, then send the new files to the CMS session. The files come from ASF commit: see `git log -1 -- web/contract/host-injection` in the ASF repo.
|
||||
|
||||
## Inputs
|
||||
|
||||
- `offerings-*.input.json`: the answer of `GET /api/v1/offerings` (an array).
|
||||
- `banner-*.input.json`: `{ "position": "top" | "bottom", "answer": <answer of GET /api/v1/offerings/banner?position=...> | null }`; `null` is a 204.
|
||||
|
||||
## Rules for the packages region (`<!--CMS:OFFERINGS-HTML-->`)
|
||||
|
||||
1. Keep a package only when all hold: `slug` is a string matching `^[a-z0-9-]+$`; `title` is a non-empty string after trimming; `features` is a non-empty array of non-empty strings (each trimmed); `price` is readable as whole euros above zero (below); the slug was not used by an earlier kept package (the first one wins). Trim all texts. Any other package is skipped.
|
||||
2. Order by `displayOrder` (a number; missing counts as 0), ties keep the given order.
|
||||
3. Price text to whole euros: trim, then the regex `^\D*?(\d{1,3}(?:\.\d{3})+|\d+)(?:,(?:-|0{1,2}))?\D*$`; group 1 without dots is the amount; it must be a whole number above zero. So "€ 175", "€ 1.250", "175,-", "€ 175,00" are readable; "op aanvraag", "€ 12,50", "€ 0", "175 of 185" are not.
|
||||
4. Output: `<ul class="cms-offerings">` + one `<li>` per package + `</ul>`; nothing at all (empty string) when no package is kept.
|
||||
5. Per package: `<li class="cms-offering" data-slug="SLUG"[ data-featured="true"]>` + `<h3>NAME</h3>` + `<p class="cms-price">€ AMOUNT</p>` + `<ul class="cms-features">` + `<li>FEATURE</li>`... + `</ul>` + `<a href="/?pakket=SLUG#contact">Boek NAME</a>` + `</li>`. `data-featured="true"` only when `featured` is the boolean `true`. AMOUNT uses a dot as thousands separator (1.250). The literal text ` ` is written, not a character. `description`, `priceNote` and `ctaLabel` are not used.
|
||||
6. Escape every text (name, features, slug, amount) as: `&` `&`, `<` `<`, `>` `>`, `"` `"`, `'` `'`.
|
||||
|
||||
## Rules for the banner regions (`<!--CMS:BANNER-TOP-HTML-->`, `<!--CMS:BANNER-BOTTOM-HTML-->`)
|
||||
|
||||
1. `null` answer: empty string. Bottom: the title is required, otherwise empty string. Top: title or text is required, otherwise empty string.
|
||||
2. Output `<aside class="cms-banner">` + the parts that are present, joined by one space, + `</aside>`. Parts: `<strong>TITLE</strong>`, `<span>TEXT</span>`, `<a href="URL">LABEL</a>` (trim all texts; empty means absent).
|
||||
3. The link is written only when label and URL are both present and the URL is safe: it starts with `https://` and parses as a URL, or it starts with one `/` (not `//`, no backslash). Otherwise the link part is left out.
|
||||
4. Escape as above (also in the `href`).
|
||||
|
||||
## Where the region sits
|
||||
|
||||
`<!--CMS:OFFERINGS-HTML-->…<!--/CMS:OFFERINGS-HTML-->` (and the two banner pairs) are written by the build; the host replaces what is between the markers and leaves the markers. See `aidlc-docs/features/asf-cms-host-injection/plan.md` for the other markers (data block, JSON-LD price, inline price).
|
||||
@@ -0,0 +1 @@
|
||||
<aside class="cms-banner"><strong>Kerstactie</strong> <span>Alleen in december</span> <a href="/contact">Boek de actie</a></aside>
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"position": "bottom",
|
||||
"answer": {
|
||||
"title": "Kerstactie",
|
||||
"text": "Alleen in december",
|
||||
"linkLabel": "Boek de actie",
|
||||
"linkUrl": "/contact",
|
||||
"position": "bottom"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<aside class="cms-banner"><strong>Past er niets bij?</strong></aside>
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"position": "bottom",
|
||||
"answer": {
|
||||
"title": "Past er niets bij?",
|
||||
"text": null,
|
||||
"linkLabel": null,
|
||||
"linkUrl": null,
|
||||
"position": "bottom"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<aside class="cms-banner"><strong><img src=x onerror=alert(1)></strong> <span>"quote" & 'single'</span> <a href="https://voorbeeld.nl/?a=1&b=2">Meer</a></aside>
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"position": "bottom",
|
||||
"answer": {
|
||||
"title": "<img src=x onerror=alert(1)>",
|
||||
"text": "\"quote\" & 'single'",
|
||||
"linkLabel": "Meer",
|
||||
"linkUrl": "https://voorbeeld.nl/?a=1&b=2",
|
||||
"position": "bottom"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"position": "bottom",
|
||||
"answer": null
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<aside class="cms-banner"><span>Nog twee plekken in december</span></aside>
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"position": "top",
|
||||
"answer": {
|
||||
"title": "",
|
||||
"text": "Nog twee plekken in december",
|
||||
"linkLabel": null,
|
||||
"linkUrl": null,
|
||||
"position": "top"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<aside class="cms-banner"><strong>Nieuw</strong> <span>x</span></aside>
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"position": "top",
|
||||
"answer": {
|
||||
"title": "Nieuw",
|
||||
"text": "x",
|
||||
"linkLabel": "Hier",
|
||||
"linkUrl": "http://voorbeeld.nl",
|
||||
"position": "top"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<ul class="cms-offerings"><li class="cms-offering" data-slug="newborn" data-featured="true"><h3>Newborn</h3><p class="cms-price">€ 185</p><ul class="cms-features"><li>1-2 uur fotografie</li></ul><a href="/?pakket=newborn#contact">Boek Newborn</a></li><li class="cms-offering" data-slug="gezin"><h3>Gezin</h3><p class="cms-price">€ 1.250</p><ul class="cms-features"><li>Hele dag</li></ul><a href="/?pakket=gezin#contact">Boek Gezin</a></li></ul>
|
||||
@@ -0,0 +1,18 @@
|
||||
[
|
||||
{
|
||||
"slug": "newborn",
|
||||
"title": "Newborn",
|
||||
"price": "€ 185",
|
||||
"features": ["1-2 uur fotografie"],
|
||||
"displayOrder": 0,
|
||||
"featured": true
|
||||
},
|
||||
{
|
||||
"slug": "gezin",
|
||||
"title": "Gezin",
|
||||
"price": "€ 1.250,-",
|
||||
"features": ["Hele dag"],
|
||||
"displayOrder": 1,
|
||||
"featured": false
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
<ul class="cms-offerings"><li class="cms-offering" data-slug="xss"><h3><script>alert("x")</script> & "co"</h3><p class="cms-price">€ 100</p><ul class="cms-features"><li><b>vet</b></li><li>o'brien & zoon</li></ul><a href="/?pakket=xss#contact">Boek <script>alert("x")</script> & "co"</a></li></ul>
|
||||
@@ -0,0 +1,9 @@
|
||||
[
|
||||
{
|
||||
"slug": "xss",
|
||||
"title": "<script>alert(\"x\")</script> & \"co\"",
|
||||
"price": "€ 100",
|
||||
"features": ["<b>vet</b>", "o'brien & zoon"],
|
||||
"displayOrder": 0
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
[]
|
||||
@@ -0,0 +1 @@
|
||||
<ul class="cms-offerings"><li class="cms-offering" data-slug="goed"><h3>Goed</h3><p class="cms-price">€ 100</p><ul class="cms-features"><li>a</li></ul><a href="/?pakket=goed#contact">Boek Goed</a></li></ul>
|
||||
@@ -0,0 +1,58 @@
|
||||
[
|
||||
{
|
||||
"slug": "goed",
|
||||
"title": "Goed",
|
||||
"price": "€ 100",
|
||||
"features": ["a"],
|
||||
"displayOrder": 0
|
||||
},
|
||||
{
|
||||
"slug": "Slecht Slug",
|
||||
"title": "Fout",
|
||||
"price": "€ 100",
|
||||
"features": ["a"],
|
||||
"displayOrder": 1
|
||||
},
|
||||
{
|
||||
"slug": "goed",
|
||||
"title": "Dubbel",
|
||||
"price": "€ 100",
|
||||
"features": ["a"],
|
||||
"displayOrder": 2
|
||||
},
|
||||
{
|
||||
"slug": "aanvraag",
|
||||
"title": "Op aanvraag",
|
||||
"price": "op aanvraag",
|
||||
"features": ["a"],
|
||||
"displayOrder": 3
|
||||
},
|
||||
{
|
||||
"slug": "leeg",
|
||||
"title": "Zonder kenmerken",
|
||||
"price": "€ 100",
|
||||
"features": [],
|
||||
"displayOrder": 4
|
||||
},
|
||||
{
|
||||
"slug": "zonder-titel",
|
||||
"title": "",
|
||||
"price": "€ 100",
|
||||
"features": ["a"],
|
||||
"displayOrder": 5
|
||||
},
|
||||
{
|
||||
"slug": "nul",
|
||||
"title": "Nul",
|
||||
"price": "€ 0",
|
||||
"features": ["a"],
|
||||
"displayOrder": 6
|
||||
},
|
||||
{
|
||||
"slug": "komma",
|
||||
"title": "Komma",
|
||||
"price": "€ 12,50",
|
||||
"features": ["a"],
|
||||
"displayOrder": 7
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1 @@
|
||||
<ul class="cms-offerings"><li class="cms-offering" data-slug="zwangerschap"><h3>Zwangerschap</h3><p class="cms-price">€ 175</p><ul class="cms-features"><li>1 uur fotografie</li><li>Persoonlijke online fotogalerij</li></ul><a href="/?pakket=zwangerschap#contact">Boek Zwangerschap</a></li><li class="cms-offering" data-slug="newborn"><h3>Newborn</h3><p class="cms-price">€ 185</p><ul class="cms-features"><li>1-2 uur fotografie</li></ul><a href="/?pakket=newborn#contact">Boek Newborn</a></li><li class="cms-offering" data-slug="gezin"><h3>Gezin</h3><p class="cms-price">€ 175</p><ul class="cms-features"><li>1 uur fotografie</li><li>Minstens 30 zorgvuldig nabewerkte foto's</li></ul><a href="/?pakket=gezin#contact">Boek Gezin</a></li></ul>
|
||||
@@ -0,0 +1,29 @@
|
||||
[
|
||||
{
|
||||
"slug": "gezin",
|
||||
"title": "Gezin",
|
||||
"price": "€ 175",
|
||||
"features": [
|
||||
"1 uur fotografie",
|
||||
"Minstens 30 zorgvuldig nabewerkte foto's"
|
||||
],
|
||||
"displayOrder": 2,
|
||||
"description": "Een shoot met het hele gezin!",
|
||||
"priceNote": "eenmalig, incl. btw"
|
||||
},
|
||||
{
|
||||
"slug": "zwangerschap",
|
||||
"title": "Zwangerschap",
|
||||
"price": "€ 175",
|
||||
"features": ["1 uur fotografie", "Persoonlijke online fotogalerij"],
|
||||
"displayOrder": 0
|
||||
},
|
||||
{
|
||||
"slug": "newborn",
|
||||
"title": "Newborn",
|
||||
"price": "€ 185",
|
||||
"features": ["1-2 uur fotografie"],
|
||||
"displayOrder": 1,
|
||||
"ctaLabel": "Boek nu"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,134 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
bakedCmsData,
|
||||
parseCmsData,
|
||||
rawBanner,
|
||||
rawOfferings,
|
||||
readCmsData,
|
||||
scriptJson,
|
||||
} from './cmsData';
|
||||
import { mapBanner, mapOfferings, mapTopBanner } from './offerings';
|
||||
import { PRODUCTION_SITE_URL } from './siteUrl';
|
||||
|
||||
const packages = [
|
||||
{
|
||||
id: 'newborn',
|
||||
name: 'Newborn',
|
||||
price: 185,
|
||||
features: ['a', 'b'],
|
||||
featured: true,
|
||||
description: 'd',
|
||||
priceNote: 'n',
|
||||
ctaLabel: 'c',
|
||||
},
|
||||
{ id: 'gezin', name: 'Gezin', price: 1250, features: ['x'] },
|
||||
];
|
||||
|
||||
describe('parseCmsData', () => {
|
||||
it('reads the three parts and checks the address', () => {
|
||||
const data = parseCmsData(
|
||||
JSON.stringify({
|
||||
siteUrl: 'https://test.andreaslop.nl/',
|
||||
offerings: [{ slug: 'x' }],
|
||||
banners: { top: null, bottom: { title: 'T' } },
|
||||
}),
|
||||
);
|
||||
expect(data).toEqual({
|
||||
siteUrl: 'https://test.andreaslop.nl',
|
||||
offerings: [{ slug: 'x' }],
|
||||
banners: { top: null, bottom: { title: 'T' } },
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to production for an address that is not an origin', () => {
|
||||
expect(
|
||||
parseCmsData(JSON.stringify({ siteUrl: 'ftp://x' }))?.siteUrl,
|
||||
).toBe(PRODUCTION_SITE_URL);
|
||||
});
|
||||
|
||||
it('leaves out what is missing or of the wrong kind', () => {
|
||||
expect(
|
||||
parseCmsData(JSON.stringify({ offerings: 'x', siteUrl: '' })),
|
||||
).toEqual({
|
||||
siteUrl: undefined,
|
||||
offerings: undefined,
|
||||
banners: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it.each([null, undefined, '', 'niet json', '[1]', '"x"', 'null'])(
|
||||
'gives null for %s',
|
||||
(text) => expect(parseCmsData(text)).toBeNull(),
|
||||
);
|
||||
});
|
||||
|
||||
describe('readCmsData', () => {
|
||||
it('reads the block of the page', () => {
|
||||
const doc = {
|
||||
getElementById: (id: string) =>
|
||||
id === 'cms-data' ? { textContent: '{"offerings":[]}' } : null,
|
||||
} as unknown as Document;
|
||||
expect(readCmsData(doc)?.offerings).toEqual([]);
|
||||
});
|
||||
|
||||
it('gives null without a block', () => {
|
||||
const doc = { getElementById: () => null } as unknown as Document;
|
||||
expect(readCmsData(doc)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('scriptJson', () => {
|
||||
it('cannot end the script element or contain a line separator', () => {
|
||||
const text = scriptJson({
|
||||
a: '</script><!-- & \u2028 \u2029',
|
||||
});
|
||||
expect(text).not.toMatch(/[<>&\u2028\u2029]/);
|
||||
expect(JSON.parse(text)).toEqual({
|
||||
a: '</script><!-- & \u2028 \u2029',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('the baked block', () => {
|
||||
it('maps back to the same packages and banners', () => {
|
||||
const data = bakedCmsData(
|
||||
'https://andreaslop.nl',
|
||||
packages,
|
||||
{ title: 'Onder', text: 't', link: { label: 'L', url: '/x' } },
|
||||
{ text: 'Boven' },
|
||||
);
|
||||
expect(data.siteUrl).toBe('https://andreaslop.nl');
|
||||
const mapped = mapOfferings(data.offerings);
|
||||
expect(mapped?.packages).toEqual(
|
||||
packages.map((pkg) => ({
|
||||
priceNote: undefined,
|
||||
description: undefined,
|
||||
ctaLabel: undefined,
|
||||
featured: undefined,
|
||||
...pkg,
|
||||
})),
|
||||
);
|
||||
expect(mapBanner(data.banners?.bottom)).toEqual({
|
||||
title: 'Onder',
|
||||
text: 't',
|
||||
link: { label: 'L', url: '/x' },
|
||||
});
|
||||
expect(mapTopBanner(data.banners?.top)).toMatchObject({
|
||||
text: 'Boven',
|
||||
});
|
||||
});
|
||||
|
||||
it('gives null for no banner and keeps the order', () => {
|
||||
expect(rawBanner(null, 'top')).toBeNull();
|
||||
expect(rawBanner({ title: 'Alleen' }, 'bottom')).toEqual({
|
||||
title: 'Alleen',
|
||||
text: null,
|
||||
linkLabel: null,
|
||||
linkUrl: null,
|
||||
position: 'bottom',
|
||||
});
|
||||
expect(
|
||||
rawOfferings(packages).map((raw) => (raw as { slug: string }).slug),
|
||||
).toEqual(['newborn', 'gezin']);
|
||||
});
|
||||
});
|
||||
@@ -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'),
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
bannerHtml,
|
||||
escapeHtml,
|
||||
offeringsHtml,
|
||||
regionMarkers,
|
||||
} from './cmsHtml';
|
||||
import { mapBanner, mapOfferings, mapTopBanner } from './offerings';
|
||||
|
||||
// The contract with the CMS host: for every *.input.json in
|
||||
// contract/host-injection there is an *.expected.html, and the host's C#
|
||||
// template must give exactly the same text for the same input. Regenerate the
|
||||
// expected files after a deliberate change with UPDATE_GOLDEN=1 and send the
|
||||
// new files to the CMS session.
|
||||
const DIR = join(process.cwd(), 'contract', 'host-injection');
|
||||
const inputs = readdirSync(DIR).filter((name) => name.endsWith('.input.json'));
|
||||
|
||||
interface BannerInput {
|
||||
position: 'top' | 'bottom';
|
||||
answer: unknown;
|
||||
}
|
||||
|
||||
function render(name: string, input: unknown): string {
|
||||
if (name.startsWith('offerings-')) {
|
||||
const mapped = mapOfferings(input);
|
||||
return offeringsHtml(mapped ? mapped.packages : []);
|
||||
}
|
||||
const { position, answer } = input as BannerInput;
|
||||
return bannerHtml(
|
||||
position === 'top' ? mapTopBanner(answer) : mapBanner(answer),
|
||||
);
|
||||
}
|
||||
|
||||
describe('host injection golden files', () => {
|
||||
it('has inputs for the packages and for both banners', () => {
|
||||
expect(inputs.length).toBeGreaterThanOrEqual(10);
|
||||
expect(inputs.some((name) => name.startsWith('offerings-'))).toBe(true);
|
||||
expect(inputs.some((name) => name.startsWith('banner-'))).toBe(true);
|
||||
});
|
||||
|
||||
it.each(inputs)('%s gives the expected HTML', (file) => {
|
||||
const name = file.replace('.input.json', '');
|
||||
const input: unknown = JSON.parse(
|
||||
readFileSync(join(DIR, file), 'utf8'),
|
||||
);
|
||||
const html = render(name, input);
|
||||
const expectedFile = join(DIR, `${name}.expected.html`);
|
||||
if (process.env.UPDATE_GOLDEN === '1') {
|
||||
writeFileSync(expectedFile, html + '\n');
|
||||
}
|
||||
expect(html + '\n').toBe(readFileSync(expectedFile, 'utf8'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('offeringsHtml and bannerHtml', () => {
|
||||
it('writes nothing for no package and no banner', () => {
|
||||
expect(offeringsHtml([])).toBe('');
|
||||
expect(bannerHtml(null)).toBe('');
|
||||
});
|
||||
|
||||
it('escapes what comes from the CMS', () => {
|
||||
expect(escapeHtml(`<a href="x">'&'</a>`)).toBe(
|
||||
'<a href="x">'&'</a>',
|
||||
);
|
||||
const html = offeringsHtml([
|
||||
{ id: 'x', name: '<b>', price: 10, features: ['"q"'] },
|
||||
]);
|
||||
expect(html).not.toContain('<b>');
|
||||
expect(html).toContain('<b>');
|
||||
expect(html).toContain('"q"');
|
||||
});
|
||||
|
||||
it('puts a non-breaking space in the price, as an entity', () => {
|
||||
const html = offeringsHtml([
|
||||
{ id: 'x', name: 'X', price: 1250, features: ['a'] },
|
||||
]);
|
||||
expect(html).toContain('€ 1.250');
|
||||
});
|
||||
|
||||
it('wraps a region in its markers', () => {
|
||||
expect(regionMarkers('OFFERINGS-HTML', '<ul></ul>')).toBe(
|
||||
'<!--CMS:OFFERINGS-HTML--><ul></ul><!--/CMS:OFFERINGS-HTML-->',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
import type { PricingBanner, PricingPackage } from '@/content/types';
|
||||
import type { OfferingTopBanner } from './offerings';
|
||||
import { formatPrice } from './formatPrice';
|
||||
|
||||
// The plain, crawlable HTML that sits between the CMS markers in the
|
||||
// prerendered pages. The host (SLP Modular CMS) writes the same markup from
|
||||
// live data with a C# template; the golden files in contract/host-injection
|
||||
// are the contract between the two, so a change here changes them too.
|
||||
// Everything that comes from the CMS is escaped.
|
||||
|
||||
const ESCAPES: Record<string, string> = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": ''',
|
||||
};
|
||||
|
||||
export const escapeHtml = (text: string): string =>
|
||||
text.replace(/[&<>"']/g, (char) => ESCAPES[char]);
|
||||
|
||||
// "€ 185" with a non-breaking space, written as an entity so the golden files
|
||||
// stay readable
|
||||
const priceHtml = (price: number): string =>
|
||||
escapeHtml(formatPrice(price)).replace(/\u00a0/g, ' ');
|
||||
|
||||
function packageHtml(pkg: PricingPackage): string {
|
||||
const features = pkg.features
|
||||
.map((feature) => `<li>${escapeHtml(feature)}</li>`)
|
||||
.join('');
|
||||
const featured = pkg.featured ? ' data-featured="true"' : '';
|
||||
return (
|
||||
`<li class="cms-offering" data-slug="${escapeHtml(pkg.id)}"${featured}>` +
|
||||
`<h3>${escapeHtml(pkg.name)}</h3>` +
|
||||
`<p class="cms-price">${priceHtml(pkg.price)}</p>` +
|
||||
`<ul class="cms-features">${features}</ul>` +
|
||||
`<a href="/?pakket=${escapeHtml(pkg.id)}#contact">Boek ${escapeHtml(pkg.name)}</a>` +
|
||||
'</li>'
|
||||
);
|
||||
}
|
||||
|
||||
// The cards as a plain list; nothing at all for no package. The packages are
|
||||
// the mapped ones (mapOfferings), so order and skipping rules are the same as
|
||||
// the site's.
|
||||
export function offeringsHtml(packages: readonly PricingPackage[]): string {
|
||||
if (packages.length === 0) return '';
|
||||
return `<ul class="cms-offerings">${packages.map(packageHtml).join('')}</ul>`;
|
||||
}
|
||||
|
||||
type AnyBanner = PricingBanner | OfferingTopBanner;
|
||||
|
||||
// A banner as one <aside>; nothing at all for no banner. Title, text and link
|
||||
// appear only when present; the link needs both parts (mapBanner already
|
||||
// dropped a half or unsafe one).
|
||||
export function bannerHtml(banner: AnyBanner | null): string {
|
||||
if (!banner) return '';
|
||||
const title = banner.title
|
||||
? `<strong>${escapeHtml(banner.title)}</strong>`
|
||||
: '';
|
||||
const text = banner.text ? `<span>${escapeHtml(banner.text)}</span>` : '';
|
||||
const link = banner.link
|
||||
? `<a href="${escapeHtml(banner.link.url)}">${escapeHtml(banner.link.label)}</a>`
|
||||
: '';
|
||||
const parts = [title, text, link].filter((part) => part !== '');
|
||||
return `<aside class="cms-banner">${parts.join(' ')}</aside>`;
|
||||
}
|
||||
|
||||
// What sits between an opening and closing marker comment
|
||||
export const regionMarkers = (name: string, inner: string): string =>
|
||||
`<!--CMS:${name}-->${inner}<!--/CMS:${name}-->`;
|
||||
Reference in New Issue
Block a user