All files / utils fix-articles.ts

85.12% Statements 103/121
75.36% Branches 52/69
100% Functions 23/23
84.07% Lines 95/113

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                                                                1x     1x     1x     1x     1x                                                                 6x 84x 84x 84x 84x 84x 84x                     5x                                         7x 7x 7x                       9x 126x 126x 126x 126x 126x                     9x 9x                                                                                         5x 5x       5x                           5x     5x 5x       5x                             1x 1x       1x                 1x                           1x     1x 1x       1x         1x                         9x 6x   3x 3x 2x   1x                                 9x     9x 9x     9x                                     25x 25x 17x 17x   8x                           11x 11x 11x 1x     10x 10x 10x 10x 11x 11x   11x   11x         1x     9x 9x 9x 9x     9x 5x     4x 1x     3x 1x       9x 9x       9x   9x       9x 3x     9x                             2x 1x   1x 2x 2x 2x     2x 2x   1x       1x                                          
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Utils/FixArticles
 * @description FALLBACK TOOL — Retroactively adds missing language switcher, article-top-nav
 * (back button), site-header, skip-link, reading-progress bar, and site-footer to existing
 * news articles.
 *
 * The primary mechanism for including these elements is the article template
 * (`generateArticleHTML` in `src/templates/article-template.ts`), which already produces
 * all required structural elements. This script is a last-resort recovery tool for
 * patching legacy articles generated before the template was complete.
 *
 * Usage: npx tsx src/utils/fix-articles.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,
  LANGUAGE_FLAGS,
  LANGUAGE_NAMES,
  BACK_TO_NEWS_LABELS,
  ARTICLE_NAV_LABELS,
  SKIP_LINK_TEXTS,
  getLocalizedString,
} from '../constants/languages.js';
import { escapeHTML } from './file-utils.js';
 
/** CSS class selector for the language switcher nav element */
const LANG_SWITCHER_CLASS = 'class="language-switcher"';
 
/** CSS class selector for the article top navigation element */
const ARTICLE_TOP_NAV_CLASS = 'class="article-top-nav"';
 
/** CSS class selector for the site header element */
const SITE_HEADER_CLASS = 'class="site-header"';
 
/** CSS class selector for the reading progress bar element */
const READING_PROGRESS_CLASS = 'class="reading-progress"';
 
/** CSS class selector for the site footer element */
const SITE_FOOTER_CLASS = 'class="site-footer"';
 
/** Context for article injection */
interface InjectionContext {
  /** Article date (YYYY-MM-DD) */
  date: string;
  /** Article URL slug */
  slug: string;
  /** Language code */
  lang: string;
  /** Link to the language-specific index page */
  indexHref: string;
  /** Localized skip link text */
  skipLinkText: string;
}
 
/** Result of an injection attempt */
interface InjectionResult {
  /** Updated HTML */
  html: string;
  /** Description of what was changed */
  change: string;
}
 
/**
 * Build the language switcher nav HTML for an article.
 *
 * @param date - Article date (YYYY-MM-DD)
 * @param slug - Article URL slug
 * @param currentLang - Active language code
 * @returns HTML string for the language switcher
 */
function buildLangSwitcher(date: string, slug: string, currentLang: string): string {
  return ALL_LANGUAGES.map((code) => {
    const flag = getLocalizedString(LANGUAGE_FLAGS, code);
    const name = getLocalizedString(LANGUAGE_NAMES, code);
    const active = code === currentLang ? ' active' : '';
    const href = `${date}-${slug}-${code}.html`;
    const safeTitle = escapeHTML(name);
    return `<a href="${href}" class="lang-link${active}" hreflang="${code}" lang="${code}" title="${safeTitle}">${flag} ${code.toUpperCase()}</a>`;
  }).join('\n        ');
}
 
/**
 * Build the site header HTML.
 *
 * @param indexHref - Link to the language-specific index page
 * @returns HTML string for the site header
 */
function buildSiteHeader(indexHref: string): string {
  return `<header class="site-header" role="banner">
    <div class="site-header__inner">
      <a href="${indexHref}" class="site-header__brand" aria-label="EU Parliament Monitor">
        <span class="site-header__flag" aria-hidden="true">šŸ‡ŖšŸ‡ŗ</span>
        <span>
          <span class="site-header__title">EU Parliament Monitor</span>
          <span class="site-header__subtitle">European Parliament Intelligence</span>
        </span>
      </a>
    </div>
  </header>`;
}
 
