All files / scripts rewrite-article-metadata.ts

0% Statements 0/224
0% Branches 0/151
0% Functions 0/34
0% Lines 0/190

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
#!/usr/bin/env -S npx tsx
/**
 * @module Infrastructure/ArticleMetadataRewriter
 * @name In-place rewriter for `news/*.html` title / description metadata
 *
 * Usage:
 *   npx tsx scripts/rewrite-article-metadata.ts --dry-run
 *   npx tsx scripts/rewrite-article-metadata.ts --apply
 *   npx tsx scripts/rewrite-article-metadata.ts --apply --file news/2026-04-23-committeeReports-en.html
 *
 * This is the **Tier B** backfill tool (see `.github/prompts/seo-metadata-contract.md`
 * and the 5-PR plan): for every `news/*.html` already on disk, re-derive
 * a publication-quality `<title>` and `<meta description>` from the
 * article body, then rewrite the downstream OG / Twitter / JSON-LD
 * copies in a single deterministic pass.
 *
 * Unlike the from-source Tier A regenerator (`scripts/render-articles.ts`)
 * this script does **not** need `analysis/daily/$DATE/$SUB/article.md`
 * to exist — it sources the new description from the rendered article's
 * own `<article>` prose, skipping any leading admin-byline paragraphs
 * (`Classification:`, `Brief ID:`, `Prepared by:`, …) via the same
 * {@link ADMIN_FIELD_RE} / {@link ADMIN_FRAGMENT_SPLITTER} helpers that
 * the forward-fix aggregator uses for freshly-generated articles.
 *
 * Design decisions:
 * - **Idempotent by design.** A file whose existing metadata already
 *   passes the contract is untouched (we compare new vs old and skip
 *   writes when equal).
 * - **No external dependencies.** Pure regex + the already-exported
 *   `__test__` helpers from `scripts/render-lib/aggregator.ts`. No
 *   `jsdom` / `cheerio` needed; HTML is touched at byte granularity
 *   so line-endings, whitespace, and every non-targeted tag survive
 *   verbatim.
 * - **Per-language charset budgets** per contract §4: CJK 30-45 title
 *   / 70-120 description; RTL 40-70 title / 120-180 description;
 *   Latin 55-70 title / 140-200 description.
 * - **Safe JSON-LD handling.** The block is parsed with `JSON.parse`,
 *   mutated in-memory, and re-stringified with `JSON.stringify` (no
 *   pretty-printing, matching the original single-line format).
 *
 * @author Hack23 AB (Infrastructure Team)
 * @license Apache-2.0
 */
 
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
 
import { __test__ } from './render-lib/aggregator.js';
 
const {
  ADMIN_FIELD_RE,
  ADMIN_FRAGMENT_SPLITTER,
  truncateToSentenceBoundary,
  cleanArticleTitle,
  titleFromBluf,
} = __test__;
 
const __filename = fileURLToPath(import.meta.url);
const ROOT_DIR = path.resolve(path.dirname(__filename), '..');
const NEWS_DIR = path.join(ROOT_DIR, 'news');
 
// ---------------------------------------------------------------------------
// Per-language metadata budgets (per `seo-metadata-contract.md` §4).
// ---------------------------------------------------------------------------
 
interface LangBudget {
  readonly titleMin: number;
  readonly titleMax: number;
  readonly descSoftMin: number; // truncator soft minimum
  readonly descHardMax: number; // truncator hard maximum
  /** Minimum acceptable description length after rewrite (contract §4). */
  readonly descMin: number;
}
 
const LATIN_BUDGET: LangBudget = {
  titleMin: 30,
  titleMax: 75,
  descSoftMin: 140,
  descHardMax: 200,
  descMin: 70,
};
 
const CJK_BUDGET: LangBudget = {
  titleMin: 20,
  titleMax: 55,
  descSoftMin: 70,
  descHardMax: 120,
  descMin: 40,
};
 
const RTL_BUDGET: LangBudget = {
  titleMin: 30,
  titleMax: 85,
  descSoftMin: 120,
  descHardMax: 180,
  descMin: 60,
};
 
