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 | 6x 6x 1160x 40x 560x 560x 560x 560x 560x 560x 560x 560x 560x 17x 17x 17x 17x 17x 17x 40x 560x 560x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 17x 40x 17x 40x 40x 40x 17x 17x 40x 16x 16x 16x 16x 40x 40x 40x 40x 40x 17x 17x 17x 40x 240x 40x 40x 240x 40x 40x 40x 40x 40x | // SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module Generators/NewsIndexes/PerLanguage
* @description Pure HTML composer for per-language news-index pages. No
* file I/O — `generateIndexHTML(lang, articles, metaMap)` returns the
* full HTML document for `index.html` / `index-<lang>.html`. Split out of
* the monolithic `news-indexes.ts` so the HTML/SEO surface can be
* regression-tested independently of discovery and write logic.
*/
import { APP_VERSION, BUILD_SHORT, BUILD_TIME, BASE_URL } from '../../constants/config.js';
import { getNewsIndexSeo } from '../seo-copy.js';
import { buildHeadFreshnessTags } from '../../constants/build-info-meta.js';
import {
ALL_LANGUAGES,
LANGUAGE_NAMES,
LANGUAGE_FLAGS,
PAGE_TITLES,
PAGE_DESCRIPTIONS,
SECTION_HEADINGS,
NO_ARTICLES_MESSAGES,
SKIP_LINK_TEXTS,
AI_SECTION_CONTENT,
FILTER_LABELS,
ARTICLE_TYPE_LABELS,
HEADER_SUBTITLE_LABELS,
getLocalizedString,
getTextDirection,
} from '../../constants/languages.js';
import {
buildOgLocaleTags,
ORG_SAME_AS,
buildTwitterAttributionTags,
} from '../../constants/seo/index.js';
import {
buildResponsiveBannerPicture,
buildResponsiveIconLinks,
buildResponsiveSocialImageMeta,
buildSiteFooter,
buildSiteHeader,
} from '../../templates/section-builders.js';
import { formatSlug, escapeHTML } from '../../utils/file-utils.js';
import { detectCategory } from '../../utils/article-category.js';
import type {
ParsedArticle,
ArticleCategoryLabels,
ArticleCategory,
LanguageCode,
} from '../../types/index.js';
const SCHEMA_ORG = 'https://schema.org';
const SITE_NAME = 'EU Parliament Monitor';
/**
* Get the index filename for a given language code.
* English uses index.html (the primary homepage), others use index-{lang}.html.
*
* @param lang - Language code
* @returns Filename string
*/
export function getIndexFilename(lang: string): string {
return lang === 'en' ? 'index.html' : `index-${lang}.html`;
}
/**
* Build the compact language switcher nav HTML.
* Uses flag emoji + language code, riksdagsmonitor style.
*
* @param currentLang - Active language code
* @returns HTML string
*/
function buildLangSwitcher(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 = getIndexFilename(code);
const current = code === currentLang ? ' aria-current="page"' : '';
const safeHref = escapeHTML(href);
const safeCode = escapeHTML(code);
const safeName = escapeHTML(name);
return `<a href="${safeHref}" class="lang-link${active}" hreflang="${safeCode}" lang="${safeCode}" title="${safeName}" aria-label="${safeName}"${current}>${flag} ${code.toUpperCase()}</a>`;
}).join('\n ');
}
/**
* Render a single news card element.
*
* @param article - Parsed article data
* @param meta - Real title and description extracted from the article HTML
* @param meta.title - Article title
* @param meta.description - Article description/excerpt
* @param categoryLabels - Optional localized article category labels
* @returns HTML string for one card
*/
function renderCard(
article: ParsedArticle,
meta: { title: string; description: string },
categoryLabels?: ArticleCategoryLabels
): string {
const category = detectCategory(article.slug);
const safeCategory = String(category).replace(/[^a-z0-9-]/gi, '');
const title = escapeHTML(meta.title || formatSlug(article.slug));
const badgeLabel = categoryLabels?.[category] ?? formatSlug(safeCategory);
const excerpt = meta.description
? `\n <p class="news-card__excerpt">${escapeHTML(meta.description)}</p>`
: '';
return `
<li class="news-card">
<a href="news/${escapeHTML(article.filename)}" class="news-card__link" lang="${escapeHTML(article.lang)}" hreflang="${escapeHTML(article.lang)}">
<div class="news-card__accent news-card__accent--${safeCategory}"></div>
<div class="news-card__body">
<div class="news-card__meta">
<span class="news-card__badge news-card__badge--${safeCategory}">${escapeHTML(badgeLabel)}</span>
<time class="news-card__date" datetime="${escapeHTML(article.date)}">${escapeHTML(article.date)}</time>
</div>
<h3 class="news-card__title">${title}</h3>${excerpt}
</div>
</a>
</li>`;
}
/**
* Build hreflang alternate link tags for SEO multi-language support.
*
* @returns HTML string of link elements
*/
function buildHreflangTags(): string {
const links = ALL_LANGUAGES.map((code) => {
const href = getIndexFilename(code);
return `<link rel="alternate" hreflang="${code}" href="${BASE_URL}/${href}">`;
});
links.push(`<link rel="alternate" hreflang="x-default" href="${BASE_URL}/index.html">`);
return links.join('\n ');
}
/**
* Generate index HTML for a language.
*
* Produces a complete, standards-compliant HTML5 page with:
* - Sticky header with EU branding
* - Compact language switcher with flag + code
* - Hero section with page title and description
* - Responsive card grid for news articles
* - Accessible empty state when no articles exist
* - Hack23 AB multi-section footer (About, Quick Links, Built by Hack23, Languages)
*
* @param lang - Language code
* @param articles - Articles for this language
* @param metaMap - Map of article filename to real title and description
* @returns Complete HTML document
*/
export function generateIndexHTML(
lang: string,
articles: ParsedArticle[],
metaMap: Map<string, { title: string; description: string }> = new Map()
): string {
const title = getLocalizedString(PAGE_TITLES, lang);
const description = getLocalizedString(PAGE_DESCRIPTIONS, lang);
const heading = getLocalizedString(SECTION_HEADINGS, lang);
const noArticlesText = getLocalizedString(NO_ARTICLES_MESSAGES, lang);
const skipLinkText = getLocalizedString(SKIP_LINK_TEXTS, lang);
const dir = getTextDirection(lang);
const selfHref = getIndexFilename(lang);
const heroTitle = title.split(' - ')[0] ?? 'EU Parliament Monitor';
const filterLabels = getLocalizedString(FILTER_LABELS, lang) as { all: string; search: string };
const categoryLabels = getLocalizedString(ARTICLE_TYPE_LABELS, lang) as ArticleCategoryLabels;
const usedCategories = new Set<ArticleCategory>();
for (const a of articles) {
usedCategories.add(detectCategory(a.slug));
}
const content =
articles.length === 0
? `
<div class="empty-state">
<div class="empty-state__icon" aria-hidden="true">📰</div>
<p class="empty-state__text">${noArticlesText}</p>
</div>`
: `
<ul class="news-grid" role="list">
${articles
.map((a) =>
renderCard(
a,
metaMap.get(a.filename) ?? { title: formatSlug(a.slug), description: '' },
categoryLabels
)
)
.join('\n')}
</ul>`;
const ai = getLocalizedString(AI_SECTION_CONTENT, lang);
const categoryCounts = new Map<ArticleCategory, number>();
for (const a of articles) {
const cat = detectCategory(a.slug);
categoryCounts.set(cat, (categoryCounts.get(cat) ?? 0) + 1);
}
const filterButtons =
articles.length > 0
? Array.from(usedCategories)
.sort()
.map((cat) => {
const safeCat = String(cat).replace(/[^a-z0-9-]/gi, '');
const label = categoryLabels[cat] ?? formatSlug(safeCat);
const count = categoryCounts.get(cat) ?? 0;
return `<button type="button" class="filter-btn" data-category="${safeCat}">${escapeHTML(label)}<span class="filter-btn__count">${count}</span></button>`;
})
.join('\n ')
: '';
const seo = getNewsIndexSeo(lang);
const canonicalUrl = `${BASE_URL}/${selfHref}`;
const websiteJsonLd = JSON.stringify({
'@context': SCHEMA_ORG,
'@type': 'WebSite',
name: SITE_NAME,
url: BASE_URL,
inLanguage: lang,
publisher: { '@id': `${BASE_URL}/#organization` },
}).replace(/</g, '\\u003c');
const organizationJsonLd = JSON.stringify({
'@context': SCHEMA_ORG,
'@type': 'NewsMediaOrganization',
'@id': `${BASE_URL}/#organization`,
name: 'Hack23 AB',
url: 'https://hack23.com',
logo: {
'@type': 'ImageObject',
url: 'https://hack23.com/icon-192.png',
width: 192,
height: 192,
},
sameAs: [...ORG_SAME_AS],
}).replace(/</g, '\\u003c');
const collectionPageJsonLd = JSON.stringify({
'@context': SCHEMA_ORG,
'@type': 'CollectionPage',
name: heroTitle,
description,
url: canonicalUrl,
inLanguage: lang,
isPartOf: { '@type': 'WebSite', name: SITE_NAME, url: BASE_URL },
publisher: { '@id': `${BASE_URL}/#organization` },
datePublished: BUILD_TIME,
dateModified: BUILD_TIME,
breadcrumb: {
'@type': 'BreadcrumbList',
itemListElement: [
{
'@type': 'ListItem',
position: 1,
name: seo.breadcrumbHome,
item: `${BASE_URL}/${selfHref}`,
},
{ '@type': 'ListItem', position: 2, name: seo.breadcrumbCurrent, item: canonicalUrl },
],
},
mainEntity: {
'@type': 'ItemList',
numberOfItems: Math.min(articles.length, 50),
itemListElement: articles.slice(0, 50).map((a, idx) => {
const url = `${BASE_URL}/news/${a.filename}`;
const headline = metaMap.get(a.filename)?.title ?? formatSlug(a.slug);
return {
'@type': 'ListItem',
position: idx + 1,
url,
item: {
'@type': 'NewsArticle',
'@id': url,
url,
headline,
name: headline,
datePublished: a.date,
inLanguage: a.lang,
},
};
}),
},
}).replace(/</g, '\\u003c');
const faqJsonLd = JSON.stringify({
'@context': SCHEMA_ORG,
'@type': 'FAQPage',
inLanguage: seo.faqLanguage,
mainEntity: seo.faqs.map((f) => ({
'@type': 'Question',
name: f.q,
acceptedAnswer: { '@type': 'Answer', text: f.a },
})),
}).replace(/</g, '\\u003c');
const breadcrumbHtml = `<nav class="breadcrumb" aria-label="${escapeHTML(seo.breadcrumbAriaLabel)}">
<ol class="breadcrumb__list">
<li class="breadcrumb__item"><a href="${selfHref}">${escapeHTML(seo.breadcrumbHome)}</a></li>
<li class="breadcrumb__item breadcrumb__item--current" aria-current="page">${escapeHTML(seo.breadcrumbCurrent)}</li>
</ol>
</nav>`;
const faqHtml = `<section class="page-faq" aria-labelledby="page-faq-heading">
<h2 id="page-faq-heading"><span aria-hidden="true">❓</span> ${escapeHTML(seo.faqHeading)}</h2>
<div class="page-faq__list">
${seo.faqs
.map(
(f) => `<details class="page-faq__item">
<summary>${escapeHTML(f.q)}</summary>
<p>${escapeHTML(f.a)}</p>
</details>`
)
.join('\n ')}
</div>
</section>`;
const header = buildSiteHeader({
lang: lang as LanguageCode,
pathPrefix: '',
homeHref: selfHref,
siteTitle: heroTitle,
languageSwitcherHtml: buildLangSwitcher(lang),
});
const ogLocaleTags = buildOgLocaleTags(lang);
const twitterAttribution = buildTwitterAttributionTags();
const twitterAttributionBlock = twitterAttribution ? `\n${twitterAttribution}` : '';
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">
<meta name="generator" content="EU Parliament Monitor v${escapeHTML(APP_VERSION)}">
<title>${title}</title>
<meta name="description" content="${description}">
<meta name="keywords" content="${escapeHTML(seo.keywords)}">
<meta name="author" content="Hack23 AB">
<meta name="publisher" content="Hack23 AB">
<meta name="robots" content="index, follow, max-snippet:-1, max-image-preview:large">
<meta http-equiv="Content-Language" content="${lang}">
<link rel="canonical" href="${canonicalUrl}">
<link rel="preconnect" href="https://hack23.com" crossorigin>
<meta property="og:type" content="website">
<meta property="og:title" content="${heroTitle}">
<meta property="og:description" content="${description}">
<meta property="og:url" content="${canonicalUrl}">
<meta property="og:site_name" content="EU Parliament Monitor">
${ogLocaleTags}
${buildResponsiveSocialImageMeta(seo.ogImageAlt)}
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="${heroTitle}">
<meta name="twitter:description" content="${description}">${twitterAttributionBlock}
${buildHreflangTags()}
<!-- Favicons -->
${buildResponsiveIconLinks('')}
<link rel="manifest" href="site.webmanifest">
<meta name="color-scheme" content="light dark">
<meta name="theme-color" content="#003399" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#0a1a38" media="(prefers-color-scheme: dark)">
<link rel="alternate" type="application/rss+xml" title="EU Parliament Monitor RSS" href="rss.xml">
<link rel="stylesheet" href="styles.css?v=${BUILD_SHORT}">
${buildHeadFreshnessTags('')}
<script type="application/ld+json">${websiteJsonLd}</script>
<script type="application/ld+json">${organizationJsonLd}</script>
<script type="application/ld+json">${collectionPageJsonLd}</script>
<script type="application/ld+json">${faqJsonLd}</script>
</head>
<body>
<a href="#main" class="skip-link">${skipLinkText}</a>
${header}
${breadcrumbHtml}
<section class="hero">
<div class="hero__inner">
<div class="hero__content">
<p class="hero__kicker">${escapeHTML(getLocalizedString(HEADER_SUBTITLE_LABELS, lang))}</p>
<h1 class="hero__title">${heroTitle}</h1>
<p class="hero__description">${description}</p>
</div>
${buildResponsiveBannerPicture({
pathPrefix: '',
pictureClass: 'hero__banner',
imageClass: 'hero__banner-img',
alt: 'EU Parliament Monitor — AI-Disrupted Parliamentary Intelligence',
sizes: '100vw',
})}
</div>
</section>
<main id="main" class="site-main">
<h2 class="section-heading"><span class="section-heading__icon" aria-hidden="true">📋</span> ${heading}</h2>${
articles.length > 0
? `
<div class="filter-toolbar" role="toolbar" aria-label="Filter articles">
<div class="filter-buttons">
<button type="button" class="filter-btn active" data-category="all">${escapeHTML(filterLabels.all)}<span class="filter-btn__count">${articles.length}</span></button>
${filterButtons}
</div>
<div class="filter-search">
<input type="search" class="filter-search__input" placeholder="${escapeHTML(filterLabels.search)}" aria-label="${escapeHTML(filterLabels.search)}">
</div>
</div>`
: ''
}
${content}
</main>
<section class="ai-intelligence" aria-labelledby="ai-heading">
<h2 id="ai-heading"><span aria-hidden="true">🤖</span> ${escapeHTML(ai.heading)}</h2>
<blockquote class="ai-intelligence__quote">${escapeHTML(ai.quote)}</blockquote>
<p>${escapeHTML(ai.description)}</p>
<ul class="ai-intelligence__features">
<li><strong>${escapeHTML(ai.featureAgents)}</strong> — ${escapeHTML(ai.featureAgentsDesc)}</li>
<li><strong>${escapeHTML(ai.featureSchedule)}</strong> — ${escapeHTML(ai.featureScheduleDesc)}</li>
<li><strong>${escapeHTML(ai.featureHuman)}</strong> — ${escapeHTML(ai.featureHumanDesc)}</li>
<li><strong>${escapeHTML(ai.featureData)}</strong> — ${escapeHTML(ai.featureDataDesc)}</li>
</ul>
</section>
${faqHtml}
${buildSiteFooter({ lang: lang as LanguageCode, pathPrefix: '', articleCount: articles.length })}
<script src="js/index-runtime.js" defer></script>
</body>
</html>`;
}
|