/**
 * Build the article-top-nav HTML with a localized back button.
 *
 * @param indexHref - Link to the language-specific index page
 * @param lang - Language code for localized labels
 * @returns HTML string for the article top navigation
 */
function buildArticleTopNav(indexHref: string, lang: string): string {
  const backLabel = getLocalizedString(BACK_TO_NEWS_LABELS, lang);
  const articleNavLabel = escapeHTML(getLocalizedString(ARTICLE_NAV_LABELS, lang));
  return `<nav class="article-top-nav" aria-label="${articleNavLabel}">
    <a href="${indexHref}" class="back-to-news">${backLabel}</a>
  </nav>`;
}
 
/**
 * Build the language grid for the article footer.
 *
 * @param currentLang - Active language code
 * @returns HTML string for the language grid
 */
function buildFooterLanguageGrid(currentLang: string): string {
  return ALL_LANGUAGES.map((code) => {
    const flag = getLocalizedString(LANGUAGE_FLAGS, code);
    const safeName = escapeHTML(getLocalizedString(LANGUAGE_NAMES, code));
    const href = code === 'en' ? '../index.html' : `../index-${code}.html`;
    const active = code === currentLang ? ' class="active"' : '';
    return `<a href="${href}"${active} hreflang="${code}">${flag} ${safeName}</a>`;
  }).join('\n            ');
}
 
/**
 * Build the site footer HTML.
 *
 * @param lang - Language code for the language grid active state
 * @returns HTML string for the site footer
 */
function buildSiteFooter(lang: string): string {
  const year = new Date().getFullYear();
  return `<footer class="site-footer" role="contentinfo">
    <div class="footer-content">
      <div class="footer-section">
        <h3>About EU Parliament Monitor</h3>
        <p>European Parliament Intelligence Platform — monitoring political activity with systematic transparency. Powered by European Parliament open data.</p>
      </div>
      <div class="footer-section">
        <h3>Quick Links</h3>
        <ul>
          <li><a href="../index.html">Home</a></li>
          <li><a href="https://github.com/Hack23/euparliamentmonitor">GitHub Repository</a></li>
          <li><a href="https://github.com/Hack23/euparliamentmonitor/blob/main/LICENSE">Apache-2.0 License</a></li>
          <li><a href="https://www.europarl.europa.eu/">European Parliament</a></li>
        </ul>
      </div>
      <div class="footer-section">
        <h3>Built by Hack23 AB</h3>
        <ul>
          <li><a href="https://hack23.com">hack23.com</a></li>
          <li><a href="https://www.linkedin.com/company/hack23">LinkedIn</a></li>
          <li><a href="https://github.com/Hack23/ISMS-PUBLIC">Security &amp; Privacy Policy</a></li>
          <li><a href="mailto:james@hack23.com">Contact</a></li>
        </ul>
      </div>
      <div class="footer-section">
        <h3>Languages</h3>
        <div class="language-grid">
          ${buildFooterLanguageGrid(lang)}
        </div>
      </div>
    </div>
    <div class="footer-bottom">
      <p>&copy; 2008-${year} <a href="https://hack23.com">Hack23 AB</a> (Org.nr 5595347807) | Gothenburg, Sweden</p>
    </div>
  </footer>`;
}
 
/**
 * Inject full structural elements for articles with no site-header (Type A).
 *
 * @param html - Current article HTML
 * @param ctx - Injection context
 * @returns Updated HTML and change description, or null if not applicable
 */
function injectTypeA(html: string, ctx: InjectionContext): InjectionResult | null {
  const bodyArticlePattern = /(<body>)\s*\n(\s*<article\s)/;
  Iif (!bodyArticlePattern.test(html)) {
    return null;
  }
 
  const injectedBlock = `$1
  <div class="reading-progress" aria-hidden="true"></div>
  <a href="#main" class="skip-link">${ctx.skipLinkText}</a>
 
  ${buildSiteHeader(ctx.indexHref)}
 
  <nav class="language-switcher" role="navigation" aria-label="Language selection">
    ${buildLangSwitcher(ctx.date, ctx.slug, ctx.lang)}
  </nav>
 
  ${buildArticleTopNav(ctx.indexHref, ctx.lang)}
 
  <main id="main" class="site-main">
  $2`;
  let result = html.replace(bodyArticlePattern, injectedBlock);
 
  // Close </main> before </body>
  result = result.replace(/(<\/article>)\s*\n(<\/body>)/, '$1\n  </main>\n$2');
  Iif (!result.includes('</main>')) {
    result = result.replace('</body>', '  </main>\n</body>');
  }
 
  return {
    html: result,
    change:
      'Added reading-progress, skip-link, site-header, language-switcher, article-top-nav, main wrapper',
  };
}
 
