All files / scripts/generate-news-indexes helpers.ts

91.35% Statements 148/162
87.79% Branches 151/172
97.29% Functions 36/37
93% Lines 133/143

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                      2x               2x 2x     2x           25x 25x 25x 25x             112x 112x 1568x 1568x 1568x 1568x   112x             5x   4x 4x 9x   5x               10315x 10315x 10315x     10315x 10315x 1x 1x     10314x     10314x                               10315x                           31223x 31223x 31223x     485x 485x 485x     485x 485x 485x   485x 485x 485x   485x                             10314x     10314x 212x       10102x 10102x                               190x   190x 190x   176x 176x     176x 176x 176x                           14x 14x               10314x     10314x                                 283159x 2325x       7989x                                 7989x 17878x 7947x       42x                                 1057x 13x     29x               10314x 10314x     10314x 15746x 15746x 15746x 15746x 15746x 15746x 15746x 15746x     10314x             10314x 10314x     10314x 15746x     10314x             11x   11x 10461x 10439x   11x     11x 154x     11x 10285x 10285x   10285x 10285x         11x 43252x     11x 154x 154x 11x   11x             8x     8x 112x   7480x   7480x 7480x       7480x 104720x 6993800x   6993800x 102440x             8x                           4x   4x 9x   10x   10x 7x   10x         4x   4x 9x 10x 10x   10x                 10x   4x    
/**
 * @module generate-news-indexes/helpers
 * @description Utility functions for article metadata parsing, topic/type
 * classification, news directory scanning, and cross-language discovery.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import type {
  LanguageConfig,
  NewsArticleMetadata,
  ArticleTypeValue,
} from './types.js';
import { LANGUAGES, LANGUAGE_FLAGS, AVAILABLE_IN_TRANSLATIONS } from './constants.js';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
 
/** Root news directory */
export const NEWS_DIR: string = path.join(__dirname, '..', '..', 'news');
 
/**
 * Generate language badge HTML for an article.
 */
export function generateLanguageBadge(lang: string, isRTL: boolean = false): string {
  const flag: string = LANGUAGE_FLAGS[lang] || '🌐';
  const langUpper: string = lang.toUpperCase();
  const dirAttr: string = isRTL ? ' dir="ltr"' : '';
  return `<span class="language-badge"${dirAttr} aria-label="${(LANGUAGES as Record<string, LanguageConfig>)[lang]?.name || lang} language"><span aria-hidden="true">${flag}</span> ${langUpper}</span>`;
}
 
/**
 * Generate language switcher navigation for news index pages.
 */
export function generateLanguageSwitcherNav(currentLang: string): string {
  const langEntries: [string, LanguageConfig][] = Object.entries(LANGUAGES);
  const links: string = langEntries.map(([code, data]) => {
    const flag: string = LANGUAGE_FLAGS[code] || '🌐';
    const filename: string = code === 'en' ? 'index.html' : `index_${code}.html`;
    const activeClass: string = code === currentLang ? ' active' : '';
    return `  <a href="${filename}" class="lang-link${activeClass}" hreflang="${code}">${flag} ${data.name}</a>`;
  }).join('\n');
  return `<nav class="language-switcher" role="navigation" aria-label="Language selection">\n${links}\n</nav>`;
}
 
/**
 * Generate "Available in" text with language badges.
 */
export function generateAvailableLanguages(languages: string[], currentLang: string): string {
  if (!languages || languages.length <= 1) return '';
 
  const isRTL: boolean = ['ar', 'he'].includes(currentLang);
  const availableText: string = AVAILABLE_IN_TRANSLATIONS[currentLang] || 'Available in';
  const badges: string = languages.map((lang) => generateLanguageBadge(lang, isRTL)).join(' ');
 
  return `<p class="available-languages"><strong>${availableText}:</strong> ${badges}</p>`;
}
 
 
/**
 * Parse HTML file to extract article metadata.
 */