const LANG_BUDGETS: Record<string, LangBudget> = {
  en: LATIN_BUDGET,
  sv: LATIN_BUDGET,
  da: LATIN_BUDGET,
  no: LATIN_BUDGET,
  nb: LATIN_BUDGET,
  fi: LATIN_BUDGET,
  de: LATIN_BUDGET,
  fr: LATIN_BUDGET,
  es: LATIN_BUDGET,
  nl: LATIN_BUDGET,
  ar: RTL_BUDGET,
  he: RTL_BUDGET,
  ja: CJK_BUDGET,
  ko: CJK_BUDGET,
  zh: CJK_BUDGET,
};
 
// ---------------------------------------------------------------------------
// HTML parse / rewrite helpers (regex-based, byte-safe)
// ---------------------------------------------------------------------------
 
const META_REGEXES = {
  title: /<title>([\s\S]*?)<\/title>/i,
  metaDescription: /(<meta\s+name="description"\s+content=")([^"]*)(")/i,
  ogTitle: /(<meta\s+property="og:title"\s+content=")([^"]*)(")/i,
  ogDescription: /(<meta\s+property="og:description"\s+content=")([^"]*)(")/i,
  twitterTitle: /(<meta\s+name="twitter:title"\s+content=")([^"]*)(")/i,
  twitterDescription: /(<meta\s+name="twitter:description"\s+content=")([^"]*)(")/i,
  ogImageAlt: /(<meta\s+property="og:image:alt"\s+content=")([^"]*)(")/i,
  twitterImageAlt: /(<meta\s+name="twitter:image:alt"\s+content=")([^"]*)(")/i,
  jsonLd: /<script type="application\/ld\+json">([\s\S]*?)<\/script>/i,
  article: /<article\b[^>]*>([\s\S]*?)<\/article>/i,
  htmlLang: /<html[^>]*\blang="([^"]+)"/i,
} as const;
 
