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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 25824x 2x 2x 2x 2x 2x 4138x 4x 4134x 4128x 4124x 25824x 25824x 25824x 25824x 10176x 10176x 10176x 16x 16x 16x 15216x 16x 16x 16x 14960x 14960x 14960x 14960x 14960x 14960x 14960x 1216x 13744x 13744x 14960x 16x 16x 16x 16x 112x 112x 9968x 9968x 96x 9872x 9840x 9840x 16x 16x 16x 25664x 25664x 220720x 25664x 25664x 16x 16x 224x 16x 16x 224x 208x 208x 208x 208x 16x 16x 224x 224x 16x 16x 224x 208x 208x 208x 208x 208x 208x 16x 16x 16x 16x 16x 208x 208x 16x 16x 176x 176x 176x 16x 16x 16x 16x 160x 160x 160x 16x 16x 16x 1216x 33552x 30416x 30240x 14960x 1216x 1216x 14960x 14960x 16x 16x 16x 16x 9840x 9840x 9840x 16x 16x 8x 8x 1x 7x 1x 6x 8x 8x 1x 5x 1x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | /**
* @module Infrastructure/SEO
* @category Intelligence Operations / Supporting Infrastructure
* @name Sitemap Generation - Multi-Language SEO Infrastructure
*
* @description
* Automated XML sitemap generation system producing search engine-optimized sitemaps
* for all 14 language variants of the Riksdagsmonitor political intelligence platform.
*
* @author Hack23 AB (Infrastructure Team)
* @license Apache-2.0
* @version 2.1.0
*/
import { execSync } from 'child_process';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import type { Language } from './types/language.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log('πΊοΈ Sitemap Generation Script');
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
const BASE_URL = 'https://riksdagsmonitor.com';
const NEWS_DIR = path.join(__dirname, '..', 'news');
const API_DIR = path.join(__dirname, '..', 'api');
const ROOT_DIR = path.join(__dirname, '..');
const SITEMAP_FILE = path.join(ROOT_DIR, 'sitemap.xml');
// Language codes
const LANGUAGES: readonly Language[] = ['en', 'sv', 'da', 'no', 'fi', 'de', 'fr', 'es', 'nl', 'ar', 'he', 'ja', 'ko', 'zh'];
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface ArticleGroup {
baseSlug: string;
languages: string[];
lastmod: string;
}
interface ApiDoc {
file: string;
path: string;
lastmod: string;
}
interface HreflangAlternate {
lang: string;
href: string;
}
// ---------------------------------------------------------------------------
// Functions
// ---------------------------------------------------------------------------
/**
* Cache of git commit timestamps keyed by relative file path.
* Populated once via loadGitTimestamps() to avoid per-file git calls.
*/
const gitTimestampCache = new Map<string, string>();
let gitTimestampsLoaded = false;
/**
* Preload git commit timestamps for all tracked files in a single git call.
* Parses `git log --name-only` output to map each file to its most recent commit timestamp.
*/
function loadGitTimestamps(): void {
if (gitTimestampsLoaded) return;
gitTimestampsLoaded = true;
try {
const output = execSync('git log --format="COMMIT %cI" --name-only --diff-filter=ACMR', {
cwd: ROOT_DIR,
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe'],
maxBuffer: 10 * 1024 * 1024,
});
let currentTimestamp = '';
for (const line of output.split('\n')) {
if (line.startsWith('COMMIT ')) {
currentTimestamp = new Date(line.substring(7)).toISOString();
} else if (line.trim() && currentTimestamp) {
// Only keep the first (most recent) timestamp per file
if (!gitTimestampCache.has(line)) {
gitTimestampCache.set(line, currentTimestamp);
}
}
}
} catch (_error: unknown) {
// git not available β getFileModTime will fall back to fs.statSync
console.warn('β οΈ Git timestamps unavailable β falling back to filesystem mtime');
}
}
/**
* Get file modification time from git history for deterministic timestamps.
* Falls back to filesystem mtime only when git history is unavailable.
*/
function getFileModTime(filePath: string): string {
loadGitTimestamps();
const relativePath = path.relative(ROOT_DIR, filePath).split(path.sep).join('/');
const cached = gitTimestampCache.get(relativePath);
if (cached) return cached;
try {
const stats = fs.statSync(filePath);
return stats.mtime.toISOString();
} catch (_error: unknown) {
return new Date().toISOString();
}
}
/**
* Get news articles with metadata.
*/
function getNewsArticles(): ArticleGroup[] {
console.log('π° Scanning news directory...');
Iif (!fs.existsSync(NEWS_DIR)) {
console.warn('β οΈ News directory not found');
return [];
}
const files = fs
.readdirSync(NEWS_DIR)
.filter((file) => file.endsWith('.html') && file !== 'index.html' && !file.startsWith('index_'));
console.log(` Found ${files.length} news articles`);
// Group articles by base slug (without language suffix)
const articles = new Map<string, ArticleGroup>();
files.forEach((file) => {
const match = file.match(/^(.+?)-(en|sv|da|no|fi|de|fr|es|nl|ar|he|ja|ko|zh)\.html$/);
Eif (match) {
const baseSlug = match[1]!;
const lang = match[2]!;
const filePath = path.join(NEWS_DIR, file);
const fileModTime = getFileModTime(filePath);
if (!articles.has(baseSlug)) {
articles.set(baseSlug, {
baseSlug,
languages: [],
lastmod: fileModTime,
});
} else {
const article = articles.get(baseSlug)!;
Iif (!article.lastmod || new Date(fileModTime) > new Date(article.lastmod)) {
article.lastmod = fileModTime;
}
}
articles.get(baseSlug)!.languages.push(lang);
}
});
return Array.from(articles.values());
}
/**
* Get API documentation files (supports TypeDoc nested directory structure).
*/
function getApiDocs(): ApiDoc[] {
console.log('π Scanning API documentation directory...');
Iif (!fs.existsSync(API_DIR)) {
console.warn('β οΈ API directory not found');
return [];
}
const results: ApiDoc[] = [];
function scanDir(dir: string): void {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory() && entry.name !== 'assets') {
scanDir(fullPath);
} else if (entry.isFile() && entry.name.endsWith('.html')) {
const relativePath = path.relative(API_DIR, fullPath).replace(/\\/g, '/');
results.push({
file: relativePath,
path: fullPath,
lastmod: getFileModTime(fullPath),
});
}
}
}
scanDir(API_DIR);
console.log(` Found ${results.length} API documentation files`);
return results;
}
/**
* Generate XML for a URL entry.
*/
function generateUrlEntry(
loc: string,
lastmod: string,
changefreq: string,
priority: string,
alternates: HreflangAlternate[] = [],
): string {
let xml = `
<url>
<loc>${BASE_URL}/${loc}</loc>
<lastmod>${lastmod}</lastmod>
<changefreq>${changefreq}</changefreq>
<priority>${priority}</priority>`;
alternates.forEach((alt) => {
xml += `
<xhtml:link rel="alternate" hreflang="${alt.lang}" href="${BASE_URL}/${alt.href}"/>`;
});
xml += `
</url>`;
return xml;
}
/**
* Generate sitemap XML.
*/
function generateSitemap(): string {
console.log('π¨ Generating sitemap...');
let xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">`;
// Main index page with all language alternates
const indexAlternates: HreflangAlternate[] = LANGUAGES.map((lang) => ({
lang,
href: lang === 'en' ? 'index.html' : `index_${lang}.html`,
}));
const indexMtime = getFileModTime(path.join(ROOT_DIR, 'index.html'));
xml += generateUrlEntry('index.html', indexMtime, 'daily', '1.0', indexAlternates);
// Individual language index pages (excluding English)
LANGUAGES.filter((lang) => lang !== 'en').forEach((lang) => {
const loc = `index_${lang}.html`;
const lastmod = getFileModTime(path.join(ROOT_DIR, loc));
const priority = lang === 'sv' ? '0.9' : '0.7';
xml += generateUrlEntry(loc, lastmod, 'daily', priority);
});
// Politician dashboard page
const politicianDashboardMtime = getFileModTime(path.join(ROOT_DIR, 'politician-dashboard.html'));
xml += generateUrlEntry('politician-dashboard.html', politicianDashboardMtime, 'weekly', '0.8');
// Dashboard pages with all language alternates (only for existing files)
const dashboardAlternates: HreflangAlternate[] = LANGUAGES.map((lang) => ({
lang,
href: lang === 'en' ? 'dashboard/index.html' : `dashboard/index_${lang}.html`,
})).filter((alt) => fs.existsSync(path.join(ROOT_DIR, alt.href)));
const dashboardEnMtime = getFileModTime(path.join(ROOT_DIR, 'dashboard', 'index.html'));
xml += generateUrlEntry('dashboard/index.html', dashboardEnMtime, 'weekly', '0.8', dashboardAlternates);
// All other language dashboard pages
LANGUAGES.filter((lang) => lang !== 'en').forEach((lang) => {
const loc = `dashboard/index_${lang}.html`;
const dashboardPath = path.join(ROOT_DIR, 'dashboard', `index_${lang}.html`);
Eif (fs.existsSync(dashboardPath)) {
const lastmod = getFileModTime(dashboardPath);
const priority = lang === 'sv' ? '0.8' : '0.7';
xml += generateUrlEntry(loc, lastmod, 'weekly', priority);
}
});
// Sitemap HTML pages with language alternates
const sitemapAlternates: HreflangAlternate[] = [
{ lang: 'en', href: 'sitemap.html' },
{ lang: 'sv', href: 'sitemap_sv.html' },
{ lang: 'da', href: 'sitemap_da.html' },
{ lang: 'no', href: 'sitemap_no.html' },
{ lang: 'fi', href: 'sitemap_fi.html' },
{ lang: 'de', href: 'sitemap_de.html' },
{ lang: 'fr', href: 'sitemap_fr.html' },
{ lang: 'es', href: 'sitemap_es.html' },
{ lang: 'nl', href: 'sitemap_nl.html' },
{ lang: 'ar', href: 'sitemap_ar.html' },
{ lang: 'he', href: 'sitemap_he.html' },
{ lang: 'ja', href: 'sitemap_ja.html' },
{ lang: 'ko', href: 'sitemap_ko.html' },
{ lang: 'zh', href: 'sitemap_zh.html' },
{ lang: 'x-default', href: 'sitemap.html' },
];
const sitemapEnMtime = getFileModTime(path.join(ROOT_DIR, 'sitemap.html'));
xml += generateUrlEntry('sitemap.html', sitemapEnMtime, 'monthly', '0.6', sitemapAlternates);
// Individual sitemap language pages (excluding English)
const sitemapLangPages: Array<{ file: string; priority: string }> = [
{ file: 'sitemap_sv.html', priority: '0.5' },
{ file: 'sitemap_da.html', priority: '0.4' },
{ file: 'sitemap_no.html', priority: '0.4' },
{ file: 'sitemap_fi.html', priority: '0.4' },
{ file: 'sitemap_de.html', priority: '0.4' },
{ file: 'sitemap_fr.html', priority: '0.4' },
{ file: 'sitemap_es.html', priority: '0.4' },
{ file: 'sitemap_nl.html', priority: '0.4' },
{ file: 'sitemap_ar.html', priority: '0.4' },
{ file: 'sitemap_he.html', priority: '0.4' },
{ file: 'sitemap_ja.html', priority: '0.4' },
{ file: 'sitemap_ko.html', priority: '0.4' },
{ file: 'sitemap_zh.html', priority: '0.4' },
];
sitemapLangPages.forEach(({ file, priority }) => {
const lastmod = getFileModTime(path.join(ROOT_DIR, file));
xml += generateUrlEntry(file, lastmod, 'monthly', priority);
});
// News index pages
const newsLangFiles = [
'index.html', 'index_sv.html', 'index_da.html', 'index_no.html', 'index_fi.html',
'index_de.html', 'index_fr.html', 'index_es.html', 'index_nl.html', 'index_ar.html', 'index_he.html',
];
const newsIndexMtimes = newsLangFiles.map((file) => {
try {
return new Date(getFileModTime(path.join(NEWS_DIR, file)));
} catch (_e: unknown) {
return new Date(0);
}
});
const newsIndexMaxMtime = new Date(Math.max(...newsIndexMtimes.map((d) => d.getTime()))).toISOString();
// Build alternates for news index pages that actually exist
const newsIndexAlternates: HreflangAlternate[] = [
{ lang: 'en', href: 'news/' },
{ lang: 'sv', href: 'news/index_sv.html' },
{ lang: 'da', href: 'news/index_da.html' },
{ lang: 'no', href: 'news/index_no.html' },
{ lang: 'fi', href: 'news/index_fi.html' },
{ lang: 'de', href: 'news/index_de.html' },
{ lang: 'fr', href: 'news/index_fr.html' },
{ lang: 'es', href: 'news/index_es.html' },
{ lang: 'nl', href: 'news/index_nl.html' },
{ lang: 'ar', href: 'news/index_ar.html' },
{ lang: 'he', href: 'news/index_he.html' },
{ lang: 'x-default', href: 'news/' },
];
xml += generateUrlEntry('news/', newsIndexMaxMtime, 'daily', '0.9', newsIndexAlternates);
// Add individual entries for each news language page (excluding EN)
const newsLanguagePages: Array<{ file: string; priority: string }> = [
{ file: 'index_sv.html', priority: '0.9' },
{ file: 'index_da.html', priority: '0.7' },
{ file: 'index_no.html', priority: '0.7' },
{ file: 'index_fi.html', priority: '0.7' },
{ file: 'index_de.html', priority: '0.7' },
{ file: 'index_fr.html', priority: '0.7' },
{ file: 'index_es.html', priority: '0.7' },
{ file: 'index_nl.html', priority: '0.7' },
{ file: 'index_ar.html', priority: '0.7' },
{ file: 'index_he.html', priority: '0.7' },
];
newsLanguagePages.forEach(({ file, priority }) => {
try {
const lastmod = getFileModTime(path.join(NEWS_DIR, file));
xml += generateUrlEntry(`news/${file}`, lastmod, 'daily', priority);
} catch (_e: unknown) {
// File doesn't exist yet, skip
}
});
// News articles
const articles = getNewsArticles();
console.log(` Processing ${articles.length} article groups...`);
articles.forEach((article) => {
const sortedLanguages = [...article.languages].sort((a, b) => {
if (a === 'en') return -1;
if (b === 'en') return 1;
return a.localeCompare(b);
});
const alternates: HreflangAlternate[] = sortedLanguages.map((altLang) => ({
lang: altLang,
href: `news/${article.baseSlug}-${altLang}.html`,
}));
alternates.push({
lang: 'x-default',
href: `news/${article.baseSlug}-${sortedLanguages[0]}.html`,
});
sortedLanguages.forEach((lang) => {
const loc = `news/${article.baseSlug}-${lang}.html`;
xml += generateUrlEntry(loc, article.lastmod, 'monthly', '0.8', alternates);
});
});
// API Documentation (TypeDoc generated)
const apiDocs = getApiDocs();
Eif (apiDocs.length > 0) {
console.log(` Processing ${apiDocs.length} API documentation files...`);
apiDocs.forEach((doc) => {
const loc = `api/${doc.file}`;
const priority = doc.file === 'index.html' ? '0.7' : '0.5';
xml += generateUrlEntry(loc, doc.lastmod, 'weekly', priority);
});
}
xml += `
</urlset>`;
return xml;
}
/**
* Validate sitemap XML.
*/
function validateSitemap(xml: string): boolean {
console.log('β
Validating sitemap...');
if (!xml.includes('<?xml version="1.0"')) {
throw new Error('Invalid XML declaration');
}
if (!xml.includes('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"')) {
throw new Error('Invalid sitemap namespace');
}
const urlCount = (xml.match(/<url>/g) || []).length;
console.log(` Found ${urlCount} URLs in sitemap`);
if (urlCount === 0) {
throw new Error('No URLs in sitemap');
}
if (!xml.includes('<loc>')) {
throw new Error('Missing <loc> tags');
}
console.log(' β
Sitemap validation passed');
return true;
}
/**
* Main function.
*/
function main(): number {
try {
console.log('π Starting sitemap generation...\n');
const sitemap = generateSitemap();
validateSitemap(sitemap);
fs.writeFileSync(SITEMAP_FILE, sitemap, 'utf8');
console.log(`\nβ
Sitemap written to: ${SITEMAP_FILE}`);
const stats = fs.statSync(SITEMAP_FILE);
console.log(` File size: ${(stats.size / 1024).toFixed(2)} KB`);
return 0;
} catch (error: unknown) {
console.error('β Error generating sitemap:', (error as Error).message);
return 1;
}
}
// Run if called directly
const exitCode = main();
process.exit(exitCode);
export { generateSitemap, validateSitemap };
|