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 | 642x 2568x 977x 448x 321x 321x 321x 321x 321x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 117x 118x 234x 177x 177x 177x 328x 4592x 4592x 4592x 4592x 328x 328x 335x 335x 335x 335x 335x 335x 4690x 4690x 4690x 4690x | /**
* @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 { BreadcrumbLabels, FooterLabelSet } from '../types/content.js';
import {
BREADCRUMB_TRANSLATIONS,
FOOTER_LABELS,
EVENT_CALENDAR_TITLES,
WATCH_SECTION_TITLES,
LOCALE_MAP,
LANG_DISPLAY,
SITE_FOOTER_LABELS,
ALL_LANG_CODES,
LANG_ARIA_LABELS,
LANG_SWITCHER_ARIA_LABELS,
} from './constants.js';
/**
* 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`;
}
/**
* Sanitize article body content for JSON-LD structured data.
* Removes newlines and normalizes whitespace to prevent invalid JSON.
*/
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
*/
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="${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 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">
<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}">Home</a></li>
<li><a href="${newsPath}">News</a></li>
<li><a href="${dashboardPath}">${labels.dashboard}</a></li>
<li><a href="https://www.hack23.com/cia" target="_blank" rel="noopener noreferrer">CIA Platform</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">Company LinkedIn</a></li>
<li><a href="https://github.com/Hack23/ISMS-PUBLIC" target="_blank" rel="noopener noreferrer">Public ISMS</a></li>
<li><a href="https://github.com/Hack23/ISMS-PUBLIC/blob/main/Information_Security_Policy.md" target="_blank" rel="noopener noreferrer">Security Policy</a></li>
<li><a href="https://github.com/Hack23/ISMS-PUBLIC/blob/main/Privacy_Policy.md" target="_blank" rel="noopener noreferrer">Privacy Policy</a></li>
<li><a href="mailto:info@hack23.com">Contact Us</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}</p>
</div>
</footer>`;
}
|