import type { APIRoute } from "astro"; import { skills, type Skill } from "../data/skills"; import { agents } from "../data/agents"; const SITE_URL = "https://www.ui-skills.com"; const escapeXml = (value: string) => value .replace(/&/g, "&") .replace(//g, ">") .replace(/\"/g, """) .replace(/'/g, "'"); const buildTopicRoutes = (allSkills: Skill[]) => Array.from(new Set(allSkills.flatMap((skill) => skill.topics ?? []))).map( (topic) => `/skills/${topic}`, ); const buildGroupPaths = (allSkills: Skill[]) => Array.from( new Set( allSkills.flatMap((skill) => { const segments = skill.pathSlug.split("/"); if (segments.length <= 1) { return []; } return segments .slice(0, -1) .map((_, index) => segments.slice(0, index + 1).join("/")); }), ), ); export const GET: APIRoute = ({ site }) => { const origin = site?.origin ?? SITE_URL; const staticRoutes = ["/", "/skills", "/skills/topics", "/agents"]; const topicRoutes = buildTopicRoutes(skills); const groupRoutes = buildGroupPaths(skills).map((path) => `/skills/${path}`); const skillRoutes = skills.map((skill) => `/skills/${skill.pathSlug}`); const agentRoutes = agents.map((agent) => `/agents/${agent.id}`); const allRoutes = Array.from( new Set([ ...staticRoutes, ...topicRoutes, ...groupRoutes, ...skillRoutes, ...agentRoutes, ]), ); const urlset = allRoutes .map((route) => { const normalizedRoute = route === "/" ? "/" : route.replace(/\/+$/, ""); const loc = new URL(normalizedRoute, origin).toString(); return `${escapeXml(loc)}`; }) .join(""); const body = `${urlset}`; return new Response(body, { headers: { "Content-Type": "application/xml; charset=utf-8", }, }); };