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 | 17614x 1150x 7475x 2331x 1435x 579x 575x 575x 575x 575x 575x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 137x 138x 274x 269x 269x 269x 601x 8414x 8414x 8414x 8414x 601x 601x 593x 593x 593x 593x 593x 593x 8302x 8302x 8302x 8302x | /**
* @module article-template/helpers
* @description Formatting, sanitisation, and HTML section generators
* for article templates. Includes date formatting, event calendar grid,
* and watch section rendering.
*
* @author Hack23 AB
* @license Apache-2.0
*/
import type { Language } from '../types/language.js';
import type { EventGridItem, WatchPoint } from '../types/article.js';
import type { WhatHappensNextItem, WinnersLosersEntry, FAQItem } from '../types/editorial.js';
import type { BreadcrumbLabels, FooterLabelSet } from '../types/content.js';
import {
BREADCRUMB_TRANSLATIONS,
FOOTER_LABELS,
EVENT_CALENDAR_TITLES,
WATCH_SECTION_TITLES,
WHAT_HAPPENS_NEXT_TITLES,
WINNERS_LOSERS_TITLES,
FAQ_SECTION_TITLES,
SIGNIFICANCE_LABELS,
OUTCOME_LABELS,
LOCALE_MAP,
LANG_DISPLAY,
SITE_FOOTER_LABELS,
ALL_LANG_CODES,
LANG_ARIA_LABELS,
LANG_SWITCHER_ARIA_LABELS,
} from './constants.js';
import { PKG_VERSION } from '../shared/version.js';
import { escapeHtml } from '../html-utils.js';
/**
* Map a language code to its BCP-47 hreflang value.
* Norwegian files use the filename suffix 'no' but must be advertised as 'nb' (Bokmål) per BCP-47.
*/
export function hreflangCode(lang: string): string {
return lang === 'no' ? 'nb' : lang;
}
/**
* Get breadcrumb name for a given language
*/
export function getBreadcrumbName(lang: Language | string, type: keyof BreadcrumbLabels): string {
return BREADCRUMB_TRANSLATIONS[lang as Language]?.[type] || BREADCRUMB_TRANSLATIONS.en[type];
}
/**
* Get footer label for a given language
*/
export function getFooterLabel(lang: Language | string, key: keyof FooterLabelSet): string {
return FOOTER_LABELS[lang as Language]?.[key] || FOOTER_LABELS.en[key];
}
/**
* Get the news index filename for a language (en → index.html, others → index_{lang}.html)
*/
export function getNewsIndexFilename(lang: Language | string): string {
if (lang === 'en') return 'index.html';
return `index_${lang}.html`;
}
/**
* Remove orphaned `</p>` tags that appear immediately after `</ul>` or `</ol>`.
* Browsers auto-close `<p>` before block-level list elements, so AI-generated
* markup of the form `<p>intro</p><ul>…</ul></p>` leaves a dangling `</p>`.
* This function removes only that specific trailing `</p>` and does not attempt
* any other HTML repair.
*/
export function fixHtmlNesting(htmlContent: string): string {
return htmlContent.replace(/<\/(ul|ol)>\s*<\/p>/g, '</$1>');
}
/**
* Sanitize article body content for JSON-LD structured data.
* Removes newlines and normalizes whitespace to prevent invalid JSON.
* Callers should apply {@link fixHtmlNesting} to the raw HTML *before*
* escaping and passing it here, so the regex has a chance to match.
*/
export function sanitizeArticleBody(htmlContent: string): string {
return htmlContent
.substring(0, 500)
.replace(/\n/g, ' ')
.replace(/\s+/g, ' ')
.trim();
}
/**
* Format date for display using locale-appropriate formatting
*/
export function formatDate(date: Date, lang: Language | string = 'en'): string {
const locale: string = LOCALE_MAP[lang] || 'en-GB';
const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' };
try {
return date.toLocaleDateString(locale, options);
} catch {
return date.toLocaleDateString('en-GB', options);
}
}
/**
* Format date range for event calendar title
*/
export function formatDateRange(events: ReadonlyArray<EventGridItem>, lang: Language | string = 'en'): string {
Iif (events.length === 0) return '';
const firstEvent = events[0];
const lastEvent = events[events.length - 1];
Iif (!firstEvent || !lastEvent) return '';
Iif (!firstEvent.date || !lastEvent.date) return '';
const locale: string = LOCALE_MAP[lang] || 'en-GB';
const longOptions: Intl.DateTimeFormatOptions = { month: 'long', day: 'numeric', year: 'numeric' };
const shortOptions: Intl.DateTimeFormatOptions = { month: 'long', day: 'numeric' };
try {
const startDate: string = new Date(firstEvent.date).toLocaleDateString(locale, longOptions);
const endDate: string = new Date(lastEvent.date).toLocaleDateString(locale, shortOptions);
return `${startDate} – ${endDate}`;
} catch {
const startDate: string = new Date(firstEvent.date).toLocaleDateString('en-GB', longOptions);
const endDate: string = new Date(lastEvent.date).toLocaleDateString('en-GB', shortOptions);
return `${startDate} – ${endDate}`;
}
}
/**
* Generate event calendar HTML section
*/
export function generateEventCalendar(events: ReadonlyArray<EventGridItem>, lang: Language = 'en'): string {
const title: string = EVENT_CALENDAR_TITLES[lang] || EVENT_CALENDAR_TITLES.en;
const firstEvt = events[0];
const weekLabel: string = events.length > 0 && firstEvt?.date ?
`${formatDateRange(events, lang)}` : '';
return `
<section class="event-calendar" aria-label="${title}">
<h2>${title}${weekLabel ? `: ${weekLabel}` : ''}</h2>
<div class="calendar-grid">
${events.map(event => ` <div class="calendar-day${event.isToday ? ' today' : ''}" aria-label="${event.dayLabel}">
<div class="day-header">${event.dayName}</div>
<span class="day-date">${event.dayNumber}</span>
<ul class="event-list">
${event.items.map(item => ` <li class="event-item">
<span class="event-time">${item.time}</span>
<span class="event-title">${item.title}</span>
</li>`).join('\n')}
</ul>
</div>`).join('\n')}
</div>
</section>`;
}
/**
* Generate "Watch Section" with key points.
*
* `WatchPoint.title` and `.description` are expected to be **pre-escaped HTML**
* (or trusted HTML containing translation markers such as `svSpan()`).
* Upstream producers (`extractWatchPoints()`) already call `escapeHtml()` and
* may inject `<span data-translate>` markers. AI-pipeline watch points are
* plain text and must be escaped at the *call site* before passing them here
* — see `generators.ts` deep-inspection path.
*/
export function generateWatchSection(watchPoints: ReadonlyArray<WatchPoint>, lang: Language = 'en'): string {
const title: string = WATCH_SECTION_TITLES[lang] || WATCH_SECTION_TITLES.en;
return `
<section class="watch-section">
<h2>${title}</h2>
<ul class="watch-list">
${watchPoints.map(point => ` <li>
<strong>${point.title}:</strong> ${point.description}
</li>`).join('\n')}
</ul>
</section>`;
}
/**
* Generate the article language switcher navigation bar.
*
* @param baseSlug - The article base slug without language suffix (e.g. "2026-02-13-evening-analysis")
* @param currentLang - The current article language
* @returns HTML nav element string
*/
export function generateArticleLanguageSwitcher(baseSlug: string, currentLang: Language | string): string {
const links: string = ALL_LANG_CODES.map(l => {
const display = LANG_DISPLAY[l];
const active: string = l === currentLang ? ' active' : '';
const ariaCurrent: string = l === currentLang ? ' aria-current="page"' : '';
return ` <a href="${baseSlug}-${l}.html" class="lang-link${active}" hreflang="${hreflangCode(l)}"${ariaCurrent}>${display.flag} ${display.name}</a>`;
}).join('\n');
const ariaLabel: string = LANG_SWITCHER_ARIA_LABELS[currentLang as Language] || LANG_SWITCHER_ARIA_LABELS.en;
return ` <nav class="language-switcher" role="navigation" aria-label="${ariaLabel}">\n${links}\n </nav>`;
}
/**
* Generate the "What Happens Next" timeline section.
*
* Renders an ordered list of upcoming legislative pipeline dates with
* significance indicators (high / medium / low). Items with no `date` are
* omitted. The section has class `what-happens-next` so the quality enhancer
* and Schema.org generator can locate it.
*
* @param items - Ordered list of upcoming events
* @param lang - Article language (determines heading and label text)
* @returns HTML `<section>` element string
*/
export function generateWhatHappensNextSection(
items: ReadonlyArray<WhatHappensNextItem>,
lang: Language = 'en',
): string {
if (items.length === 0) return '';
const title: string = WHAT_HAPPENS_NEXT_TITLES[lang] || WHAT_HAPPENS_NEXT_TITLES.en;
const sigLabels = SIGNIFICANCE_LABELS[lang] || SIGNIFICANCE_LABELS.en;
const VALID_SIGNIFICANCE = new Set(['high', 'medium', 'low']);
const rows = items
.filter(item => item.date && item.event)
.map(item => {
const significance = VALID_SIGNIFICANCE.has(item.significance) ? item.significance : 'medium';
const sigClass = `significance-${significance}`;
const sigLabel = sigLabels[significance];
return ` <li class="timeline-item ${sigClass}">
<time class="timeline-date" datetime="${escapeHtml(item.date)}">${escapeHtml(item.date)}</time>
<span class="timeline-event">${escapeHtml(item.event)}</span>
<span class="timeline-significance" aria-label="${escapeHtml(sigLabel)}">${escapeHtml(sigLabel)}</span>
</li>`;
})
.join('\n');
if (!rows) return '';
return `
<section class="what-happens-next" aria-label="${escapeHtml(title)}">
<h2>${escapeHtml(title)}</h2>
<ol class="timeline-list">
${rows}
</ol>
</section>`;
}
/**
* Generate the "Winners & Losers" political analysis section.
*
* Each entry names an actor, classifies their outcome (wins / loses / mixed),
* and provides a one-sentence evidence string. The section has class
* `winners-losers` so downstream validators can detect it.
*
* @param entries - Array of actor outcome entries
* @param lang - Article language
* @returns HTML `<section>` element string
*/
export function generateWinnersLosersSection(
entries: ReadonlyArray<WinnersLosersEntry>,
lang: Language = 'en',
): string {
if (entries.length === 0) return '';
const title: string = WINNERS_LOSERS_TITLES[lang] || WINNERS_LOSERS_TITLES.en;
const outcomeLabels = OUTCOME_LABELS[lang] || OUTCOME_LABELS.en;
const VALID_OUTCOMES = new Set(['wins', 'loses', 'mixed']);
const rows = entries
.filter(e => e.actor && e.evidence)
.map(e => {
const outcome = VALID_OUTCOMES.has(e.outcome) ? e.outcome : 'mixed';
const outcomeClass = `outcome-${outcome}`;
const outcomeLabel = outcomeLabels[outcome];
return ` <li class="wl-entry ${outcomeClass}">
<span class="wl-actor">${escapeHtml(e.actor)}</span>
<span class="wl-outcome">${escapeHtml(outcomeLabel)}</span>
<span class="wl-evidence">${escapeHtml(e.evidence)}</span>
</li>`;
})
.join('\n');
if (!rows) return '';
return `
<section class="winners-losers" aria-label="${escapeHtml(title)}">
<h2>${escapeHtml(title)}</h2>
<ul class="wl-list">
${rows}
</ul>
</section>`;
}
/**
* Generate the FAQ section HTML.
*
* Renders a `<section class="faq-section">` with question/answer pairs in a
* definition-list structure. This HTML is used for in-page display; the
* matching Schema.org FAQPage structured data is emitted separately in
* `generateArticleHTML`.
*
* @param items - Array of FAQ items
* @param lang - Article language
* @returns HTML `<section>` element string (empty string if no items)
*/
export function generateFaqSection(
items: ReadonlyArray<FAQItem>,
lang: Language = 'en',
): string {
if (items.length === 0) return '';
const title: string = FAQ_SECTION_TITLES[lang] || FAQ_SECTION_TITLES.en;
const pairs = items
.filter(item => item.question && item.answer)
.map(item => ` <div class="faq-item">
<dt class="faq-question">${escapeHtml(item.question)}</dt>
<dd class="faq-answer">${escapeHtml(item.answer)}</dd>
</div>`)
.join('\n');
if (!pairs) return '';
return `
<section class="faq-section" aria-label="${escapeHtml(title)}">
<h2>${escapeHtml(title)}</h2>
<dl class="faq-list">
${pairs}
</dl>
</section>`;
}
/**
* Generate the full site footer matching index.html structure.
*
* @param lang - The current language
* @returns HTML footer element string
*/
export function generateSiteFooter(lang: Language | string): string {
const normalizedLang: Language = ALL_LANG_CODES.includes(lang as Language)
? (lang as Language)
: 'en';
const labels = SITE_FOOTER_LABELS[normalizedLang];
const homePath: string = normalizedLang === 'en' ? '../index.html' : `../index_${normalizedLang}.html`;
const newsPath: string = getNewsIndexFilename(normalizedLang);
const dashboardPath: string = normalizedLang === 'en' ? '../dashboard/index.html' : `../dashboard/index_${normalizedLang}.html`;
return `<footer role="contentinfo">
<div class="footer-content">
<div class="footer-section">
<a href="${homePath}" aria-label="Riksdagsmonitor Home">
<img src="../images/riksdagsmonitor-logo.webp" alt="Riksdagsmonitor" class="footer-logo" width="80" height="80" loading="lazy">
</a>
<h3>${labels.about}</h3>
<p>${labels.aboutText}</p>
<ul class="footer-stats">
<li>${labels.statMPs}</li>
<li>${labels.statRiskRules}</li>
<li>${labels.statLanguages}</li>
<li>${labels.statHistoricalData}</li>
</ul>
</div>
<div class="footer-section">
<h3>${labels.quickLinks}</h3>
<ul>
<li><a href="${homePath}">${labels.home}</a></li>
<li><a href="${newsPath}">${labels.news}</a></li>
<li><a href="${dashboardPath}">${labels.dashboard}</a></li>
<li><a href="https://www.hack23.com/cia" target="_blank" rel="noopener noreferrer">${labels.ciaplatform}</a></li>
<li><a href="https://github.com/Hack23/riksdagsmonitor" target="_blank" rel="noopener noreferrer">GitHub Repository</a></li>
<li><a href="https://www.riksdagen.se" target="_blank" rel="noopener noreferrer">Sveriges Riksdag</a></li>
</ul>
</div>
<div class="footer-section">
<h3>${labels.builtBy}</h3>
<p>${labels.builtByText}</p>
<ul>
<li><a href="https://www.hack23.com" target="_blank" rel="noopener noreferrer">Hack23.com</a></li>
<li><a href="https://www.linkedin.com/company/hack23/" target="_blank" rel="noopener noreferrer">${labels.companyLinkedin}</a></li>
<li><a href="https://github.com/Hack23/ISMS-PUBLIC" target="_blank" rel="noopener noreferrer">${labels.publicIsms}</a></li>
<li><a href="https://github.com/Hack23/ISMS-PUBLIC/blob/main/Information_Security_Policy.md" target="_blank" rel="noopener noreferrer">${labels.securityPolicy}</a></li>
<li><a href="https://github.com/Hack23/ISMS-PUBLIC/blob/main/Privacy_Policy.md" target="_blank" rel="noopener noreferrer">${labels.privacyPolicy}</a></li>
<li><a href="mailto:info@hack23.com">${labels.contactUs}</a></li>
</ul>
</div>
<div class="footer-section">
<h3>${labels.languages}</h3>
<div class="language-grid">
${ALL_LANG_CODES.map(l => {
const display = LANG_DISPLAY[l];
const ariaLabel = LANG_ARIA_LABELS[l];
const href: string = l === 'en' ? '../index.html' : `../index_${l}.html`;
return ` <a href="${href}" title="${display.name}" aria-label="${ariaLabel}"><span aria-hidden="true">${display.flag}</span> ${l.toUpperCase()}</a>`;
}).join('\n')}
</div>
</div>
</div>
<div class="footer-bottom">
<p>© 2008-<time datetime="2026">2026</time> <a href="https://www.hack23.com" target="_blank" rel="noopener noreferrer">Hack23 AB</a> (Org.nr 5595347807) | ${labels.location} | v${escapeHtml(String(PKG_VERSION))}</p>
<p class="footer-disclaimer">⚠️ ${labels.disclaimer} <a href="https://github.com/Hack23/riksdagsmonitor/issues" target="_blank" rel="noopener noreferrer">${labels.disclaimerLink}</a>.</p>
</div>
</footer>`;
}
|