import { SitemapUrlRecord } from '../types.js'; import { escapeXml } from './escapeXml.js'; const SITEMAP_NS = 'http://www.sitemaps.org/schemas/sitemap/0.9'; const XHTML_NS = 'http://www.w3.org/1999/xhtml'; /** * Serialize already-absolute `SitemapUrlRecord`s into a `` document (sitemaps.org * 0.9). The `xmlns:xhtml` namespace is declared ONLY when at least one record carries * hreflang alternates, so single-language sitemaps stay namespace-free. Absolutization and * locale prefixing happened upstream in `expandLocales` — this function is a pure serializer. */ export function renderUrlset(records: SitemapUrlRecord[]): string { const hasAlternates = records.some( (record) => record.alternates && record.alternates.length > 0 ); const open = hasAlternates ? `` : ``; const body = records.map(renderUrl).join('\n'); return `\n${open}\n${body}${ body ? '\n' : '' }\n`; } function renderUrl(record: SitemapUrlRecord): string { const lines = [` ${escapeXml(record.loc)}`]; if (record.lastmod) { lines.push(` ${escapeXml(record.lastmod)}`); } if (record.changefreq) { lines.push(` ${escapeXml(record.changefreq)}`); } if (record.priority !== undefined) { lines.push(` ${formatPriority(record.priority)}`); } if (record.alternates) { for (const alt of record.alternates) { lines.push( ` ` ); } } return ` \n${lines.join('\n')}\n `; } /** Clamp to [0,1] and render with one decimal (sitemap priority precision). */ function formatPriority(priority: number): string { const clamped = Math.max(0, Math.min(1, priority)); return clamped.toFixed(1); }