Files
SluijsensandClaude Sonnet 5.5 246ebe5fd6 Translation files, step 0: typed text helper, checks and the baseline of the pages
Adds src/i18n (t, tList, tPlural over typed Dutch JSON, key and parameter
checks), the first text (back to top) as proof, a baseline tool for the
prerendered pages (pnpm baseline:save and baseline:check), a lint warning for
text typed straight into JSX, and docs/translations.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 17:34:18 +01:00

131 lines
4.4 KiB
TypeScript

// Safety net for moving texts into the language files: saves what the
// prerendered pages say (their text, links, labels, meta tags and structured
// data) and later checks that a new build says exactly the same. Styling,
// scripts, asset hashes and generated class names are left out, so only a
// change of wording or content shows up.
//
// Usage (after `pnpm build`):
// node config/htmlBaseline.ts save writes .baseline/<page>.html (ignored by git)
// node config/htmlBaseline.ts check compares dist/client with .baseline
import {
existsSync,
mkdirSync,
readdirSync,
readFileSync,
writeFileSync,
} from 'node:fs';
import { join, relative } from 'node:path';
const DIST = 'dist/client';
const BASELINE = '.baseline';
// What stays: text, links, labels, meta, structured data and the data block
const KEEP_SCRIPT = /type="application\/(?:ld\+json|json)"/;
export function normalizeHtml(html: string): string {
return (
html
.replace(/<!--(?!\/?CMS:)[\s\S]*?-->/g, '')
.replace(/<style[\s\S]*?<\/style>/g, '')
.replace(
/<script\b([^>]*)>([\s\S]*?)<\/script>/g,
(all, attrs: string) => (KEEP_SCRIPT.test(attrs) ? all : ''),
)
.replace(/<noscript>[\s\S]*?<\/noscript>/g, '<noscript></noscript>')
// Only the canonical link is content; the others point to hashed files
.replace(/<link\b[^>]*>/g, (tag) =>
/rel="canonical"/.test(tag) ? tag : '',
)
// Look, not content
.replace(/\s(?:class|style|data-emotion|data-reveal)="[^"]*"/g, '')
.replace(/\s(?:hidden|inert)=""/g, ' $&')
// The hash in /assets/name-Hash.ext
.replace(
/(\/assets\/[\w.-]*?)-[A-Za-z0-9_-]{8}(\.[a-z0-9]+)/g,
'$1$2',
)
.replace(/>\s+</g, '><')
// One tag per line, so a difference is readable
.replace(/></g, '>\n<')
.trim() + '\n'
);
}
function pages(dir: string): string[] {
return readdirSync(dir, { recursive: true, withFileTypes: true })
.filter((entry) => entry.isFile() && entry.name.endsWith('.html'))
.map((entry) => relative(dir, join(entry.parentPath, entry.name)))
.sort();
}
// The first lines that differ, for the message
export function firstDifferences(a: string, b: string, max = 6): string[] {
const left = a.split('\n');
const right = b.split('\n');
const out: string[] = [];
for (
let i = 0;
i < Math.max(left.length, right.length) && out.length < max;
i++
) {
if (left[i] !== right[i]) {
out.push(
` regel ${i + 1}:`,
` basislijn: ${left[i] ?? '(geen)'}`,
` nu: ${right[i] ?? '(geen)'}`,
);
}
}
return out;
}
function save() {
mkdirSync(BASELINE, { recursive: true });
for (const page of pages(DIST)) {
const target = join(BASELINE, page.replace(/[\\/]/g, '__'));
writeFileSync(
target,
normalizeHtml(readFileSync(join(DIST, page), 'utf8')),
);
}
console.log(
`Basislijn opgeslagen: ${pages(DIST).length} pagina's in ${BASELINE}`,
);
}
function check(): boolean {
if (!existsSync(BASELINE)) {
console.error('Geen basislijn; draai eerst `save`.');
return false;
}
let ok = true;
const names = pages(DIST);
for (const page of names) {
const file = join(BASELINE, page.replace(/[\\/]/g, '__'));
if (!existsSync(file)) {
console.error(`${page}: niet in de basislijn`);
ok = false;
continue;
}
const now = normalizeHtml(readFileSync(join(DIST, page), 'utf8'));
const was = readFileSync(file, 'utf8');
if (now !== was) {
ok = false;
console.error(`${page}: tekst verschilt van de basislijn`);
console.error(firstDifferences(was, now).join('\n'));
}
}
if (ok) console.log(`Gelijk aan de basislijn: ${names.length} pagina's`);
return ok;
}
if (process.argv[1]?.endsWith('htmlBaseline.ts')) {
const command = process.argv[2];
if (command === 'save') save();
else if (command === 'check') process.exitCode = check() ? 0 : 1;
else {
console.error('Gebruik: node config/htmlBaseline.ts save|check');
process.exitCode = 2;
}
}