All files / src/generators/sitemap html.ts

100% Statements 53/53
78.12% Branches 25/32
100% Functions 12/12
100% Lines 52/52

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                                                                                                                                                                      2416x                     1739x                           56x 784x 784x 784x 784x 784x 784x                                                                   56x 56x 56x 56x 56x 56x 56x         56x           56x 56x 56x 56x 56x                 56x     784x           56x 784x 784x 784x 784x 784x             56x 56x 16x 16x 16x 15x 15x   16x     56x 616x 15x       56x       15x   15x 15x 15x     16x           15x                   56x                             56x                                                                       56x   56x                                                                                                                                                                                            
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Generators/Sitemap/Html
 * @description Generates the per-language `sitemap_<lang>.html` pages —
 * the human-friendly index of every news article and documentation
 * page, with category grouping, hreflang alternates, breadcrumb,
 * structured data, and the 14-language language switcher.
 *
 * Lifted out of the monolithic `sitemap.ts` so the HTML renderer can be
 * unit-tested in isolation, so the bounded context "sitemap HTML" is
 * clear, and so the lookup tables in `copy.ts` can be reused by any
 * future renderer (Atom-feed metadata, OG-card builder, etc.) without
 * having to import `sitemap.ts` and pull in the entire CLI surface.
 *
 * Output is byte-identical to the legacy in-line implementation that
 * lived in `sitemap.ts` between Apr-2026 and the bounded-context
 * refactor — verified by the regression test in
 * `test/unit/sitemap-byte-equality.test.js` (compares against the
 * golden snapshots taken from `npm run prebuild`).
 */
 
import { BASE_URL, THEME_TOGGLE_SCRIPT } from '../../constants/config.js';
import {
  ALL_LANGUAGES,
  LANGUAGE_NAMES,
  LANGUAGE_FLAGS,
  PAGE_TITLES,
  PAGE_DESCRIPTIONS,
  SKIP_LINK_TEXTS,
  getLocalizedString,
  getTextDirection,
} from '../../constants/languages.js';
import { escapeHTML } from '../../utils/file-utils.js';
import { detectCategory } from '../../utils/article-category.js';
import {
  ARTICLE_TYPE_LABELS,
  FOOTER_POLITICAL_INTELLIGENCE_LABELS,
} from '../../constants/language-ui.js';
import type { ArticleCategory, LanguageCode } from '../../types/index.js';
import {
  buildSiteFooter,
  buildSiteHeader,
  buildPageBanner,
} from '../../templates/section-builders.js';
import { getPoliticalIntelligenceFilename } from '../political-intelligence.js';
import {
  SITEMAP_TITLES,
  SITEMAP_SECTIONS,
  DOCS_LABELS,
  CATEGORY_ORDER,
  DEFAULT_SITEMAP_TITLE,
  getSitemapCopy,
} from './copy.js';
 
/**
 * Article info extracted for sitemap HTML display.
 *
 * `slug` is the article slug excluding the `YYYY-MM-DD-` prefix and the
 * `_<lang>` suffix — used only for editorial-category detection. When
 * absent, `filename` is used as the fallback signal.
 */
export interface SitemapArticleInfo {
  /** Filename within the news/ directory (e.g. `2026-04-27-foo.en.html`) */
  readonly filename: string;
  /** ISO date (`YYYY-MM-DD`) parsed from the filename */
  readonly date: string;
  /** Article title (already-decoded plain text, NOT yet HTML-escaped) */
  readonly title: string;
  /** Article description (already-decoded plain text, NOT yet HTML-escaped) */
  readonly description: string;
  /** Original slug (excluding date and language suffix), used for category detection */
  readonly slug?: string;
}
 
/**
 * Get the sitemap HTML filename for a given language code.
 *
 * @param lang - Language code (e.g. `en`, `sv`, `de`)
 * @returns `sitemap.html` for English, `sitemap_<lang>.html` for everything else
 */
export function getSitemapFilename(lang: string): string {
  return lang === 'en' ? 'sitemap.html' : `sitemap_${lang}.html`;
}
 
/**
 * Get the index filename for a given language code. Mirrors the rule
 * used by `getIndexFilename()` in `news-indexes.ts`.
 *
 * @param lang - Language code
 * @returns `index.html` for English, `index-<lang>.html` for everything else
 */
