{"version":3,"file":"hreflang-I8HfDZBL.mjs","names":[],"sources":["../src/seo/hreflang.ts"],"sourcesContent":["/**\n * hreflang alternate resolution for translated content (#1690).\n *\n * Mirrors the sitemap route's semantics so the render path and the\n * sitemap always agree:\n *\n * - Alternates are emitted per published locale variant of a\n *   translation group, including a self-referencing entry (Google\n *   recommends the page annotate itself).\n * - `x-default` points at the default-locale variant, falling back to\n *   the first routable variant when the default locale has no\n *   translation.\n * - Variants whose locale isn't in the configured `i18n.locales` list\n *   are dropped: linking to a route the site can't serve is worse than\n *   no link at all.\n * - Variants flagged `noindex` in the SEO panel are excluded — the\n *   sitemap doesn't list them, and pointing search engines at a page\n *   that asks not to be indexed is a contradictory signal. When the\n *   entry itself is noindex the result is empty: a page opting out of\n *   indexing shouldn't announce an hreflang set at all.\n * - When i18n is disabled (or no absolute site URL can be resolved,\n *   which hreflang requires) the result is empty and no queries run.\n */\n\nimport type { Kysely } from \"kysely\";\n\nimport { ContentRepository } from \"../database/repositories/content.js\";\nimport type { Database } from \"../database/types.js\";\nimport { getI18nConfig, isI18nEnabled } from \"../i18n/config.js\";\nimport { interpolateUrlPattern, localizePath } from \"../i18n/resolve.js\";\nimport { requestCached } from \"../request-cache.js\";\nimport { getCollectionInfoWithDb } from \"../schema/query.js\";\nimport { getSiteSettingsWithDb } from \"../settings/index.js\";\n\nconst TRAILING_SLASH_RE = /\\/$/;\nconst ABSOLUTE_URL_RE = /^https?:\\/\\//i;\n\n/**\n * IDs of variants flagged `noindex` in the SEO panel. Entries without\n * an `_emdash_seo` row are indexable by default (same as the sitemap).\n * The id list is bounded by the number of configured locales, so no\n * chunking is needed.\n */\nasync function findNoindexIds(\n\tdb: Kysely<Database>,\n\tcollection: string,\n\tids: string[],\n): Promise<Set<string>> {\n\tif (ids.length === 0) return new Set();\n\tconst rows = await db\n\t\t.selectFrom(\"_emdash_seo\")\n\t\t.select(\"content_id\")\n\t\t.where(\"collection\", \"=\", collection)\n\t\t.where(\"content_id\", \"in\", ids)\n\t\t.where(\"seo_no_index\", \"=\", 1)\n\t\t.execute();\n\treturn new Set(rows.map((r) => r.content_id));\n}\n\n/** A single alternate link, ready to render as `<link rel=\"alternate\">`. */\nexport interface HreflangAlternate {\n\t/** Locale code, or `\"x-default\"` for the default variant */\n\threflang: string;\n\t/** Absolute URL of the variant */\n\thref: string;\n}\n\nexport interface HreflangOptions {\n\t/**\n\t * Absolute site origin (e.g. `https://example.com`) used to build\n\t * the alternate URLs. Falls back to the site settings URL; when\n\t * neither is available the result is empty, since hreflang\n\t * requires fully-qualified URLs.\n\t */\n\tsiteUrl?: string;\n}\n\n/**\n * Resolve hreflang alternates for a content entry.\n *\n * @example\n * ```astro\n * ---\n * import { getHreflangAlternates } from \"@premium-cms/emdash\";\n *\n * const alternates = await getHreflangAlternates(\"posts\", entry.data.id, {\n *   siteUrl: Astro.url.origin,\n * });\n * ---\n * <head>\n *   {alternates.map((a) => <link rel=\"alternate\" hreflang={a.hreflang} href={a.href} />)}\n * </head>\n * ```\n */\nexport async function getHreflangAlternates(\n\tcollection: string,\n\tentryId: string,\n\toptions: HreflangOptions = {},\n): Promise<HreflangAlternate[]> {\n\tif (!isI18nEnabled()) return [];\n\tconst key = `hreflang:${collection}:${entryId}:${options.siteUrl ?? \"\"}`;\n\treturn requestCached(key, async () => {\n\t\tconst { getDb } = await import(\"../loader.js\");\n\t\tconst db = await getDb();\n\t\treturn getHreflangAlternatesWithDb(db, collection, entryId, options);\n\t});\n}\n\n/**\n * Resolve hreflang alternates with an explicit db handle.\n *\n * @internal Use `getHreflangAlternates()` in templates. This variant is\n * for routes/components that already have a database handle.\n */\nexport async function getHreflangAlternatesWithDb(\n\tdb: Kysely<Database>,\n\tcollection: string,\n\tentryId: string,\n\toptions: HreflangOptions = {},\n): Promise<HreflangAlternate[]> {\n\tif (!isI18nEnabled()) return [];\n\n\tlet siteUrl = options.siteUrl;\n\tif (!siteUrl) {\n\t\tconst settings = await getSiteSettingsWithDb(db);\n\t\tsiteUrl = settings.url;\n\t}\n\t// hreflang requires fully-qualified URLs; a relative site URL would\n\t// produce invalid output (and EmDashHead's isSafeHref would drop it).\n\tif (!siteUrl || !ABSOLUTE_URL_RE.test(siteUrl)) return [];\n\tsiteUrl = siteUrl.replace(TRAILING_SLASH_RE, \"\");\n\n\tconst repo = new ContentRepository(db);\n\tconst item = await repo.findByIdOrSlug(collection, entryId);\n\tif (!item) return [];\n\n\t// Legacy rows imported before i18n may have no translation_group;\n\t// treat them as a single-variant group.\n\tconst group = item.translationGroup || item.id;\n\tlet variants = await repo.findTranslations(collection, group);\n\tif (variants.length === 0) variants = [item];\n\n\tlet published = variants.filter((v) => v.status === \"published\");\n\tif (published.length === 0) return [];\n\n\t// Exclude noindex variants, matching the sitemap's `_emdash_seo`\n\t// filter so head and sitemap agree on which variants are\n\t// discoverable. When the requested entry itself is noindex, emit\n\t// nothing: a page opting out of indexing shouldn't announce an\n\t// hreflang set.\n\tconst noindexIds = await findNoindexIds(\n\t\tdb,\n\t\tcollection,\n\t\tpublished.map((v) => v.id),\n\t);\n\tif (noindexIds.has(item.id)) return [];\n\tpublished = published.filter((v) => !noindexIds.has(v.id));\n\n\tconst info = await getCollectionInfoWithDb(db, collection);\n\tconst urlPattern = info?.urlPattern ?? null;\n\n\tconst resolved: Array<{ locale: string; href: string }> = [];\n\tfor (const variant of published) {\n\t\tconst locale = variant.locale || \"en\";\n\t\tconst path = interpolateUrlPattern({\n\t\t\tpattern: urlPattern,\n\t\t\tcollection,\n\t\t\tslug: variant.slug || variant.id,\n\t\t\tid: variant.id,\n\t\t});\n\t\tconst localized = await localizePath(path, locale);\n\t\tif (localized === null) continue;\n\t\tresolved.push({ locale, href: `${siteUrl}${localized}` });\n\t}\n\tif (resolved.length === 0) return [];\n\n\tresolved.sort((a, b) => a.locale.localeCompare(b.locale));\n\tconst alternates: HreflangAlternate[] = resolved.map((r) => ({\n\t\threflang: r.locale,\n\t\thref: r.href,\n\t}));\n\n\t// x-default: default-locale variant, else the first routable one.\n\t// Same fallback the sitemap route uses.\n\tconst defaultLocale = getI18nConfig()?.defaultLocale;\n\tconst xDefault = resolved.find((r) => r.locale === defaultLocale) ?? resolved[0];\n\tif (xDefault) {\n\t\talternates.push({ hreflang: \"x-default\", href: xDefault.href });\n\t}\n\n\treturn alternates;\n}\n"],"mappings":";;;;;;;;AAkCA,MAAM,oBAAoB;AAC1B,MAAM,kBAAkB;;;;;;;AAQxB,eAAe,eACd,IACA,YACA,KACuB;AACvB,KAAI,IAAI,WAAW,EAAG,wBAAO,IAAI,KAAK;CACtC,MAAM,OAAO,MAAM,GACjB,WAAW,cAAc,CACzB,OAAO,aAAa,CACpB,MAAM,cAAc,KAAK,WAAW,CACpC,MAAM,cAAc,MAAM,IAAI,CAC9B,MAAM,gBAAgB,KAAK,EAAE,CAC7B,SAAS;AACX,QAAO,IAAI,IAAI,KAAK,KAAK,MAAM,EAAE,WAAW,CAAC;;;;;;;;;;;;;;;;;;;AAsC9C,eAAsB,sBACrB,YACA,SACA,UAA2B,EAAE,EACE;AAC/B,KAAI,CAAC,eAAe,CAAE,QAAO,EAAE;AAE/B,QAAO,cADK,YAAY,WAAW,GAAG,QAAQ,GAAG,QAAQ,WAAW,MAC1C,YAAY;EACrC,MAAM,EAAE,UAAU,MAAM,OAAO;AAE/B,SAAO,4BADI,MAAM,OAAO,EACe,YAAY,SAAS,QAAQ;GACnE;;;;;;;;AASH,eAAsB,4BACrB,IACA,YACA,SACA,UAA2B,EAAE,EACE;AAC/B,KAAI,CAAC,eAAe,CAAE,QAAO,EAAE;CAE/B,IAAI,UAAU,QAAQ;AACtB,KAAI,CAAC,QAEJ,YADiB,MAAM,sBAAsB,GAAG,EAC7B;AAIpB,KAAI,CAAC,WAAW,CAAC,gBAAgB,KAAK,QAAQ,CAAE,QAAO,EAAE;AACzD,WAAU,QAAQ,QAAQ,mBAAmB,GAAG;CAEhD,MAAM,OAAO,IAAI,kBAAkB,GAAG;CACtC,MAAM,OAAO,MAAM,KAAK,eAAe,YA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