{"version":3,"file":"sitemap-_collection_.xml.mjs","names":[],"sources":["../../../src/astro/routes/sitemap-[collection].xml.ts"],"sourcesContent":["/**\n * Per-collection sitemap endpoint\n *\n * GET /sitemap-{collection}.xml - Sitemap for a single content collection.\n *\n * Uses the collection's url_pattern to build URLs. Falls back to\n * /{collection}/{slug} when no pattern is configured.\n *\n * i18n behaviour: when Astro i18n is enabled, the locale prefix is\n * applied via Astro's own `getRelativeLocaleUrl` (which honours\n * `prefixDefaultLocale`, custom `path` mappings, and other `routing`\n * config). Each translation row is emitted as its own `<url>` with\n * `<xhtml:link rel=\"alternate\" hreflang=\"...\">` entries pointing to\n * its siblings (grouped by `translation_group`). The default-locale\n * variant is also linked as `hreflang=\"x-default\"`.\n */\n\nimport type { APIRoute } from \"astro\";\n\nimport { handleSitemapData } from \"#api/handlers/seo.js\";\nimport { getPublicOrigin } from \"#api/public-url.js\";\nimport { getSiteSettingsWithDb } from \"#settings/index.js\";\n\nimport { getI18nConfig, isI18nEnabled } from \"../../i18n/config.js\";\nimport { interpolateUrlPattern, localizePath } from \"../../i18n/resolve.js\";\nimport { buildSeoImageUrl } from \"../../seo/media-url.js\";\n\nexport const prerender = false;\n\nconst TRAILING_SLASH_RE = /\\/$/;\nconst AMP_RE = /&/g;\nconst LT_RE = /</g;\nconst GT_RE = />/g;\nconst QUOT_RE = /\"/g;\nconst APOS_RE = /'/g;\n\nexport const GET: APIRoute = async ({ params, locals, url }) => {\n\tconst { emdash } = locals;\n\tconst collectionSlug = params.collection;\n\n\tif (!emdash?.db || !collectionSlug) {\n\t\treturn new Response(\"<!-- EmDash not configured -->\", {\n\t\t\tstatus: 500,\n\t\t\theaders: { \"Content-Type\": \"application/xml\" },\n\t\t});\n\t}\n\n\ttry {\n\t\tconst settings = await getSiteSettingsWithDb(emdash.db);\n\t\tconst siteUrl = (settings.url || getPublicOrigin(url, emdash?.config)).replace(\n\t\t\tTRAILING_SLASH_RE,\n\t\t\t\"\",\n\t\t);\n\n\t\tconst result = await handleSitemapData(emdash.db, collectionSlug);\n\n\t\tif (!result.success || !result.data) {\n\t\t\treturn new Response(\"<!-- Failed to generate sitemap -->\", {\n\t\t\t\tstatus: 500,\n\t\t\t\theaders: { \"Content-Type\": \"application/xml\" },\n\t\t\t});\n\t\t}\n\n\t\tconst col = result.data.collections[0];\n\t\tif (!col) {\n\t\t\treturn new Response(\"<!-- Collection not found or empty -->\", {\n\t\t\t\tstatus: 404,\n\t\t\t\theaders: { \"Content-Type\": \"application/xml\" },\n\t\t\t});\n\t\t}\n\n\t\tconst i18nEnabled = isI18nEnabled();\n\t\tconst i18nConfig = getI18nConfig();\n\n\t\t// Group entries by `translation_group` so each <url> can advertise\n\t\t// its sibling translations via xhtml:link. Rows without a group\n\t\t// (legacy/single-locale data) are emitted individually.\n\t\ttype Entry = (typeof col.entries)[number];\n\t\tconst groups = new Map<string, Entry[]>();\n\t\tconst ungrouped: Entry[] = [];\n\t\tfor (const entry of col.entries) {\n\t\t\tif (i18nEnabled && entry.translationGroup) {\n\t\t\t\tconst list = groups.get(entry.translationGroup);\n\t\t\t\tif (list) list.push(entry);\n\t\t\t\telse groups.set(entry.translationGroup, [entry]);\n\t\t\t} else {\n\t\t\t\tungrouped.push(entry);\n\t\t\t}\n\t\t}\n\n\t\t// Resolve every URL up-front so we can reference sibling URLs\n\t\t// while emitting hreflang alternates without re-resolving.\n\t\t// `localizePath` returns `null` when the row's locale isn't in\n\t\t// the configured `i18n.locales` list -- the site can't serve a\n\t\t// route for it, so the entry is dropped from the sitemap and\n\t\t// omitted from sibling alternates.\n\t\tconst urlByEntry = new Map<string, string | null>();\n\t\tconst resolveEntryUrl = async (entry: Entry): Promise<string | null> => {\n\t\t\tif (urlByEntry.has(entry.id)) return urlByEntry.get(entry.id) ?? null;\n\t\t\tconst path = interpolateUrlPattern({\n\t\t\t\tpattern: col.urlPattern,\n\t\t\t\tcollection: col.collection,\n\t\t\t\tslug: entry.slug || entry.id,\n\t\t\t\tid: entry.id,\n\t\t\t});\n\t\t\tconst localized = await localizePath(path, entry.locale);\n\t\t\tconst absolute = localized === null ? null : `${siteUrl}${localized}`;\n\t\t\turlByEntry.set(entry.id, absolute);\n\t\t\treturn absolute;\n\t\t};\n\n\t\tconst useXhtml = i18nEnabled;\n\t\tconst lines: string[] = ['<?xml version=\"1.0\" encoding=\"UTF-8\"?>'];\n\t\tlines.push(\n\t\t\tuseXhtml\n\t\t\t\t? '<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xhtml=\"http://www.w3.org/1999/xhtml\" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\">'\n\t\t\t\t: '<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:image=\"http://www.google.com/schemas/sitemap-image/1.1\">',\n\t\t);\n\n\t\tconst writeUrl = async (entry: Entry, siblings: Entry[] | null) => {\n\t\t\tconst loc = await resolveEntryUrl(entry);\n\t\t\t// Skip rows whose locale isn't in the configured `i18n.locales`\n\t\t\t// list. Linking to a route the site can't serve is worse than\n\t\t\t// no link at all (search engines hit a 404 and downrank).\n\t\t\tif (loc === null) return;\n\n\t\t\tlines.push(\"  <url>\");\n\t\t\tlines.push(`    <loc>${escapeXml(loc)}</loc>`);\n\t\t\tlines.push(`    <lastmod>${escapeXml(entry.updatedAt)}</lastmod>`);\n\n\t\t\t// Google image sitemap extension: advertise the entry's SEO\n\t\t\t// image (the same \"preferred image\" used for og:image) so it\n\t\t\t// can be discovered and indexed for Google Images.\n\t\t\tif (entry.image) {\n\t\t\t\tconst imageLoc = buildSeoImageUrl(entry.image, siteUrl);\n\t\t\t\tlines.push(\"    <image:image>\");\n\t\t\t\tlines.push(`      <image:loc>${escapeXml(imageLoc)}</image:loc>`);\n\t\t\t\tlines.push(\"    </image:image>\");\n\t\t\t}\n\n\t\t\tconst alternateEntries = siblings ?? (useXhtml ? [entry] : null);\n\t\t\tif (useXhtml && alternateEntries) {\n\t\t\t\t// Emit one xhtml:link per sibling (including self -- Google\n\t\t\t\t// recommends including the page's own hreflang annotation).\n\t\t\t\t// Siblings with unroutable locales are skipped here too.\n\t\t\t\tfor (const sib of alternateEntries) {\n\t\t\t\t\tconst sibLoc = await resolveEntryUrl(sib);\n\t\t\t\t\tif (sibLoc === null) continue;\n\t\t\t\t\tlines.push(\n\t\t\t\t\t\t`    <xhtml:link rel=\"alternate\" hreflang=\"${escapeXml(sib.locale)}\" href=\"${escapeXml(sibLoc)}\" />`,\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\t// x-default: prefer the default-locale sibling, otherwise\n\t\t\t\t// the first sibling with a routable URL. Stable order:\n\t\t\t\t// rows arrive sorted by updated_at DESC from the handler.\n\t\t\t\tconst defaultSibling =\n\t\t\t\t\ti18nConfig && alternateEntries.find((s) => s.locale === i18nConfig.defaultLocale);\n\t\t\t\tlet xDefaultLoc: string | null = null;\n\t\t\t\tif (defaultSibling) {\n\t\t\t\t\txDefaultLoc = await resolveEntryUrl(defaultSibling);\n\t\t\t\t}\n\t\t\t\tif (xDefaultLoc === null) {\n\t\t\t\t\tfor (const sib of alternateEntries) {\n\t\t\t\t\t\tconst sibLoc = await resolveEntryUrl(sib);\n\t\t\t\t\t\tif (sibLoc !== null) {\n\t\t\t\t\t\t\txDefaultLoc = sibLoc;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (xDefaultLoc !== null) {\n\t\t\t\t\tlines.push(\n\t\t\t\t\t\t`    <xhtml:link rel=\"alternate\" hreflang=\"x-default\" href=\"${escapeXml(xDefaultLoc)}\" />`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tlines.push(\"  </url>\");\n\t\t};\n\n\t\tfor (const siblings of groups.values()) {\n\t\t\tfor (const entry of siblings) {\n\t\t\t\tawait writeUrl(entry, siblings);\n\t\t\t}\n\t\t}\n\t\tfor (const entry of ungrouped) {\n\t\t\tawait writeUrl(entry, null);\n\t\t}\n\n\t\tlines.push(\"</urlset>\");\n\n\t\treturn new Response(lines.join(\"\\n\"), {\n\t\t\tstatus: 200,\n\t\t\theaders: {\n\t\t\t\t\"Content-Type\": \"application/xml; charset=utf-8\",\n\t\t\t\t\"Cache-Control\": \"public, max-age=3600\",\n\t\t\t},\n\t\t});\n\t} catch {\n\t\treturn new Response(\"<!-- Internal error generating sitemap -->\", {\n\t\t\tstatus: 500,\n\t\t\theaders: { \"Content-Type\": \"application/xml\" },\n\t\t});\n\t}\n};\n\n/** Escape special XML characters in a string */\nfunction escapeXml(str: string): string {\n\treturn str\n\t\t.replace(AMP_RE, \"&amp;\")\n\t\t.replace(LT_RE, \"&lt;\")\n\t\t.replace(GT_RE, \"&gt;\")\n\t\t.replace(QUOT_RE, \"&quot;\")\n\t\t.replace(APOS_RE, \"&apos;\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA2BA,MAAa,YAAY;AAEzB,MAAM,oBAAoB;AAC1B,MAAM,SAAS;AACf,MAAM,QAAQ;AACd,MAAM,QAAQ;AACd,MAAM,UAAU;AAChB,MAAM,UAAU;AAEhB,MAAa,MAAgB,OAAO,EAAE,QAAQ,QAAQ,UAAU;CAC/D,MAAM,EAAE,WAAW;CACnB,MAAM,iBAAiB,OAAO;AAE9B,KAAI,CAAC,QAAQ,MAAM,CAAC,eACnB,QAAO,IAAI,SAAS,kCAAkC;EACrD,QAAQ;EACR,SAAS,EAAE,gBAAgB,mBAAmB;EAC9C,CAAC;AAGH,KAAI;EAEH,MAAM,YADW,MAAM,sBAAsB,OAAO,GAAG,EAC7B,OAAO,gBAAgB,KAAK,QAAQ,OAAO,EAAE,QACtE,mBACA,GACA;EAED,MAAM,SAAS,MAAM,kBAAkB,OAAO,IAAI,eAAe;AAEjE,MAAI,CAAC,OAAO,WAAW,CAAC,OAAO,KAC9B,QAAO,IAAI,SAAS,uCAAuC;GAC1D,QAAQ;GACR,SAAS,EAAE,gBAAgB,mBAAmB;GAC9C,CAAC;EAG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