All files / scripts/sitemap-xml/render url-entry.ts

100% Statements 6/6
100% Branches 1/1
100% Functions 2/2
100% Lines 6/6

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                                      3x                                   87643x             87643x 530940x       87643x     87643x    
/**
 * @module Infrastructure/SitemapXml/Render/UrlEntry
 * @category Intelligence Operations / Supporting Infrastructure
 * @name Single `<url>` entry XML builder
 *
 * @description
 * Pure string builder for a single `<url>` block including its optional
 * `<xhtml:link rel="alternate">` hreflang siblings. Hreflang codes are
 * normalised through `hreflangCode` so the file-suffix `no` becomes
 * `nb` (Norwegian Bokmål) on the wire.
 *
 * Round-6 split: extracted from `scripts/generate-sitemap.ts`.
 *
 * @author Hack23 AB (Infrastructure Team)
 * @license Apache-2.0
 */
 
import { hreflangCode } from '../hreflang.js';
 
const BASE_URL = 'https://riksdagsmonitor.com';
 
/** One `rel="alternate"` link target keyed by hreflang code. */
export interface HreflangAlternate {
  lang: string;
  href: string;
}
 
/**
 * Generate XML for a URL entry.
 */
export 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="${hreflangCode(alt.lang)}" href="${BASE_URL}/${alt.href}"/>`;
  });
 
  xml += `
</url>`;
 
  return xml;
}