/**
 * Inject language switcher and top nav for articles with site-header but missing both (Type B).
 *
 * @param html - Current article HTML
 * @param ctx - Injection context
 * @returns Updated HTML and change description, or null if not applicable
 */
function injectTypeB(html: string, ctx: InjectionContext): InjectionResult | null {
  const headerMainPattern = /(<\/header>)\s*\n(\s*<main\s)/;
  Iif (!headerMainPattern.test(html)) {
    return null;
  }
 
  const injectedBlock = `$1
 
  <nav class="language-switcher" role="navigation" aria-label="Language selection">
    ${buildLangSwitcher(ctx.date, ctx.slug, ctx.lang)}
  </nav>
 
  ${buildArticleTopNav(ctx.indexHref, ctx.lang)}
 
  $2`;
  return {
    html: html.replace(headerMainPattern, injectedBlock),
    change: 'Added language-switcher and article-top-nav',
  };
}
 
/**
 * Inject article-top-nav for articles that already have language-switcher (Type C).
 *
 * @param html - Current article HTML
 * @param ctx - Injection context
 * @returns Updated HTML and change description, or null if not applicable
 */
function injectTypeC(html: string, ctx: InjectionContext): InjectionResult | null {
  Iif (html.includes(ARTICLE_TOP_NAV_CLASS)) {
    return null;
  }
  const langSwitcherMainPattern = /(<\/nav>)\s*\n(\s*<main\s)/;
  Iif (!langSwitcherMainPattern.test(html)) {
    return null;
  }
 
  const injectedBlock = `$1
 
  ${buildArticleTopNav(ctx.indexHref, ctx.lang)}
 
  $2`;
  return {
    html: html.replace(langSwitcherMainPattern, injectedBlock),
    change: 'Added article-top-nav',
  };
}
 
/**
 * Inject reading-progress bar if missing.
 *
 * @param html - Current article HTML
 * @returns Updated HTML and change description, or null if already present
 */
function injectReadingProgress(html: string): InjectionResult | null {
  if (html.includes(READING_PROGRESS_CLASS)) {
    return null;
  }
  const pattern = /(<body>)\s*\n(\s*<a href="#main")/;
  if (!pattern.test(html)) {
    return null;
  }
  return {
    html: html.replace(
      pattern,
      '$1\n  <div class="reading-progress" aria-hidden="true"></div>\n$2'
    ),
    change: 'Added reading-progress',
  };
}
 
/**
 * Inject site-footer if missing. Inserts before </body>.
 *
 * @param html - Current article HTML
 * @param lang - Language code for the footer language grid
 * @returns Updated HTML and change description, or null if already present
 */
function injectSiteFooter(html: string, lang: string): InjectionResult | null {
  Iif (html.includes(SITE_FOOTER_CLASS)) {
    return null;
  }
  const pattern = /(\s*)<\/body>/;
  Iif (!pattern.test(html)) {
    return null;
  }
  return {
    html: html.replace(pattern, `\n\n  ${buildSiteFooter(lang)}\n</body>`),
    change: 'Added site-footer',
  };
}
 
/**
 * Apply an injection result if present.
 *
 * @param current - Current HTML content
 * @param injector - Function returning an injection result or null
 * @param changes - Array to append change descriptions to
 * @returns Updated HTML content
 */
function applyInjection(
  current: string,
  injector: () => InjectionResult | null,
  changes: string[]
): string {
  const result = injector();
  if (result) {
    changes.push(result.change);
    return result.html;
  }
  return current;
}
 
/**
 * Fix a single article file by injecting missing structural elements.
 *
 * @param filepath - Absolute path to article HTML file
 * @param dryRun - If true, only report what would change
 * @returns Object with changed flag and description
 */
