Taalbestanden: indeling gelijk aan ASF (common, site, home, era), baseline-check van de prerender en lint-waarschuwingen voor losse tekst, docs/translations.md
Prerender-uitvoer blijft byte-identiek. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
// Safety net for moving texts into the language files: saves what the prerendered page says
|
||||
// (text, links, labels, meta tags, structured data and the CMS data block, if present) 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`, or `vite build && node scripts/prerender.mjs`):
|
||||
// node scripts/htmlBaseline.mjs save writes .baseline/index.html (ignored by git)
|
||||
// node scripts/htmlBaseline.mjs check compares dist/index.html with .baseline (exit code 1 on a difference)
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const DIST_FILE = join('dist', 'index.html');
|
||||
const BASELINE_DIR = '.baseline';
|
||||
const BASELINE_FILE = join(BASELINE_DIR, 'index.html');
|
||||
|
||||
// What stays of the scripts: structured data and data blocks, not code
|
||||
const KEEP_SCRIPT = /type="application\/(?:ld\+json|json)"/;
|
||||
|
||||
export function normalizeHtml(html) {
|
||||
return (
|
||||
html
|
||||
.replace(/<!--(?!\/?CMS:)[\s\S]*?-->/g, '')
|
||||
.replace(/<style[\s\S]*?<\/style>/g, '')
|
||||
.replace(/<script\b([^>]*)>([\s\S]*?)<\/script>/g, (all, attrs) =>
|
||||
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, '')
|
||||
// The hash in /assets/name-Hash.ext
|
||||
.replace(/(\/assets\/[\w.-]*?)-[A-Za-z0-9_-]{8}(\.[a-z0-9]+)/g, '$1$2')
|
||||
// Line breaks and indentation inside a tag are formatting
|
||||
.replace(/<[a-zA-Z][^>]*>/g, (tag) => tag.replace(/\s+/g, ' ').replace(/\s+(\/?>)$/, '$1'))
|
||||
.replace(/>\s+</g, '><')
|
||||
// One tag per line, so a difference is readable
|
||||
.replace(/></g, '>\n<')
|
||||
.trim() + '\n'
|
||||
);
|
||||
}
|
||||
|
||||
// The first lines that differ, for the message
|
||||
export function firstDifferences(a, b, max = 6) {
|
||||
const left = a.split('\n');
|
||||
const right = b.split('\n');
|
||||
const out = [];
|
||||
for (let i = 0; i < Math.max(left.length, right.length) && out.length < max * 3; i++) {
|
||||
if (left[i] !== right[i]) {
|
||||
out.push(
|
||||
` regel ${i + 1}:`,
|
||||
` basislijn: ${left[i] ?? '(geen)'}`,
|
||||
` nu: ${right[i] ?? '(geen)'}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function readDist() {
|
||||
if (!existsSync(DIST_FILE)) {
|
||||
console.error(`${DIST_FILE} bestaat niet; draai eerst \`pnpm build\`.`);
|
||||
return null;
|
||||
}
|
||||
return normalizeHtml(readFileSync(DIST_FILE, 'utf8'));
|
||||
}
|
||||
|
||||
function save() {
|
||||
const now = readDist();
|
||||
if (now === null) return false;
|
||||
mkdirSync(BASELINE_DIR, { recursive: true });
|
||||
writeFileSync(BASELINE_FILE, now);
|
||||
console.log(`Basislijn opgeslagen in ${BASELINE_FILE}`);
|
||||
return true;
|
||||
}
|
||||
|
||||
function check() {
|
||||
if (!existsSync(BASELINE_FILE)) {
|
||||
console.error('Geen basislijn; draai eerst `pnpm baseline:save`.');
|
||||
return false;
|
||||
}
|
||||
const now = readDist();
|
||||
if (now === null) return false;
|
||||
const was = readFileSync(BASELINE_FILE, 'utf8');
|
||||
if (now !== was) {
|
||||
console.error('index.html: tekst verschilt van de basislijn');
|
||||
console.error(firstDifferences(was, now).join('\n'));
|
||||
return false;
|
||||
}
|
||||
console.log('Gelijk aan de basislijn: index.html');
|
||||
return true;
|
||||
}
|
||||
|
||||
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
|
||||
const command = process.argv[2];
|
||||
if (command === 'save') process.exitCode = save() ? 0 : 1;
|
||||
else if (command === 'check') process.exitCode = check() ? 0 : 1;
|
||||
else {
|
||||
console.error('Gebruik: node scripts/htmlBaseline.mjs save|check');
|
||||
process.exitCode = 2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { firstDifferences, normalizeHtml } from './htmlBaseline.mjs';
|
||||
|
||||
describe('normalizeHtml', () => {
|
||||
it('keeps text, links, labels, meta, structured data and the data block', () => {
|
||||
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>' +
|
||||
'<script type="application/json" id="cms">{"b":2}</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}',
|
||||
'{"b":2}',
|
||||
]) {
|
||||
expect(out).toContain(part);
|
||||
}
|
||||
});
|
||||
|
||||
it('leaves out look and build details', () => {
|
||||
const html =
|
||||
'<style>.a{}</style><script type="module" src="/assets/x-AbCd1234.js"></script>' +
|
||||
'<link rel="stylesheet" href="/assets/y-AbCd1234.css"/><!-- opmerking -->' +
|
||||
'<div class="css-1abc" style="color:red" 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|opmerking|\.a\{|stylesheet|AbCd1234\.js/);
|
||||
expect(out).toContain('<div id="x">');
|
||||
expect(out).toContain('/assets/foto.jpg');
|
||||
expect(out).toContain('<noscript>\n</noscript>');
|
||||
});
|
||||
|
||||
it('ignores formatting but notices a changed text', () => {
|
||||
const a = normalizeHtml('<p>Hallo</p>\n\n <p>wereld</p>');
|
||||
const b = normalizeHtml('<p>Hallo</p><p>wereld</p>');
|
||||
expect(a).toBe(b);
|
||||
expect(normalizeHtml('<p>Hallo</p><p>werelden</p>')).not.toBe(a);
|
||||
});
|
||||
});
|
||||
|
||||
describe('firstDifferences', () => {
|
||||
it('reports the lines that differ and nothing for equal input', () => {
|
||||
expect(firstDifferences('a\nb', 'a\nb')).toEqual([]);
|
||||
const out = firstDifferences('a\nb\nc', 'a\nx\nc').join('\n');
|
||||
expect(out).toContain('regel 2');
|
||||
expect(out).toContain('basislijn: b');
|
||||
expect(out).toContain('nu: x');
|
||||
});
|
||||
|
||||
it('limits the number of reported differences', () => {
|
||||
const many = Array.from({ length: 20 }, (_, i) => `r${i}`).join('\n');
|
||||
const other = Array.from({ length: 20 }, (_, i) => `s${i}`).join('\n');
|
||||
expect(firstDifferences(many, other, 2)).toHaveLength(6);
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeHtml tags', () => {
|
||||
it('ignores line breaks and indentation inside a tag', () => {
|
||||
expect(normalizeHtml('<meta\n property="a"\n content="b"\n />')).toBe(
|
||||
normalizeHtml('<meta property="a" content="b"/>'),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user