export function getIndexFilename(lang: string): string {
  return lang === 'en' ? 'index.html' : `index-${lang}.html`;
}
 
/**
 * Build the language switcher nav HTML for the sitemap pages.
 *
 * Each link points at the sibling `sitemap_<lang>.html`; the active
 * language gets `aria-current="page"` and the `active` class so screen
 * readers and CSS can flag it.
 *
 * @param currentLang - Active language code
 * @returns HTML fragment to be embedded inside `<nav class="language-switcher">`
 */
function buildSitemapLangSwitcher(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 ariaCurrent = code === currentLang ? ' aria-current="page"' : '';
    const href = getSitemapFilename(code);
    return `<a href="${href}" class="lang-link${active}" hreflang="${code}" lang="${code}" title="${escapeHTML(name)}" aria-label="${escapeHTML(name)}"${ariaCurrent}>${flag} ${code.toUpperCase()}</a>`;
  }).join('\n        ');
}
 
/**
 * Generate a sitemap HTML page for a specific language.
 *
 * Lists all articles for that language with titles and descriptions,
 * grouped by editorial category in {@link CATEGORY_ORDER}, plus a
 * high-level documentation section (only when `hasDocsDir` is true) and
 * a Pages section with one entry per supported language.
 *
 * The output document includes:
 * - `<head>` `<link rel="alternate">` hreflang block covering every
 *   supported language plus `x-default` → English
 * - JSON-LD `CollectionPage` structured data (with `<` escaped as
 *   `\u003c` to avoid breaking out of the `<script>` element)
 * - Skip link, header brand, theme toggle, language switcher
 * - Hero section with localized stats (article count, language count,
 *   category count, last-updated date)
 * - Breadcrumb nav
 * - Pages / Documentation / News sections
 * - Shared site footer via {@link buildSiteFooter}
 *
 * @param lang - Language code
 * @param articleInfos - Article info (title/description) for this language
 * @param hasDocsDir - Whether the docs/ directory exists (controls the Documentation section)
 * @returns Complete HTML document string
 */