export function fixArticle(
  filepath: string,
  dryRun: boolean = false
): { changed: boolean; description: string } {
  const filename = path.basename(filepath);
  const match = filename.match(ARTICLE_FILENAME_PATTERN);
  if (!match) {
    return { changed: false, description: `Skipped (not matching pattern): ${filename}` };
  }
 
  const date = match[1] as string;
  const slug = match[2] as string;
  const lang = match[3] as string;
  const indexHref = lang === 'en' ? '../index.html' : `../index-${lang}.html`;
  const skipLinkText = getLocalizedString(SKIP_LINK_TEXTS, lang);
  const ctx: InjectionContext = { date, slug, lang, indexHref, skipLinkText };
 
  let html = fs.readFileSync(filepath, 'utf-8');
 
  if (
    html.includes(LANG_SWITCHER_CLASS) &&
    html.includes(ARTICLE_TOP_NAV_CLASS) &&
    html.includes(SITE_FOOTER_CLASS)
  ) {
    return { changed: false, description: `Already complete: ${filename}` };
  }
 
  const changes: string[] = [];
  const hasSiteHeader = html.includes(SITE_HEADER_CLASS);
  const hasLangSwitcher = html.includes(LANG_SWITCHER_CLASS);
  const hasTopNav = html.includes(ARTICLE_TOP_NAV_CLASS);
 
  // Type A: Missing everything
  if (!hasSiteHeader && !hasLangSwitcher && !hasTopNav) {
    html = applyInjection(html, () => injectTypeA(html, ctx), changes);
  }
  // Type B: Has site-header but no language-switcher or top-nav
  else if (hasSiteHeader && !hasLangSwitcher && !hasTopNav) {
    html = applyInjection(html, () => injectTypeB(html, ctx), changes);
  }
  // Type C: Has language-switcher but no article-top-nav
  else if (hasLangSwitcher && !hasTopNav) {
    html = applyInjection(html, () => injectTypeC(html, ctx), changes);
  }
 
  // Add reading-progress if missing (for types B and C)
  Eif (html.includes(SITE_HEADER_CLASS)) {
    html = applyInjection(html, () => injectReadingProgress(html), changes);
  }
 
  // Add site-footer if missing
  html = applyInjection(html, () => injectSiteFooter(html, lang), changes);
 
  Iif (changes.length === 0) {
    return { changed: false, description: `No changes needed: ${filename}` };
  }
 
  if (!dryRun) {
    fs.writeFileSync(filepath, html, 'utf-8');
  }
 
  return { changed: true, description: `Fixed ${filename}: ${changes.join(', ')}` };
}
 
/**
 * Fix all articles in the news directory.
 *
 * @param dryRun - If true, only report what would change
 * @returns Summary of changes
 */
export function fixAllArticles(dryRun: boolean = false): {
  total: number;
  fixed: number;
  skipped: number;
  results: Array<{ changed: boolean; description: string }>;
} {
  const files = fs.readdirSync(NEWS_DIR).filter((f) => f.endsWith('.html'));
  const results: Array<{ changed: boolean; description: string }> = [];
 
  for (const file of files) {
    const filepath = path.join(NEWS_DIR, file);
    const result = fixArticle(filepath, dryRun);
    results.push(result);
  }
 
  const fixed = results.filter((r) => r.changed).length;
  const skipped = results.filter((r) => !r.changed).length;
 
  return { total: files.length, fixed, skipped, results };
}
 
// CLI entry point
Iif (process.argv[1] && path.resolve(process.argv[1]) === path.resolve(import.meta.filename ?? '')) {
  const dryRun = process.argv.includes('--dry-run');
  console.log(`šŸ”§ Fix Articles ${dryRun ? '(DRY RUN)' : ''}`);
  console.log(`šŸ“ Scanning: ${NEWS_DIR}\n`);
 
  const summary = fixAllArticles(dryRun);
 
  for (const result of summary.results) {
    if (result.changed) {
      console.log(`  āœ… ${result.description}`);
    }
  }
 
  console.log(
    `\nšŸ“Š Summary: ${summary.fixed} fixed, ${summary.skipped} skipped, ${summary.total} total`
  );
 
  if (dryRun) {
    console.log('\nāš ļø  Dry run — no files were modified. Remove --dry-run to apply changes.');
  }
}