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>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { firstDifferences, normalizeHtml } from './htmlBaseline.ts';
|
||||
|
||||
describe('normalizeHtml', () => {
|
||||
it('keeps text, links, labels, meta and structured data', () => {
|
||||
const html =
|
||||
'<html><head><title>Titel</title><meta name="description" content="Tekst"/>' +
|
||||
'<link rel="canonical" href="https://a.nl/"/></head><body>' +
|
||||
'<a href="/over-mij" aria-label="Naar over mij">Over mij</a>' +
|
||||
'<script type="application/ld+json">{"a":1}</script></body></html>';
|
||||
const out = normalizeHtml(html);
|
||||
for (const part of [
|
||||
'<title>Titel</title>',
|
||||
'content="Tekst"',
|
||||
'rel="canonical"',
|
||||
'aria-label="Naar over mij"',
|
||||
'Over mij',
|
||||
'{"a":1}',
|
||||
]) {
|
||||
expect(out).toContain(part);
|
||||
}
|
||||
});
|
||||
|
||||
it('leaves out look and build details', () => {
|
||||
const html =
|
||||
'<style data-emotion="css">.a{}</style><script src="/assets/x-AbCd1234.js"></script>' +
|
||||
'<link rel="stylesheet" href="/assets/y-AbCd1234.css"/><!-- opmerking -->' +
|
||||
'<div class="css-1abc" style="color:red" data-reveal="hidden" id="x">tekst</div>' +
|
||||
'<img src="/assets/foto-AbCd1234.jpg"/><noscript><style>.b{}</style></noscript>';
|
||||
const out = normalizeHtml(html);
|
||||
expect(out).not.toMatch(
|
||||
/css-1abc|color:red|data-reveal|opmerking|\.a\{|stylesheet|AbCd1234.js/,
|
||||
);
|
||||
expect(out).toContain('<div id="x">');
|
||||
expect(out).toContain('/assets/foto.jpg');
|
||||
expect(out).toContain(
|
||||
'<noscript>' + String.fromCharCode(10) + '</noscript>',
|
||||
);
|
||||
});
|
||||
|
||||
it('keeps the CMS markers and the data block', () => {
|
||||
const html =
|
||||
'<!--CMS:DATA--><script type="application/json" id="cms-data">{"x":1}</script>' +
|
||||
'<!--CMS:OFFERINGS-HTML-->a<!--/CMS:OFFERINGS-HTML-->';
|
||||
const out = normalizeHtml(html);
|
||||
expect(out).toContain('<!--CMS:DATA-->');
|
||||
expect(out).toContain('{"x":1}');
|
||||
expect(out).toContain('<!--/CMS:OFFERINGS-HTML-->');
|
||||
});
|
||||
|
||||
it('notices a changed word', () => {
|
||||
expect(normalizeHtml('<p>een</p>')).not.toBe(
|
||||
normalizeHtml('<p>twee</p>'),
|
||||
);
|
||||
});
|
||||
|
||||
it('puts one tag per line and ends with a newline', () => {
|
||||
expect(normalizeHtml('<ul> <li>a</li> <li>b</li> </ul>')).toBe(
|
||||
'<ul>\n<li>a</li>\n<li>b</li>\n</ul>\n',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('firstDifferences', () => {
|
||||
it('shows the lines that differ, at most a few', () => {
|
||||
const out = firstDifferences('a\nb\nc', 'a\nB\nc\nd');
|
||||
expect(out.join('\n')).toContain('regel 2');
|
||||
expect(out.join('\n')).toContain('basislijn: b');
|
||||
expect(out.join('\n')).toContain('nu: B');
|
||||
expect(out.join('\n')).toContain('(geen)');
|
||||
expect(firstDifferences('a', 'a')).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,130 @@
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user