Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/lukebennett88/luke-ui. ui.lukebennett.dev
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344const XML_ESCAPE_PATTERN = /[&<>"']/g;const XML_ESCAPES: Record<string, string> = { '"': '"', '&': '&', "'": ''', '<': '<', '>': '>',};
/** A page to list in the sitemap. */export interface SitemapPage { url: string;}
/** * Builds a sitemaps.org 0.9 protocol document. Every URL is absolute and * trailing-slashed, except the homepage, matching the URL the host serves * with a 200 instead of a redirect. */export function buildSitemap(origin: string, pages: Array<SitemapPage>): string { const entries = pages.map((page) => buildUrlEntry(origin, page)).join('');
return ( '<?xml version="1.0" encoding="UTF-8"?>\n' + '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' + entries + '</urlset>\n' );}
function buildUrlEntry(origin: string, page: SitemapPage): string { const loc = escapeXml(`${origin}${sitemapPath(page.url)}`); return ` <url>\n <loc>${loc}</loc>\n </url>\n`;}
function sitemapPath(url: string): string { if (url === '/') return '/'; return url.endsWith('/') ? url : `${url}/`;}
function escapeXml(value: string): string { return value.replace(XML_ESCAPE_PATTERN, (char) => XML_ESCAPES[char] ?? char);}