check-blog-readability.mjs 2.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import assert from 'node:assert/strict';
  2. import fs from 'node:fs';
  3. import { execFileSync } from 'node:child_process';
  4. // The user's verified handoff is the preservation baseline for this body-only edit.
  5. const baseline = '9cae31f7ac0c1ef41790436e0454757d79b7783d';
  6. const before = JSON.parse(execFileSync('git', ['show', `${baseline}:src/content/blog/articles.json`], { encoding: 'utf8' }));
  7. const after = JSON.parse(fs.readFileSync('src/content/blog/articles.json', 'utf8'));
  8. const key = a => `${a.locale}/${a.id}`;
  9. const links = body => [...body.matchAll(/\]\(([^)\s]+)\)/g)].map(m => m[1]).sort();
  10. const headings = body => (body.match(/^## /gm) || []).length;
  11. const stats = body => {
  12. const paragraphs = body.split(/\n\s*\n/).filter(p => p.trim() && !/^(?:#|\||[-*] |\d+\. )/.test(p));
  13. const visible = paragraphs.map(p => p.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1').replace(/[*_`]/g, ''));
  14. return { proseBlocks: paragraphs.length, longestVisibleProseBlock: Math.max(0, ...visible.map(p => p.length)), listItems: (body.match(/^(?:[-*]|\d+\.) /gm) || []).length };
  15. };
  16. assert.equal(before.length, 21);
  17. assert.equal(after.length, 21);
  18. assert.deepEqual(after.map(key).sort(), before.map(key).sort());
  19. const report = [];
  20. for (const article of after) {
  21. const original = before.find(a => key(a) === key(article));
  22. const { body: oldBody, ...oldFields } = original;
  23. const { body, ...fields } = article;
  24. assert.deepEqual(fields, oldFields, `${key(article)} changed fields outside body`);
  25. assert.deepEqual(links(body), links(oldBody), `${key(article)} changed link occurrences`);
  26. assert.equal(headings(body), headings(oldBody), `${key(article)} changed H2 section count`);
  27. assert(!/<\/?(?:script|iframe|html|body)\b/i.test(body), 'Unsafe HTML');
  28. report.push({ article: key(article), changed: body !== oldBody, before: stats(oldBody), after: stats(body) });
  29. }
  30. console.log(JSON.stringify({ baseline, changedBodies: report.filter(a => a.changed).length, articles: report }, null, 2));
  31. console.log('PASS: 21-article inventory, body-only scope, exact link occurrences and H2 boundaries. Readability statistics are descriptive; factual qualifiers require editorial review.');