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

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.
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.5",
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
package/src/lib/schema.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Graph, Thing, WithContext } from "schema-dts"
1
+ import type { Graph, Thing } from "schema-dts"
2
2
 
3
3
  import { getConfig, localize } from "./config.ts"
4
4
  import type { Localized } from "./config.ts"
@@ -8,8 +8,11 @@ import { url } from "./url.ts"
8
8
  * A structured data item of your own, e.g. a `Product` or an `Event`. The schema.org types
9
9
  * give the editor completion, and other objects are also accepted. `@context` is not
10
10
  * necessary: the items go into one graph with the context.
11
+ *
12
+ * `@context` is an optional property, not `WithContext<Thing>`: that type makes the union of
13
+ * schema-dts too large for TypeScript when a layout spreads its props into `<Metadata>`.
11
14
  */
12
- export type SchemaData = Thing | WithContext<Thing> | Record<string, unknown>
15
+ export type SchemaData = (Thing & { "@context"?: string }) | Record<string, unknown>
13
16
 
14
17
  /**
15
18
  * The organization or the person behind the site. It is the publisher of the site and of