function htmlEscape(s: string): string {
  return s
    .replace(/&/g, '&amp;')
    .replace(/"/g, '&quot;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;');
}
 
function htmlUnescape(s: string): string {
  return s
    .replace(/&amp;/g, '&')
    .replace(/&quot;/g, '"')
    .replace(/&#39;/g, "'")
    .replace(/&apos;/g, "'")
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&nbsp;/g, ' ');
}
 
/** Strip inline HTML tags and collapse whitespace to produce plain prose. */
function stripTagsToText(fragment: string): string {
  return fragment
    .replace(/<br\s*\/?>/gi, ' ')
    .replace(/<[^>]+>/g, '')
    .replace(/\s+/g, ' ')
    .trim();
}
 
/** Decode common HTML entities used in our articles (numeric + named). */
function decodeEntities(s: string): string {
  let out = htmlUnescape(s);
  // Numeric character references — e.g. &#8212; for em-dash.
  out = out.replace(/&#(\d+);/g, (_, n: string) => String.fromCodePoint(Number(n)));
  out = out.replace(/&#x([0-9a-f]+);/gi, (_, n: string) => String.fromCodePoint(parseInt(n, 16)));
  return out;
}
 
/**
 * Extract the first prose paragraph from the article body, skipping
 * admin-byline blocks (detected via {@link ADMIN_FIELD_RE} /
 * {@link ADMIN_FRAGMENT_SPLITTER}), generic filler copy, tables,
 * figures, code blocks, and Mermaid diagrams. Returns `null` when no
 * usable prose is found.
 */
function extractBestDescription(articleHtml: string): string | null {
  // Remove structural noise that would otherwise be matched as prose.
  const scrubbed = articleHtml
    .replace(/<pre class="mermaid"[^>]*>[\s\S]*?<\/pre>/gi, '')
    .replace(/<pre\b[^>]*>[\s\S]*?<\/pre>/gi, '')
    .replace(/<code\b[^>]*>[\s\S]*?<\/code>/gi, '')
    .replace(/<table\b[^>]*>[\s\S]*?<\/table>/gi, '')
    .replace(/<figure\b[^>]*>[\s\S]*?<\/figure>/gi, '')
    .replace(/<nav\b[^>]*>[\s\S]*?<\/nav>/gi, '')
    .replace(/<aside\b[^>]*>[\s\S]*?<\/aside>/gi, '')
    .replace(/<header\b[^>]*>[\s\S]*?<\/header>/gi, '');
 
  // Iterate every <p>…</p> in order, returning the first whose plain
  // text is non-admin, non-banned, and non-trivial (≥ 40 chars).
  const re = /<p\b[^>]*>([\s\S]*?)<\/p>/gi;
  let m: RegExpExecArray | null;
  while ((m = re.exec(scrubbed)) !== null) {
    const raw = m[1] ?? '';
    const text = decodeEntities(stripTagsToText(raw));
    if (text.length < 40) continue;
 
    // Strip leading emphasis markers and boilerplate prefixes.
    const cleaned = text.replace(/^\s*(?:🎯|BLUF[::]?)\s*/i, '').trim();
 
    // Skip banned filler phrases: "… — AI-generated political intelligence …"
    // and "Executive Brief — …" which appear as the `<p class="lede">`
    // site-tagline in some older templates.
    if (BANNED_PHRASES.some((rx) => rx.test(cleaned))) continue;
 
    // If every structural fragment matches ADMIN_FIELD_RE, skip — it's
    // an admin byline that leaked into a <p>.
    const fragments = cleaned.split(ADMIN_FRAGMENT_SPLITTER).filter(Boolean);
    const allAdmin = fragments.length > 0 && fragments.every((f) => ADMIN_FIELD_RE.test(f.trim()));
    if (allAdmin) continue;
 
    return cleaned;
  }
  return null;
}
 
/**
 * Strip admin-byline fragments from an existing description string.
 * Returns the remaining prose, or `null` when every fragment is admin.
 */
function stripAdminFromDescription(description: string): string | null {
  const fragments = description.split(ADMIN_FRAGMENT_SPLITTER).filter(Boolean);
  const prose = fragments.filter((f) => !ADMIN_FIELD_RE.test(f.trim())).map((f) => f.trim());
  if (prose.length === 0) return null;
  const joined = prose.join(' ').replace(/\s+/g, ' ').trim();
  return joined.length > 0 ? joined : null;
}
 
/**
 * Parse the article's `lang` attribute to pick the right budget.
 * Falls back to Latin when the lang is unknown (safe default).
 */
function resolveBudget(html: string, filename: string): LangBudget {
  const m = html.match(META_REGEXES.htmlLang);
  let lang = (m?.[1] ?? '').split('-')[0]!.toLowerCase();
  if (!lang) {
    // Filename fallback: `2026-04-23-committeeReports-en.html` → `en`.
    const fm = filename.match(/-(en|sv|da|no|nb|fi|de|fr|es|nl|ar|he|ja|ko|zh)\.html$/);
    lang = fm?.[1] ?? 'en';
  }
  return LANG_BUDGETS[lang] ?? LATIN_BUDGET;
}
 
// ---------------------------------------------------------------------------
// Contract checker (subset sufficient for rewrite decisions)
// ---------------------------------------------------------------------------
 
interface ViolationSet {
  adminInDescription: boolean;
  brandInDocTitle: boolean;
  doubleBrandOgOrTwitter: boolean;
  executiveBriefPrefix: boolean;
  isoDateInTitle: boolean;
  descriptionTooShort: boolean;
  descriptionTooLong: boolean;
  titleTooLong: boolean;
  midWordDescriptionCut: boolean;
  genericFiller: boolean;
  bannedPhraseInDescription: boolean;
}
 
const BANNED_PHRASES: readonly RegExp[] = [
  /AI[- ]generated\s+political\s+intelligence/i,
  /Executive\s+Brief\s*[—-]/i,
  /\bAdmiralty\b/i,
];
 
function detectViolations(
  titleText: string,
  description: string,
  ogTitleText: string,
  twitterTitleText: string,
  budget: LangBudget,
): ViolationSet {
  const adminInDescription = (() => {
    const fragments = description.split(ADMIN_FRAGMENT_SPLITTER).filter(Boolean);
    return fragments.some((f) => ADMIN_FIELD_RE.test(f.trim()));
  })();
 
  const isoDateInTitle = /\b\d{4}-\d{2}-\d{2}\b/.test(titleText);
  const executiveBriefPrefix = /^\s*Executive Brief\s*[—-]/i.test(titleText) ||
    /^\s*Realtime Monitor\s*[—-]/i.test(titleText);
  const brandInDocTitle = /\bRiksdagsmonitor\b/i.test(titleText);
  const doubleBrandOgOrTwitter =
    /Riksdagsmonitor.*Riksdagsmonitor/i.test(ogTitleText) ||
    /Riksdagsmonitor.*Riksdagsmonitor/i.test(twitterTitleText);
 
  const descriptionTooShort = description.trim().length < budget.descMin;
  // Only flag "too long" when meaningfully over budget — a 207-char
  // Latin description is SEO-fine (Google's display floor is ~155 and
  // the ceiling is flexible); only ≥ 30-char overflow is worth
  // actively rewriting.
  const descriptionTooLong = description.trim().length > budget.descHardMax + 30;
  // Likewise titles: the 75-char budget is the *display-safe* ceiling,
  // but 80-120 char titles still parse fine in search / social. Only
  // rewrite titles when they blow past 120 chars or carry a structural
  // problem (ISO date, boilerplate prefix, double-brand).
  const titleTooLong = titleText.trim().length > budget.titleMax + 50;
 
  // Mid-word cut heuristic: description ends with a lowercase letter
  // (no punctuation) and was probably truncated by the old blind
  // 300-char slicer. Latin-script only; CJK/RTL have their own rules.
  const midWordDescriptionCut = /[a-zåäöøæéèüñç]$/i.test(description.trim()) &&
    description.length >= 120;
 
  const genericFiller = /AI[- ]generated\s+political\s+intelligence/i.test(description);
  const bannedPhraseInDescription = BANNED_PHRASES.some((re) => re.test(description));
 
  return {
    adminInDescription,
    brandInDocTitle,
    doubleBrandOgOrTwitter,
    executiveBriefPrefix,
    isoDateInTitle,
    descriptionTooShort,
    descriptionTooLong,
    titleTooLong,
    midWordDescriptionCut,
    genericFiller,
    bannedPhraseInDescription,
  };
}
 
function needsRewrite(v: ViolationSet): boolean {
  return (
    v.adminInDescription ||
    v.doubleBrandOgOrTwitter ||
    v.executiveBriefPrefix ||
    v.isoDateInTitle ||
    v.descriptionTooLong ||
    v.titleTooLong ||
    v.midWordDescriptionCut ||
    v.genericFiller ||
    v.bannedPhraseInDescription
  );
}
 
// ---------------------------------------------------------------------------
// Single-file rewrite pipeline
// ---------------------------------------------------------------------------
 
interface RewriteOutcome {
  readonly file: string;
  readonly changed: boolean;
  readonly reasons: readonly string[];
  readonly beforeTitle: string;
  readonly afterTitle: string;
  readonly beforeDescription: string;
  readonly afterDescription: string;
}
 
function rewriteOne(filePath: string): { outcome: RewriteOutcome; nextHtml: string | null } {
  const html = fs.readFileSync(filePath, 'utf8');
  const filename = path.basename(filePath);
 
  const budget = resolveBudget(html, filename);
 
  // --- parse current values -------------------------------------------------
  const docTitleRaw = html.match(META_REGEXES.title)?.[1]?.trim() ?? '';
  const docTitleText = decodeEntities(docTitleRaw);
 
  const descRaw = html.match(META_REGEXES.metaDescription)?.[2] ?? '';
  const descText = decodeEntities(descRaw);
 
  const ogDescRaw = html.match(META_REGEXES.ogDescription)?.[2] ?? '';
  const ogDescText = decodeEntities(ogDescRaw);
 
  const ogTitleRaw = html.match(META_REGEXES.ogTitle)?.[2] ?? '';
  const ogTitleText = decodeEntities(ogTitleRaw);
 
  const twTitleRaw = html.match(META_REGEXES.twitterTitle)?.[2] ?? '';
  const twTitleText = decodeEntities(twTitleRaw);
 
  // Pick the richer description to use as the "current" one (the
  // chrome template keeps these in sync, but a buggy generator may
  // have drifted them).
  const richestCurrentDescription =
    [descText, ogDescText].sort((a, b) => b.length - a.length)[0] ?? '';
 
  // --- violations -----------------------------------------------------------
  const violations = detectViolations(
    docTitleText,
    richestCurrentDescription,
    ogTitleText,
    twTitleText,
    budget,
  );
 
  // Fast-path: nothing to do.
  if (!needsRewrite(violations) && !violations.descriptionTooShort) {
    return {
      outcome: {
        file: filePath,
        changed: false,
        reasons: [],
        beforeTitle: docTitleText,
        afterTitle: docTitleText,
        beforeDescription: richestCurrentDescription,
        afterDescription: richestCurrentDescription,
      },
      nextHtml: null,
    };
  }
 
  const reasons: string[] = [];
  for (const [k, v] of Object.entries(violations)) if (v) reasons.push(k);
 
  // --- derive new description ----------------------------------------------
  let newDescription = richestCurrentDescription;
 
  // Strategy 1: strip admin byline from existing description when present.
  if (violations.adminInDescription || violations.bannedPhraseInDescription || violations.genericFiller) {
    const stripped = stripAdminFromDescription(richestCurrentDescription);
    if (stripped && stripped.length >= 40) newDescription = stripped;
    else newDescription = ''; // force fallback to article body
  }
 
  // Strategy 2: if still no good description OR it's too short / too long
  // / truncated mid-word, extract from article body.
  const needsNewFromBody =
    newDescription.length < budget.descMin ||
    newDescription.length > budget.descHardMax ||
    violations.midWordDescriptionCut ||
    violations.genericFiller;
 
  if (needsNewFromBody) {
    const articleBlock = html.match(META_REGEXES.article)?.[1] ?? '';
    const fromBody = extractBestDescription(articleBlock);
    if (fromBody && fromBody.length >= budget.descMin) {
      newDescription = fromBody;
    } else if (fromBody && fromBody.length >= 40) {
      // Even a short body prose is better than admin leakage or generic
      // filler; take it and let the truncator apply the language budget.
      newDescription = fromBody;
    }
  }
 
  // Apply sentence-aware truncation to the language budget.
  newDescription = truncateToSentenceBoundary(
    newDescription,
    budget.descSoftMin,
    budget.descHardMax,
  ).trim();
 
  if (newDescription.length === 0) {
    // Pathological case — nothing usable. Leave file untouched.
    return {
      outcome: {
        file: filePath,
        changed: false,
        reasons: [...reasons, 'no-usable-prose'],
        beforeTitle: docTitleText,
        afterTitle: docTitleText,
        beforeDescription: richestCurrentDescription,
        afterDescription: richestCurrentDescription,
      },
      nextHtml: null,
    };
  }
 
  // --- derive new title -----------------------------------------------------
  // Start from the doc title but strip the trailing brand chrome.
  const titleWithoutBrand = docTitleText.replace(/\s*[—\-|]\s*Riksdagsmonitor\s*$/i, '').trim();
 
  let newTitle = titleWithoutBrand;
  const titleHasStructuralIssue =
    violations.isoDateInTitle ||
    violations.executiveBriefPrefix ||
    violations.titleTooLong ||
    violations.doubleBrandOgOrTwitter;
 
  if (titleHasStructuralIssue) {
    const cleaned = cleanArticleTitle(titleWithoutBrand);
    if (cleaned) {
      newTitle = cleaned;
    } else {
      // Fall back to BLUF-style title from the new description.
      const fromBluf = titleFromBluf(newDescription, budget.titleMax);
      if (fromBluf) newTitle = fromBluf;
    }
 
    // Final safeguard: if the title still contains a raw ISO date
    // anywhere (trailing OR embedded), strip every occurrence and
    // tidy trailing connector words that get left behind.
    newTitle = newTitle.replace(/\s*\d{4}-\d{2}-\d{2}(?:\s+\d{1,2}:\d{2})?\s*/g, ' ').replace(/\s+/g, ' ').trim();
    newTitle = newTitle.replace(/[\s,;:]*(?:to|till|bis|à|a|إلى|から|til|–|—|-|:)\s*$/iu, '').trim();
 
    // Only actively truncate when the title is genuinely excessive
    // (> titleMax + 50) — otherwise a 103-char publication-quality
    // headline would be decimated.
    if (newTitle.length > budget.titleMax + 50) {
      const slice = newTitle.slice(0, budget.titleMax);
      const lastSpace = slice.lastIndexOf(' ');
      newTitle = (lastSpace > budget.titleMin ? slice.slice(0, lastSpace) : slice).trim();
    }
 
    if (!newTitle || newTitle.length < 5) {
      // Degenerate — fall back to the (brand-stripped) original rather
      // than emitting an empty title. A 5-char floor tolerates very
      // short CJK headlines (e.g. 11-char `涉及欧盟和外交事务领域`) which
      // are legitimate; it only catches the pathological zero / 1-2
      // char case.
      newTitle = titleWithoutBrand || docTitleText;
    }
  }
  // else: title has no structural issue, keep `titleWithoutBrand` as-is.
 
  // --- derive OG / Twitter variants ----------------------------------------
  // The chrome template decides whether to append ` — Riksdagsmonitor`.
  // We mirror that decision here: append the brand iff the title does
  // not already contain `Riksdagsmonitor` (contract §3.g).
  const brandedTitle = /riksdagsmonitor/i.test(newTitle)
    ? newTitle
    : `${newTitle} — Riksdagsmonitor`;
 
  // --- rewrite the HTML -----------------------------------------------------
  let next = html;
  const escTitle = htmlEscape(newTitle);
  const escBranded = htmlEscape(brandedTitle);
  const escDesc = htmlEscape(newDescription);
 
  next = next.replace(META_REGEXES.title, `<title>${escBranded}</title>`);
  next = next.replace(META_REGEXES.metaDescription, (_m, pre: string, _old: string, post: string) => `${pre}${escDesc}${post}`);
  next = next.replace(META_REGEXES.ogTitle, (_m, pre: string, _old: string, post: string) => `${pre}${escBranded}${post}`);
  next = next.replace(META_REGEXES.ogDescription, (_m, pre: string, _old: string, post: string) => `${pre}${escDesc}${post}`);
  next = next.replace(META_REGEXES.twitterTitle, (_m, pre: string, _old: string, post: string) => `${pre}${escBranded}${post}`);
  next = next.replace(META_REGEXES.twitterDescription, (_m, pre: string, _old: string, post: string) => `${pre}${escDesc}${post}`);
  next = next.replace(META_REGEXES.ogImageAlt, (_m, pre: string, _old: string, post: string) => `${pre}Riksdagsmonitor ${escTitle}${post}`);
  next = next.replace(META_REGEXES.twitterImageAlt, (_m, pre: string, _old: string, post: string) => `${pre}Riksdagsmonitor ${escTitle}${post}`);
 
  // JSON-LD: parse the NewsArticle schema and rewrite `headline`,
  // `description`, and (when present) `alternativeHeadline`. Every
  // NewsArticle emits the same single-line JSON, so we splice by
  // stringifying with the same compact formatting.
  next = next.replace(META_REGEXES.jsonLd, (whole: string, body: string) => {
    let parsed: Record<string, unknown>;
    try {
      parsed = JSON.parse(body.trim()) as Record<string, unknown>;
    } catch {
      return whole; // malformed JSON-LD; leave untouched.
    }
    if (typeof parsed['@type'] === 'string' && /NewsArticle/i.test(parsed['@type'] as string)) {
      parsed.headline = newTitle;
      parsed.description = newDescription;
      if (Object.prototype.hasOwnProperty.call(parsed, 'alternativeHeadline')) {
        parsed.alternativeHeadline = newTitle;
      }
    }
    return `<script type="application/ld+json">${JSON.stringify(parsed)}</script>`;
  });
 
  if (next === html) {
    return {
      outcome: {
        file: filePath,
        changed: false,
        reasons: [...reasons, 'no-change-after-rewrite'],
        beforeTitle: docTitleText,
        afterTitle: docTitleText,
        beforeDescription: richestCurrentDescription,
        afterDescription: richestCurrentDescription,
      },
      nextHtml: null,
    };
  }
 
  return {
    outcome: {
      file: filePath,
      changed: true,
      reasons,
      beforeTitle: docTitleText,
      afterTitle: newTitle,
      beforeDescription: richestCurrentDescription,
      afterDescription: newDescription,
    },
    nextHtml: next,
  };
}
 
// ---------------------------------------------------------------------------
// CLI driver
// ---------------------------------------------------------------------------
 
function parseArgs(argv: readonly string[]): {
  dryRun: boolean;
  apply: boolean;
  singleFile: string | null;
  quiet: boolean;
} {
  let dryRun = false;
  let apply = false;
  let singleFile: string | null = null;
  let quiet = false;
  for (let i = 0; i < argv.length; i += 1) {
    const a = argv[i];
    if (a === '--dry-run') dryRun = true;
    else if (a === '--apply') apply = true;
    else if (a === '--file') singleFile = argv[++i] ?? null;
    else if (a === '--quiet') quiet = true;
  }
  if (!dryRun && !apply) dryRun = true;
  return { dryRun, apply, singleFile, quiet };
}
 
function listNewsFiles(singleFile: string | null): string[] {
  if (singleFile) {
    const abs = path.isAbsolute(singleFile) ? singleFile : path.join(ROOT_DIR, singleFile);
    if (!fs.existsSync(abs)) {
      throw new Error(`File not found: ${abs}`);
    }
    return [abs];
  }
  if (!fs.existsSync(NEWS_DIR)) return [];
  return fs
    .readdirSync(NEWS_DIR)
    .filter((n) => n.endsWith('.html') && !n.startsWith('index') && !n.startsWith('sitemap'))
    .map((n) => path.join(NEWS_DIR, n));
}
 
function main(): void {
  const args = parseArgs(process.argv.slice(2));
  const files = listNewsFiles(args.singleFile);
 
  const started = Date.now();
  let changed = 0;
  let skipped = 0;
  let errors = 0;
  const changeLog: RewriteOutcome[] = [];
 
  for (const f of files) {
    try {
      const { outcome, nextHtml } = rewriteOne(f);
      if (!outcome.changed) {
        skipped += 1;
        continue;
      }
      changed += 1;
      changeLog.push(outcome);
      if (args.apply && nextHtml !== null) fs.writeFileSync(f, nextHtml, 'utf8');
      if (!args.quiet) {
        const suffix = args.apply ? '✏️  ' : '🔎 ';
        console.log(`${suffix}${path.basename(f)}  [${outcome.reasons.join(', ')}]`);
      }
    } catch (e) {
      errors += 1;
      console.error(`❌ ${path.basename(f)}: ${(e as Error).message}`);
    }
  }
 
  const elapsed = ((Date.now() - started) / 1000).toFixed(1);
  const mode = args.apply ? 'APPLIED' : 'DRY-RUN';
  console.log(`\n📊 ${mode}: ${files.length} files scanned, ${changed} changed, ${skipped} unchanged, ${errors} errors in ${elapsed}s`);
 
  // Emit a compact CSV report alongside the files.
  const reportDir = path.join(ROOT_DIR, 'analysis', 'metadata-backfill');
  fs.mkdirSync(reportDir, { recursive: true });
  const reportPath = path.join(reportDir, `rewrite-report-${new Date().toISOString().slice(0, 10)}.csv`);
  const csvEscape = (s: string) => `"${s.replace(/"/g, '""')}"`;
  const rows: string[] = ['file,reasons,before_title,after_title,before_description,after_description'];
  for (const o of changeLog) {
    rows.push(
      [
        csvEscape(path.relative(ROOT_DIR, o.file)),
        csvEscape(o.reasons.join('|')),
        csvEscape(o.beforeTitle),
        csvEscape(o.afterTitle),
        csvEscape(o.beforeDescription),
        csvEscape(o.afterDescription),
      ].join(','),
    );
  }
  fs.writeFileSync(reportPath, rows.join('\n') + '\n', 'utf8');
  console.log(`📝 Report: ${path.relative(ROOT_DIR, reportPath)}`);
 
  if (errors > 0) process.exitCode = 1;
}
 
main();