export function parseArticleMetadata(filePath: string): NewsArticleMetadata | null {
  try {
    const content: string = fs.readFileSync(filePath, 'utf-8');
    const fileName: string = path.basename(filePath);
 
    // Extract language from filename (e.g., article-en.html → en, article-da.html → da)
    const langMatch: RegExpMatchArray | null = fileName.match(/-(en|sv|da|no|fi|de|fr|es|nl|ar|he|ja|ko|zh)\.html$/);
    if (!langMatch) {
      console.warn(`  ⚠️ Skipping ${fileName}: no language suffix`);
      return null;
    }
 
    const lang: string = langMatch[1]!;
 
    // Extract metadata from HTML meta tags
    const metadata: NewsArticleMetadata = {
      slug: fileName,
      lang,
      title: extractMetaContent(content, 'og:title') || extractTitle(content) || 'Untitled',
      description: extractMetaContent(content, 'og:description') || extractMetaContent(content, 'description') || '',
      date: normalizeDateString(
        extractMetaContent(content, 'article:published_time') ||
        extractMetaContent(content, 'date') ||
        extractDateFromJSONLD(content) ||
        extractFromFilename(fileName),
      ),
      type: classifyArticleType(content, fileName),
      topics: extractTopics(content),
      tags: extractTags(content),
    };
 
    return metadata;
  } catch (error: unknown) {
    console.error(`  ❌ Error parsing ${path.basename(filePath)}:`, (error as Error).message);
    return null;
  }
}
 
/**
 * Extract content from meta tags.
 *
 * Fixed: regex now properly handles apostrophes and special characters in content.
 */
export function extractMetaContent(html: string, property: string): string | null {
  // Match double-quoted attributes
  const doubleQuotePattern = new RegExp(`<meta\\s+(?:property|name)="${property}"\\s+content="([^"]+)"`, 'i');
  const doubleQuoteMatch: RegExpMatchArray | null = html.match(doubleQuotePattern);
  if (doubleQuoteMatch) return doubleQuoteMatch[1]!;
 
  // Match single-quoted attributes
  const singleQuotePattern = new RegExp(`<meta\\s+(?:property|name)='${property}'\\s+content='([^']+)'`, 'i');
  const singleQuoteMatch: RegExpMatchArray | null = html.match(singleQuotePattern);
  Iif (singleQuoteMatch) return singleQuoteMatch[1]!;
 
  // Try reversed order (content before property/name)
  const reversedDoublePattern = new RegExp(`<meta\\s+content="([^"]+)"\\s+(?:property|name)="${property}"`, 'i');
  const reversedDoubleMatch: RegExpMatchArray | null = html.match(reversedDoublePattern);
  Iif (reversedDoubleMatch) return reversedDoubleMatch[1]!;
 
  const reversedSinglePattern = new RegExp(`<meta\\s+content='([^']+)'\\s+(?:property|name)='${property}'`, 'i');
  const reversedSingleMatch: RegExpMatchArray | null = html.match(reversedSinglePattern);
  Iif (reversedSingleMatch) return reversedSingleMatch[1]!;
 
  return null;
}
 
/**
 * Extract title from <title> tag.
 */
export function extractTitle(html: string): string | null {
  const match: RegExpMatchArray | null = html.match(/<title>([^<]+)<\/title>/i);
  return match ? match[1]!.replace(' - Riksdagsmonitor', '').trim() : null;
}
 
/**
 * Normalize date string to YYYY-MM-DD format.
 */
export function normalizeDateString(dateStr: string | null): string {
  Iif (!dateStr) return new Date().toISOString().split('T')[0]!;
 
  // If already in YYYY-MM-DD format, return as-is
  if (/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
    return dateStr;
  }
 
  // If ISO timestamp (with time), extract just the date part
  Eif (dateStr.includes('T')) {
    return dateStr.split('T')[0]!;
  }
 
  // If has timezone offset like +01:00, remove it first
  const cleaned: string = dateStr.replace(/[+-]\d{2}:\d{2}$/, '');
  if (cleaned.includes('T')) {
    return cleaned.split('T')[0]!;
  }
 
  return dateStr;
}
 
/**
 * Extract date from JSON-LD structured data.
 */
export function extractDateFromJSONLD(html: string): string | null {
  try {
    // Extract JSON-LD script tag content
    const jsonLdMatch: RegExpMatchArray | null = html.match(/<script type="application\/ld\+json">([\s\S]*?)<\/script>/i);
    if (!jsonLdMatch) return null;
 
    const jsonLdText: string = jsonLdMatch[1]!.trim();
    const jsonData: { datePublished?: string } = JSON.parse(jsonLdText) as { datePublished?: string };
 
    // Extract datePublished from NewsArticle schema
    Eif (jsonData.datePublished) {
      const dateStr: string = jsonData.datePublished.split('T')[0]!;
      return dateStr;
    }
 
    return null;
  } catch {
    // Silently fail - this is a fallback mechanism
    return null;
  }
}
 
