@mannisto/astro-metadata 2.0.0-alpha.3 → 2.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/integration.js +20 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mannisto/astro-metadata",
3
- "version": "2.0.0-alpha.3",
3
+ "version": "2.0.0-alpha.4",
4
4
  "description": "Metadata for Astro: page tags, social cards, favicons, the web app manifest and structured data.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -64,19 +64,35 @@ const I18N_FILE = fileURLToPath(new URL("./lib/i18n.ts", import.meta.url))
64
64
  * the project by its path, because with pnpm this package can not see it by its name.
65
65
  *
66
66
  * @param {string} runtime - The absolute path of `@mannisto/astro-i18n/runtime`.
67
+ * @param {string} base - The `base` of the Astro config.
67
68
  * @returns {string}
68
69
  */
69
- function astroI18nAdapter(runtime) {
70
+ function astroI18nAdapter(runtime, base) {
70
71
  return `import { Locale } from ${JSON.stringify(runtime)}
71
72
 
73
+ const BASE = ${JSON.stringify(base.replace(/\/$/, ""))}
74
+
75
+ // Returns the locale prefix of the URL, e.g. "fi" for "/fi/about". A page in the [locale]
76
+ // folder always has the prefix in Astro.url, also after the rewrite of the default locale.
77
+ // A page outside the folder, e.g. the 404 page, has none.
78
+ function prefix(page) {
79
+ let path = page.url.pathname
80
+ if (BASE && path.startsWith(BASE)) path = path.slice(BASE.length)
81
+ const first = path.split("/")[1]
82
+ return Locale.supported.includes(first) ? first : undefined
83
+ }
84
+
72
85
  export const i18n = {
73
86
  locale(page) {
74
- return Locale.fromURL(page.url)
87
+ return prefix(page) ?? Locale.defaultLocale
75
88
  },
76
89
  path(page) {
77
- return Locale.url(Locale.fromURL(page.url), page.url.pathname)
90
+ const code = prefix(page)
91
+ return code ? Locale.url(code, page.url.pathname) : page.url.pathname
78
92
  },
79
93
  alternates(page) {
94
+ if (!prefix(page)) return []
95
+
80
96
  const path = page.url.pathname
81
97
  const links = Locale.supported.map((code) => {
82
98
  return { hreflang: code, href: Locale.url(code, path) }
@@ -151,7 +167,7 @@ function i18nAdapter(config, logger) {
151
167
  if (astroI18n) {
152
168
  try {
153
169
  const fromProject = createRequire(fileURLToPath(new URL("./package.json", config.root)))
154
- return astroI18nAdapter(fromProject.resolve("@mannisto/astro-i18n/runtime"))
170
+ return astroI18nAdapter(fromProject.resolve("@mannisto/astro-i18n/runtime"), config.base)
155
171
  } catch {
156
172
  logger.warn("@mannisto/astro-i18n is in the config, but its runtime was not found.")
157
173
  return undefined