All files / src/utils retrofit-analysis-links.ts

0% Statements 0/118
0% Branches 0/55
0% Functions 0/8
0% Lines 0/107

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Utils/RetrofitAnalysisLinks
 * @description Retroactively injects analysis transparency sections into existing
 * news articles that have matching analysis directories on disk but lack the
 * `<section class="analysis-transparency">` section.
 *
 * This tool scans all articles in `news/`, matches them against available analysis
 * directories under `analysis/daily/{date}/{type}*`, discovers all `.md` analysis
 * files on disk, and injects the rendered section before the `<nav class="article-nav">`
 * element.
 *
 * Usage: npx tsx src/utils/retrofit-analysis-links.ts [--dry-run]
 */
 
import fs from 'node:fs';
import path from 'node:path';
import { NEWS_DIR, ARTICLE_FILENAME_PATTERN } from '../constants/config.js';
import { ALL_LANGUAGES } from '../constants/language-core.js';
import type { LanguageCode } from '../types/index.js';
import { discoverAnalysisFileEntries } from './file-utils.js';
import { renderAnalysisTransparencySection } from '../templates/article-template.js';
 
// ─── Constants ───────────────────────────────────────────────────────────────
 
const ANALYSIS_BASE_DIR = 'analysis/daily';
 
/** Regex to detect analysis transparency section already present */
const ANALYSIS_SECTION_REGEX = /<section\s+class="analysis-transparency"/;
 
/** Regex to match the full analysis transparency section for replacement */
const ANALYSIS_SECTION_FULL_REGEX =
  /\s*<section\s+class="analysis-transparency"[\s\S]*?<\/section>/;
 
/** Injection point — insert the analysis section just before the article-nav */
const INJECTION_REGEX = /(\s*<nav\s+class="article-nav")/;
 
// ─── Analysis directory resolution ──────────────────────────────────────────
 
/**
 * Parse a directory suffix into a numeric priority.
 * Exact match (no suffix) = 0, numeric suffix = its value, runN = N+1000.
 *
 * @param suffixStr - The suffix string from the regex capture, or undefined
 * @returns Numeric priority value
 */
function parseSuffixPriority(suffixStr: string | undefined): number {
  if (!suffixStr) return 0;
  if (suffixStr.startsWith('run')) {
    return parseInt(suffixStr.slice(3), 10) + 1000;
  }
  return parseInt(suffixStr, 10);
}
 
/**
 * Find the best matching analysis directory for a given date and article type.
 *
 * Checks for exact match first (e.g. `propositions`), then scans for suffixed
 * variants (e.g. `propositions-2`, `propositions-run12`) and returns the one
 * with the highest suffix number.
 *
 * @param date - Article date (YYYY-MM-DD)
 * @param articleType - Article type slug (e.g. 'committee-reports')
 * @returns Absolute path to the best matching analysis directory, or null
 */
function findBestAnalysisDir(date: string, articleType: string): string | null {
  const dateDir = path.resolve(ANALYSIS_BASE_DIR, date);
  if (!fs.existsSync(dateDir)) return null;
 
  try {
    const entries = fs.readdirSync(dateDir, { withFileTypes: true });
    const escaped = articleType.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const pattern = new RegExp(`^${escaped}(?:-(\\d+|run\\d+))?$`);
 
    let bestPath: string | null = null;
    let bestSuffix = -1;
 
    for (const entry of entries) {
      if (!entry.isDirectory()) continue;
      const match = pattern.exec(entry.name);
      if (!match) continue;
      const suffix = parseSuffixPriority(match[1]);
      if (suffix > bestSuffix) {
        bestSuffix = suffix;
        bestPath = path.join(dateDir, entry.name);
      }
    }
 
    return bestPath;
  } catch {
    return null;
  }
}
 
// ─── Article parsing ────────────────────────────────────────────────────────
 
/**
 * Parse a news article filename into its components.
 *
 * @param filename - Filename like '2026-04-01-propositions-en.html'
 * @returns Parsed components or null if filename doesn't match
 */
