import type { APIRoute } from 'astro';
import { site } from '@shared/lib/site';
export const prerender = true;
// Both page conventions are supported: `foo/index.mdx` and flat `foo.mdx`
// produce the same route (/foo/) with the default 'directory' build format.
const pages = import.meta.glob('./**/*.{astro,mdx}');
const urls = [
...new Set(
Object.keys(pages).map((file) => {
const path = file
.replace(/^\.\//, '')
.replace(/\/?index\.(?:astro|mdx)$/, '')
.replace(/\.(?:astro|mdx)$/, '');
return path ? `/${path}/` : '/';
}),
),
]
.sort((a, b) => {
if (a === '/') return -1;
if (b === '/') return 1;
return a.localeCompare(b);
});
const escapeXml = (value: string) =>
value
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
export const GET: APIRoute = () => {
const isDevelopment = process.env.NODE_ENV === 'development';
const baseUrl = isDevelopment ? '' : site.docsUrl;
const lastModified = new Date().toISOString();
const entries = urls
.map(
(url) => `
${escapeXml(`${baseUrl}${url}`)}
${lastModified}
`,
)
.join('\n');
const body = `
${entries}
`;
return new Response(body, {
headers: {
'Content-Type': 'application/xml; charset=utf-8',
},
});
};