/**
 * Extract date from filename (YYYY-MM-DD format).
 */
export function extractFromFilename(fileName: string): string {
  const match: RegExpMatchArray | null = fileName.match(/^(\d{4}-\d{2}-\d{2})/);
  return match ? match[1]! : new Date().toISOString().split('T')[0]!;
}
 
/**
 * Classify article type based on content and filename.
 * Supports detection keywords in all 14 languages.
 */
export function classifyArticleType(content: string, fileName: string): ArticleTypeValue {
  const lowerContent: string = content.toLowerCase();
 
  // Prospective: week-ahead / upcoming previews
  const prospectiveKeywords: string[] = [
    'week ahead', 'week-ahead', 'upcoming', 'preview', 'look ahead',           // en
    'veckan som kommer', 'kommande vecka', 'framåtblick',                       // sv
    'ugen der kommer', 'kommende uge', 'fremadrettet',                          // da
    'uken som kommer', 'fremtidsrettet',                                        // no
    'tuleva viikko', 'ennakko',                                                 // fi
    'woche voraus', 'vorschau',                                                 // de
    'semaine à venir', 'aperçu',                                                // fr
    'semana por delante', 'adelanto',                                            // es
    'week vooruit', 'vooruitblik',                                               // nl
    'الأسبوع المقبل', 'القادم',                                                  // ar
    'השבוע הבא', 'הקרוב',                                                       // he
    '来週の展望', '今後',                                                          // ja
    '주간 전망', '다가오는',                                                       // ko
    '一周展望', '即将'                                                             // zh
  ];
 
  if (fileName.includes('week-ahead') || fileName.includes('month-ahead') || prospectiveKeywords.some((kw) => lowerContent.includes(kw.toLowerCase()))) {
    return 'prospective';
  }
 
  // Analysis: committee reports, propositions, motions
  const analysisKeywords: string[] = [
    'committee reports', 'analysis', 'review', 'assessment',                     // en
    'utskottsbetänkanden', 'analys', 'granskning', 'betänkande',                // sv
    'udvalgsrapporter', 'analyse', 'gennemgang', 'udvalgsbetænkning',           // da
    'komitérapporter', 'gjennomgang', 'komitéinnstilling',                      // no
    'valiokuntaraportit', 'analyysi', 'katsaus', 'valiokunnan mietintö',        // fi
    'ausschussberichte', 'überprüfung', 'ausschussbericht',                     // de
    'rapports de commission', 'examen', 'rapport de commission',                 // fr
    'informes de comité', 'análisis', 'revisión', 'informe de comité',          // es
    'commissierapporten', 'beoordeling', 'commissieverslag',                     // nl
    'تقارير اللجان', 'تحليل', 'تقرير اللجنة',                                  // ar
    'דוחות ועדות', 'ניתוח', 'דוח ועדה',                                         // he
    '委員会報告', '分析',                                                          // ja
    '위원회 보고서', '분석',                                                       // ko
    '委员会报告', '分析'                                                           // zh
  ];
 
  if (fileName.includes('committee-reports') || fileName.includes('propositions') || fileName.includes('motions') ||
      analysisKeywords.some((kw) => lowerContent.includes(kw.toLowerCase()))) {
    return 'analysis';
  }
 
  // Breaking: urgent/alert news
  const breakingKeywords: string[] = [
    'breaking', 'urgent', 'alert', 'flash',                                      // en
    'senaste nytt', 'akut', 'brådskande',                                        // sv
    'seneste nyt', 'hastesag',                                                   // da
    'siste nytt', 'haster',                                                      // no
    'viimeisimmät', 'kiireellinen', 'hälytys',                                   // fi
    'eilmeldungen', 'dringend', 'alarm',                                         // de
    'dernières nouvelles', 'alerte',                                              // fr
    'última hora', 'urgente', 'alerta',                                           // es
    'laatste nieuws', 'alert',                                                    // nl
    'أخبار عاجلة', 'عاجل',                                                       // ar
    'חדשות אחרונות', 'דחוף',                                                     // he
    '速報', '緊急',                                                               // ja
    '속보', '긴급',                                                               // ko
    '突发新闻', '紧急'                                                             // zh
  ];
 
  if (fileName.includes('breaking') || breakingKeywords.some((kw) => lowerContent.includes(kw.toLowerCase()))) {
    return 'breaking';
  }
 
  return 'retrospective';
}
 