function parseArticleComponents(
  filename: string
): { date: string; articleType: string; lang: LanguageCode } | null {
  const match = ARTICLE_FILENAME_PATTERN.exec(filename);
  if (!match?.[1] || !match[2] || !match[3]) return null;
 
  const date = match[1];
  const articleType = match[2];
  const lang = match[3] as LanguageCode;
 
  if (!ALL_LANGUAGES.includes(lang)) return null;
  return { date, articleType, lang };
}
 
// ─── Retrofit logic ─────────────────────────────────────────────────────────
 
interface RetrofitResult {
  readonly file: string;
  readonly analysisDir: string;
  readonly fileCount: number;
}
 
/**
 * Retrofit a single article file with an analysis transparency section.
 *
 * @param filePath - Absolute path to the article HTML file
 * @param date - Article date
 * @param articleType - Article type slug
 * @param lang - Language code
 * @param analysisDirPath - Absolute path to the analysis directory
 * @param dryRun - If true, don't write changes
 * @param force - If true, replace existing analysis sections
 * @returns Retrofit result or null if no changes needed
 */
function retrofitArticle(
  filePath: string,
  date: string,
  articleType: string,
  lang: LanguageCode,
  analysisDirPath: string,
  dryRun: boolean,
  force: boolean
): RetrofitResult | null {
  let html = fs.readFileSync(filePath, 'utf-8');
 
  const hasExisting = ANALYSIS_SECTION_REGEX.test(html);
 
  // Skip if already has analysis section (unless force mode)
  if (hasExisting && !force) return null;
 
  // In force mode, remove existing section before re-injecting
  if (hasExisting && force) {
    html = html.replace(ANALYSIS_SECTION_FULL_REGEX, '');
  }
 
  // Find the injection point
  const injectionMatch = INJECTION_REGEX.exec(html);
  if (!injectionMatch) {
    console.warn(`  ⚠️  No injection point found in ${path.basename(filePath)}`);
    return null;
  }
 
  // Discover analysis files on disk
  const analysisFiles = discoverAnalysisFileEntries(analysisDirPath);
  const analysisDirName = path.basename(analysisDirPath);
 
  // Render the analysis section
  const sectionHtml = renderAnalysisTransparencySection(
    date,
    articleType,
    lang,
    analysisDirName,
    analysisFiles.length > 0 ? analysisFiles : undefined
  );
 
  // Inject before the article-nav
  const injectionIndex = injectionMatch.index;
  const newHtml =
    html.slice(0, injectionIndex) +
    '\n    ' +
    sectionHtml.trim() +
    '\n    ' +
    html.slice(injectionIndex);
 
  if (!dryRun) {
    fs.writeFileSync(filePath, newHtml, 'utf-8');
  }
 
  return {
    file: path.basename(filePath),
    analysisDir: analysisDirName,
    fileCount: analysisFiles.length,
  };
}
 
// ─── Main execution ─────────────────────────────────────────────────────────
 
/**
 * Log the result of a single article retrofit operation.
 *
 * @param result - The retrofit result, or null if skipped
 * @param filename - Article filename
 * @param analysisDirName - Analysis directory name
 * @param dryRun - Whether this is a dry run
 */
function logRetrofitResult(
  result: RetrofitResult | null,
  filename: string,
  analysisDirName: string,
  dryRun: boolean
): void {
  if (!result) return;
  const prefix = dryRun ? '🔍 Would retrofit' : '✅ Retrofitted';
  console.log(`  ${prefix}: ${filename} → ${analysisDirName} (${result.fileCount} analysis files)`);
}
 
/**
 * Process a single article group (one date+type with all its language variants).
 *
 * @param group - The article group containing date, type, and language variants
 * @param group.date - Article date
 * @param group.articleType - Article type slug
 * @param group.files - Array of filename + language code tuples
 * @param newsDir - Absolute path to the news directory
 * @param dryRun - Whether to skip writing changes
 * @param force - Whether to replace existing analysis sections
 * @returns Count of retrofitted, skipped, and errored articles
 */