export function generateSitemapHTML(
  lang: string,
  articleInfos: SitemapArticleInfo[],
  hasDocsDir: boolean = false
): string {
  const sitemapTitle = SITEMAP_TITLES[lang] ?? SITEMAP_TITLES['en'] ?? DEFAULT_SITEMAP_TITLE;
  const pageTitle = `${getLocalizedString(PAGE_TITLES, lang).split(' - ')[0]} - ${sitemapTitle}`;
  const description = getLocalizedString(PAGE_DESCRIPTIONS, lang);
  const skipLinkText = getLocalizedString(SKIP_LINK_TEXTS, lang);
  const dir = getTextDirection(lang);
  const today = new Date().toISOString().slice(0, 10);
  const sections = (SITEMAP_SECTIONS[lang] ?? SITEMAP_SECTIONS['en']) as {
    news: string;
    docs: string;
    pages: string;
  };
  const docsLabels = (DOCS_LABELS[lang] ?? DOCS_LABELS['en']) as {
    api: string;
    coverage: string;
    testResults: string;
    docsHome: string;
  };
  const copy = getSitemapCopy(lang);
  const heroTitle = getLocalizedString(PAGE_TITLES, lang).split(' - ')[0] ?? '';
  const typeLabels = getLocalizedString(ARTICLE_TYPE_LABELS, lang);
  const canonicalUrl = `${BASE_URL}/${getSitemapFilename(lang)}`;
  const header = buildSiteHeader({
    lang: lang as LanguageCode,
    pathPrefix: '',
    homeHref: getIndexFilename(lang),
    siteTitle: heroTitle,
    languageSwitcherHtml: buildSitemapLangSwitcher(lang),
  });
 
  // ─── <head> hreflang alternates for all sitemap language variants ───
  const hreflangLinks = [
    ...ALL_LANGUAGES.map(
      (code) =>
        `  <link rel="alternate" hreflang="${code}" href="${BASE_URL}/${getSitemapFilename(code)}">`
    ),
    `  <link rel="alternate" hreflang="x-default" href="${BASE_URL}/sitemap.html">`,
  ].join('\n');
 
  // ─── Pages section (one per supported language) ─────────────────────
  const pagesSection = ALL_LANGUAGES.map((code) => {
    const name = getLocalizedString(LANGUAGE_NAMES, code);
    const flag = getLocalizedString(LANGUAGE_FLAGS, code);
    const href = getIndexFilename(code);
    const pageDesc = getLocalizedString(PAGE_DESCRIPTIONS, code);
    return `          <li>
            <a href="${href}" hreflang="${code}">${flag} ${escapeHTML(name)}</a>
            <span class="link-description">${escapeHTML(pageDesc)}</span>
          </li>`;
  }).join('\n');
 
  // ─── News articles grouped by editorial category ────────────────────
  const articlesByCategory = new Map<ArticleCategory, SitemapArticleInfo[]>();
  for (const article of articleInfos) {
    const category = detectCategory(article.slug ?? article.filename);
    let bucket = articlesByCategory.get(category);
    if (!bucket) {
      bucket = [];
      articlesByCategory.set(category, bucket);
    }
    bucket.push(article);
  }
  // Render in the canonical category order, then any remaining categories
  const orderedCategories: ArticleCategory[] = [
    ...CATEGORY_ORDER.filter((c) => articlesByCategory.has(c)),
    ...[...articlesByCategory.keys()].filter((c) => !CATEGORY_ORDER.includes(c)),
  ];
 
  const articlesSection =
    articleInfos.length === 0
      ? ''
      : orderedCategories
          .map((category) => {
            const bucket = articlesByCategory.get(category) ?? [];
            // Newest first within each category
            bucket.sort((a, b) => b.date.localeCompare(a.date));
            const label = typeLabels[category] ?? category;
            const items = bucket
              .map(
                (a) =>
                  `            <li>
              <a href="news/${escapeHTML(a.filename)}">${escapeHTML(a.title)}</a>
              <span class="sitemap-date">${escapeHTML(a.date)}</span>${a.description ? `\n              <p class="sitemap-desc">${escapeHTML(a.description)}</p>` : ''}
            </li>`
              )
              .join('\n');
            return `        <section class="sitemap-category" aria-labelledby="cat-${category}">
          <h3 id="cat-${category}" class="sitemap-category__heading">${escapeHTML(label)} <span class="sitemap-category__count" aria-label="${bucket.length} ${escapeHTML(copy.statArticlesLabel)}">${bucket.length}</span></h3>
          <ul class="sitemap-list">
${items}
          </ul>
        </section>`;
          })
          .join('\n');
 
  // ─── Documentation section (high-level links) ───────────────────────
  const docsSection = hasDocsDir
    ? `
      <section class="sitemap-section">
        <h2><span aria-hidden="true">📚</span> ${escapeHTML(sections.docs)}</h2>
        <p class="section-description">${escapeHTML(copy.docsDescription)}</p>
        <ul class="sitemap-list">
          <li><a href="docs/">${escapeHTML(docsLabels.docsHome)}</a></li>
          <li><a href="docs/api/">${escapeHTML(docsLabels.api)}</a></li>
          <li><a href="docs/coverage/index.html">${escapeHTML(docsLabels.coverage)}</a></li>
          <li><a href="docs/test-results/index.html">${escapeHTML(docsLabels.testResults)}</a></li>
        </ul>
      </section>`
    : '';
 
  // ─── JSON-LD CollectionPage structured data for SEO ─────────────────
  const jsonLd = {
    '@context': 'https://schema.org',
    '@type': 'CollectionPage',
    name: sitemapTitle,
    url: canonicalUrl,
    description: copy.intro,
    inLanguage: lang,
    isPartOf: {
      '@type': 'WebSite',
      name: 'EU Parliament Monitor',
      url: BASE_URL,
    },
    breadcrumb: {
      '@type': 'BreadcrumbList',
      itemListElement: [
        {
          '@type': 'ListItem',
          position: 1,
          name: copy.home,
          item: `${BASE_URL}/${getIndexFilename(lang)}`,
        },
        {
          '@type': 'ListItem',
          position: 2,
          name: copy.breadcrumbCurrent,
          item: canonicalUrl,
        },
      ],
    },
    mainEntity: {
      '@type': 'ItemList',
      numberOfItems: articleInfos.length,
      name: sections.news,
    },
  };
  // Safely embed JSON-LD: escape the `<` that could start `</script>` sequences
  const jsonLdString = JSON.stringify(jsonLd).replace(/</g, '\\u003c');
 
  return `<!DOCTYPE html>
<html lang="${lang}" dir="${dir}">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-Content-Type-Options" content="nosniff">
  <meta name="referrer" content="no-referrer">
  <title>${escapeHTML(pageTitle)}</title>
  <meta name="description" content="${escapeHTML(description)}">
  <link rel="canonical" href="${canonicalUrl}">
${hreflangLinks}
  <meta property="og:type" content="website">
  <meta property="og:title" content="${escapeHTML(sitemapTitle)}">
  <meta property="og:description" content="${escapeHTML(description)}">
  <meta property="og:url" content="${canonicalUrl}">
  <meta property="og:site_name" content="EU Parliament Monitor">
  <meta property="og:locale" content="${lang}">
  <meta property="og:image" content="https://hack23.github.io/euparliamentmonitor/images/og-image.jpg">
  <meta property="og:image:width" content="1200">
  <meta property="og:image:height" content="630">
  <!-- Favicons -->
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="images/favicon-16x16.png">
  <link rel="apple-touch-icon" sizes="180x180" href="images/apple-touch-icon.png">
  <link rel="manifest" href="site.webmanifest">
  <meta name="theme-color" content="#003399">
  <link rel="stylesheet" href="styles.css">
  <script type="application/ld+json">${jsonLdString}</script>
</head>
<body>
  <a href="#main" class="skip-link">${escapeHTML(skipLinkText)}</a>
 
  ${header}
 
  ${buildPageBanner('')}
 
  <main id="main" class="site-main">
    <section class="sitemap-hero" aria-labelledby="sitemap-heading">
      <h1 id="sitemap-heading">🗺️ ${escapeHTML(sitemapTitle)}</h1>
      <p class="sitemap-hero__subtitle">${escapeHTML(copy.heroSubtitle)}</p>
      <p class="sitemap-hero__intro">${escapeHTML(copy.intro)}</p>
      <dl class="sitemap-stats" aria-label="${escapeHTML(sitemapTitle)}">
        <div class="sitemap-stats__item">
          <dt>${escapeHTML(copy.statArticlesLabel)}</dt>
          <dd>${articleInfos.length}</dd>
        </div>
        <div class="sitemap-stats__item">
          <dt>${escapeHTML(copy.statLanguagesLabel)}</dt>
          <dd>${ALL_LANGUAGES.length}</dd>
        </div>
        <div class="sitemap-stats__item">
          <dt>${escapeHTML(copy.statCategoriesLabel)}</dt>
          <dd>${orderedCategories.length}</dd>
        </div>
        <div class="sitemap-stats__item">
          <dt>${escapeHTML(copy.statLastUpdatedLabel)}</dt>
          <dd><time datetime="${today}">${today}</time></dd>
        </div>
      </dl>
    </section>
 
    <nav class="breadcrumb" aria-label="${escapeHTML(copy.breadcrumbLabel)}">
      <ol>
        <li><a href="${getIndexFilename(lang)}">${escapeHTML(copy.home)}</a></li>
        <li aria-current="page">${escapeHTML(copy.breadcrumbCurrent)}</li>
      </ol>
    </nav>
 
    <div class="sitemap-grid">
      <section class="sitemap-section">
        <h2><span aria-hidden="true">🏠</span> ${escapeHTML(sections.pages)}</h2>
        <p class="section-description">${escapeHTML(copy.pagesDescription)}</p>
        <ul class="sitemap-list">
${pagesSection}
          <li>
            <a href="${getPoliticalIntelligenceFilename(lang)}" hreflang="${lang}">🧭 ${escapeHTML(getLocalizedString(FOOTER_POLITICAL_INTELLIGENCE_LABELS, lang))}</a>
            <span class="link-description">${escapeHTML(copy.politicalIntelligenceLinkDescription)}</span>
          </li>
        </ul>
      </section>
${docsSection}
      <section class="sitemap-section sitemap-section--news">
        <h2><span aria-hidden="true">📰</span> ${escapeHTML(sections.news)}</h2>
        <p class="section-description">${escapeHTML(copy.newsDescription)}</p>
${articlesSection}
      </section>
    </div>
  </main>
 
  ${buildSiteFooter({ lang: lang as LanguageCode, pathPrefix: '', articleCount: articleInfos.length })}${THEME_TOGGLE_SCRIPT}
</body>
</html>`;
}