/**
 * Extract topics from article tags.
 * Supports topic detection keywords in all 14 languages.
 */
export function extractTopics(content: string): string[] {
  const topics: string[] = [];
  const tagPattern = /<meta\s+property=["']article:tag["']\s+content=["']([^"']+)["']/gi;
  let match: RegExpExecArray | null;
 
  while ((match = tagPattern.exec(content)) !== null) {
    const tag: string = match[1]!.toLowerCase();
    if (tag.includes('eu')) topics.push('eu');
    if (tag.includes('parliament') || tag.includes('riksdag') || tag.includes('parlamentet') || tag.includes('議会') || tag.includes('의회') || tag.includes('议会') || tag.includes('البرلمان') || tag.includes('פרלמנט')) topics.push('parliament');
    if (tag.includes('government') || tag.includes('regering') || tag.includes('regjeringen') || tag.includes('hallitus') || tag.includes('regierung') || tag.includes('gouvernement') || tag.includes('gobierno') || tag.includes('政府') || tag.includes('정부') || tag.includes('الحكومة') || tag.includes('ממשלה')) topics.push('government');
    if (tag.includes('defense') || tag.includes('defence') || tag.includes('försvar') || tag.includes('forsvar') || tag.includes('puolustus') || tag.includes('verteidigung') || tag.includes('défense') || tag.includes('defensa') || tag.includes('defensie') || tag.includes('الدفاع') || tag.includes('הגנה') || tag.includes('防衛') || tag.includes('국방') || tag.includes('国防')) topics.push('defense');
    if (tag.includes('environment') || tag.includes('miljö') || tag.includes('miljø') || tag.includes('ympäristö') || tag.includes('umwelt') || tag.includes('environnement') || tag.includes('medio ambiente') || tag.includes('milieu') || tag.includes('البيئة') || tag.includes('סביבה') || tag.includes('環境') || tag.includes('환경') || tag.includes('环境')) topics.push('environment');
    if (tag.includes('committee') || tag.includes('utskott') || tag.includes('udvalg') || tag.includes('utvalg') || tag.includes('valiokunt') || tag.includes('ausschuss') || tag.includes('commission') || tag.includes('comité') || tag.includes('commissie') || tag.includes('لجنة') || tag.includes('ועדה') || tag.includes('委員会') || tag.includes('위원회') || tag.includes('委员会')) topics.push('committees');
    if (tag.includes('legislation') || tag.includes('lagstiftning') || tag.includes('lovgivning') || tag.includes('lainsäädäntö') || tag.includes('gesetzgebung') || tag.includes('législation') || tag.includes('legislación') || tag.includes('wetgeving') || tag.includes('التشريعات') || tag.includes('חקיקה') || tag.includes('立法') || tag.includes('입법')) topics.push('legislation');
  }
 
  return [...new Set(topics)].slice(0, 5); // Unique, max 5
}
 
/**
 * Extract tags from article:tag meta tags.
 */
export function extractTags(content: string): string[] {
  const tags: string[] = [];
  const tagPattern = /<meta\s+property=["']article:tag["']\s+content=["']([^"']+)["']/gi;
  let match: RegExpExecArray | null;
 
  while ((match = tagPattern.exec(content)) !== null) {
    tags.push(match[1]!);
  }
 
  return tags.slice(0, 4); // Max 4 tags for display
}
 
/**
 * Scan news directory and group articles by language.
 */
export function scanNewsArticles(): Record<string, NewsArticleMetadata[]> {
  console.log('\n📰 Scanning for articles...');
 
  const files: string[] = fs.readdirSync(NEWS_DIR)
    .filter((file) => file.endsWith('.html'))
    .filter((file) => !file.startsWith('index')); // Exclude index files
 
  console.log(`  Found ${files.length} article files`);
 
  // Initialize buckets for all 14 supported languages
  const articlesByLang: Record<string, NewsArticleMetadata[]> = Object.fromEntries(
    Object.keys(LANGUAGES).map((lang) => [lang, []]),
  );
 
  files.forEach((file) => {
    const filePath: string = path.join(NEWS_DIR, file);
    const metadata: NewsArticleMetadata | null = parseArticleMetadata(filePath);
 
    Eif (metadata && articlesByLang[metadata.lang]) {
      articlesByLang[metadata.lang]!.push(metadata);
    }
  });
 
  // Sort by date descending (newest first)
  Object.keys(articlesByLang).forEach((lang) => {
    articlesByLang[lang]?.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
  });
 
  const langCounts: string[] = Object.entries(articlesByLang)
    .filter(([, arr]) => arr.length > 0)
    .map(([lang, arr]) => `${lang.toUpperCase()} ${arr.length}`);
  console.log(`  📊 Articles by language: ${langCounts.length > 0 ? langCounts.join(', ') : 'none found'}`);
 
  return articlesByLang;
}
 
/**
 * Build map of base slugs to available languages for cross-language discovery.
 */
export function buildSlugToLanguagesMap(articlesByLang: Record<string, NewsArticleMetadata[]>): Record<string, string[]> {
  const slugToLanguages: Record<string, string[]> = {};
 
  // Iterate through all articles in all languages
  Object.entries(articlesByLang).forEach(([_lang, articles]) => {
    articles.forEach((article) => {
      // Strip language suffix from slug to get base slug
      const baseSlug: string = article.slug.replace(/-(en|sv|da|no|fi|de|fr|es|nl|ar|he|ja|ko|zh)\.html$/, '.html');
 
      Eif (!slugToLanguages[article.slug]) {
        slugToLanguages[article.slug] = [];
      }
 
      // Find all articles with the same base slug across languages
      Object.entries(articlesByLang).forEach(([otherLang, otherArticles]) => {
        otherArticles.forEach((otherArticle) => {
          const otherBaseSlug: string = otherArticle.slug.replace(/-(en|sv|da|no|fi|de|fr|es|nl|ar|he|ja|ko|zh)\.html$/, '.html');
 
          if (baseSlug === otherBaseSlug && !slugToLanguages[article.slug]!.includes(otherLang)) {
            slugToLanguages[article.slug]!.push(otherLang);
          }
        });
      });
    });
  });
 
  return slugToLanguages;
}
 
/**
 * Get all articles with language information for cross-language discovery.
 *
 * NOTE: This function is currently UNUSED in production but preserved for potential
 * future use. It was implemented for Issue #155's cross-language discovery feature
 * but the requirement changed to language-specific filtering.
 *
 * @deprecated Currently unused - kept for potential future cross-language discovery
 */
export function getAllArticlesWithLanguageInfo(articlesByLang: Record<string, NewsArticleMetadata[]>): NewsArticleMetadata[] {
  // Build a map of slugs to available languages
  const slugToLanguages = new Map<string, string[]>();
 
  Object.entries(articlesByLang).forEach(([lang, articles]) => {
    articles.forEach((article) => {
      // Extract base slug (remove language suffix)
      const baseSlug: string = article.slug.replace(/-(en|sv|da|no|fi|de|fr|es|nl|ar|he|ja|ko|zh)\.html$/, '');
 
      if (!slugToLanguages.has(baseSlug)) {
        slugToLanguages.set(baseSlug, []);
      }
      slugToLanguages.get(baseSlug)!.push(lang);
    });
  });
 
  // Collect all articles and enrich with language info
  const allArticles: NewsArticleMetadata[] = [];
 
  Object.entries(articlesByLang).forEach(([lang, articles]) => {
    articles.forEach((article) => {
      const baseSlug: string = article.slug.replace(/-(en|sv|da|no|fi|de|fr|es|nl|ar|he|ja|ko|zh)\.html$/, '');
      const availableLanguages: string[] = slugToLanguages.get(baseSlug) || [lang];
 
      allArticles.push({
        ...article,
        availableLanguages: availableLanguages.sort(),
        baseSlug,
      });
    });
  });
 
  // Sort by date descending (newest first)
  allArticles.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
 
  return allArticles;
}