function processArticleGroup(
  group: {
    date: string;
    articleType: string;
    files: Array<{ filename: string; lang: LanguageCode }>;
  },
  newsDir: string,
  dryRun: boolean,
  force: boolean
): { total: number; retrofitted: number; skipped: number; errors: number } {
  const analysisDirPath = findBestAnalysisDir(group.date, group.articleType);
  if (!analysisDirPath) return { total: 0, retrofitted: 0, skipped: 0, errors: 0 };
 
  const analysisDirName = path.basename(analysisDirPath);
  let total = 0;
  let retrofitted = 0;
  let skipped = 0;
  let errors = 0;
 
  for (const { filename, lang } of group.files) {
    total++;
    try {
      const result = retrofitArticle(
        path.join(newsDir, filename),
        group.date,
        group.articleType,
        lang,
        analysisDirPath,
        dryRun,
        force
      );
      if (result) {
        retrofitted++;
        logRetrofitResult(result, filename, analysisDirName, dryRun);
      } else {
        skipped++;
      }
    } catch (err) {
      errors++;
      console.error(
        `  ❌ Error processing ${filename}: ${err instanceof Error ? err.message : String(err)}`
      );
    }
  }
 
  return { total, retrofitted, skipped, errors };
}
 
/**
 * Retrofit all articles that have matching analysis directories.
 *
 * @param dryRun - If true, report what would be changed without writing
 * @param force - If true, replace existing analysis sections
 * @returns Summary statistics
 */
export function retrofitAllArticles(
  dryRun: boolean = false,
  force: boolean = false
): {
  total: number;
  retrofitted: number;
  skipped: number;
  errors: number;
} {
  const newsDir = path.resolve(NEWS_DIR);
  if (!fs.existsSync(newsDir)) {
    console.log('📁 News directory does not exist');
    return { total: 0, retrofitted: 0, skipped: 0, errors: 0 };
  }
 
  const files = fs.readdirSync(newsDir).filter((f) => f.endsWith('.html'));
  const counts = { total: 0, retrofitted: 0, skipped: 0, errors: 0 };
 
  // Group by date+type to avoid redundant analysis dir lookups
  const articleGroups = new Map<
    string,
    { date: string; articleType: string; files: Array<{ filename: string; lang: LanguageCode }> }
  >();
 
  for (const filename of files) {
    const parsed = parseArticleComponents(filename);
    if (!parsed) continue;
 
    const key = `${parsed.date}/${parsed.articleType}`;
    const group = articleGroups.get(key);
    if (group) {
      group.files.push({ filename, lang: parsed.lang });
    } else {
      articleGroups.set(key, {
        date: parsed.date,
        articleType: parsed.articleType,
        files: [{ filename, lang: parsed.lang }],
      });
    }
  }
 
  for (const [, group] of articleGroups) {
    const groupResult = processArticleGroup(group, newsDir, dryRun, force);
    counts.total += groupResult.total;
    counts.retrofitted += groupResult.retrofitted;
    counts.skipped += groupResult.skipped;
    counts.errors += groupResult.errors;
  }
 
  return counts;
}
 
// ─── CLI entry point ────────────────────────────────────────────────────────
 
const isDryRun = process.argv.includes('--dry-run');
const isForce = process.argv.includes('--force');
 
console.log('');
console.log('🔗 Analysis Transparency Retrofit Tool');
console.log(
  `   Mode: ${isDryRun ? 'DRY RUN (no files will be modified)' : 'LIVE (files will be modified)'}${isForce ? ' [FORCE: replacing existing sections]' : ''}`
);
console.log('');
 
const result = retrofitAllArticles(isDryRun, isForce);
 
console.log('');
console.log('📊 Summary:');
console.log(`   Total articles with analysis dirs: ${result.total}`);
console.log(`   Retrofitted: ${result.retrofitted}`);
console.log(`   Already had section (skipped): ${result.skipped}`);
console.log(`   Errors: ${result.errors}`);
console.log('');