@escape-game-over/atlas 0.1.90 → 0.1.91

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": "@escape-game-over/atlas",
3
- "version": "0.1.90",
3
+ "version": "0.1.91",
4
4
  "type": "module",
5
5
  "description": "Typed, data-driven machinery for static multi-locale, multi-deployment Astro sites.",
6
6
  "private": false,
@@ -1,6 +1,7 @@
1
1
  import type { LocalesOf, SiteConfigShape } from "../config.ts";
2
2
  import type { IsNever, NoExcessKeys, StringKeys } from "../types.ts";
3
3
  import type {
4
+ AddedMessageExists,
4
5
  EntryTokens,
5
6
  MismatchedLocales,
6
7
  OverridePlaceholderMismatch,
@@ -137,10 +138,32 @@ export type MessageOverrides<Catalog, L extends string> = {
137
138
  readonly [K in StringKeys<Catalog>]?: Readonly<Partial<Record<L, string>>>;
138
139
  };
139
140
 
140
- /** Merges a project's override catalog over the base catalog, locale by locale. */
141
+ /**
142
+ * What `defineProject` checks an `addMessages` against: each key new to the
143
+ * base catalog, written for exactly the locales the project publishes, with
144
+ * the same `{placeholders}` and `[marks]` in each.
145
+ *
146
+ * New only, because a key the base already has is copy every project shares,
147
+ * and rewording it is what `overrideMessages` is for — with its own check that
148
+ * the placeholders survive. An addition shadowing a base key would get round
149
+ * that check, and two fields could then say different things about one key.
150
+ */
151
+ export type ValidateAdditions<T, Base, Published extends string> = {
152
+ [K in keyof T]: K extends keyof Base
153
+ ? AddedMessageExists<K & string>
154
+ : NoExcessKeys<T[K], Published> &
155
+ Readonly<Record<Published, string>> &
156
+ SelfConsistent<T[K], K>;
157
+ };
158
+
159
+ /**
160
+ * Merges a project's override catalog over the base catalog, locale by locale,
161
+ * then adds the project's own messages.
162
+ */
141
163
  export function mergeCatalog<L extends string>(
142
164
  base: BaseCatalog<L>,
143
- overrides: OverrideCatalog<L>
165
+ overrides: OverrideCatalog<L>,
166
+ additions: OverrideCatalog<L> = {}
144
167
  ): Readonly<Record<string, Readonly<Record<L, string>>>> {
145
168
  const merged: Record<string, Record<string, string>> = {};
146
169
  for (const [key, entry] of Object.entries(base)) {
@@ -154,5 +177,15 @@ export function mergeCatalog<L extends string>(
154
177
  if (typeof text === "string") target[locale] = text;
155
178
  }
156
179
  }
180
+ for (const [key, entry] of Object.entries(additions)) {
181
+ // Unreachable via the typed API, which refuses a base key here; an
182
+ // Astro config is evaluated before the type check, so say it plainly.
183
+ if (key in merged) {
184
+ throw new Error(
185
+ `addMessages: "${key}" is already in the default catalog. Reword it in overrideMessages instead.`
186
+ );
187
+ }
188
+ merged[key] = { ...entry } as Record<string, string>;
189
+ }
157
190
  return merged as Readonly<Record<string, Readonly<Record<L, string>>>>;
158
191
  }
@@ -112,6 +112,11 @@ export interface PlaceholderMismatch<Key extends string, L extends string> {
112
112
  readonly __PLACEHOLDER_MISMATCH__: `Message "${Key}" uses different {placeholders} or [marks] in locale "${L}" than in its other locales`;
113
113
  }
114
114
 
115
+ /** Editor-facing error when a project adds a message the base already has. */
116
+ export interface AddedMessageExists<Key extends string> {
117
+ readonly __ADDED_MESSAGE_EXISTS__: `Message "${Key}" is already in the default catalog: reword it in overrideMessages instead of adding it`;
118
+ }
119
+
115
120
  /** Editor-facing error when an override drops or invents one. */
116
121
  export interface OverridePlaceholderMismatch<
117
122
  Key extends string,
package/src/index.ts CHANGED
@@ -230,6 +230,7 @@ export {
230
230
  tierRange,
231
231
  } from "./money.ts";
232
232
  export type {
233
+ AddedCatalog,
233
234
  DefinedProject,
234
235
  ProjectInput,
235
236
  ProjectRouteId,
package/src/project.ts CHANGED
@@ -10,6 +10,7 @@ import type { Fonts } from "./fonts.ts";
10
10
  import type {
11
11
  BaseCatalog,
12
12
  OverrideCatalog,
13
+ ValidateAdditions,
13
14
  ValidateOverrideCatalog,
14
15
  } from "./i18n/define.ts";
15
16
  import type { ImageAsset } from "./image.ts";
@@ -139,6 +140,20 @@ export interface ProjectInput<L extends string, Routes> {
139
140
  * than the argument.
140
141
  */
141
142
  readonly overrideMessages: OverrideCatalog<L>;
143
+ /**
144
+ * Copy only this project has: keys the default catalog does not declare,
145
+ * each written for every locale the project publishes and no others.
146
+ *
147
+ * For what one deployment says and no other does — a venue's own FAQ, a
148
+ * house rule — so it goes through `translate` and `rich` like any message,
149
+ * marks and placeholders included, without the shared catalog carrying it
150
+ * in languages the venue never publishes. A key the default catalog
151
+ * already has is refused: reword it in `overrideMessages`.
152
+ *
153
+ * Optional, unlike `overrideMessages`: nothing downstream is computed from
154
+ * it except the catalog's keys, which an absent field leaves as they are.
155
+ */
156
+ readonly addMessages?: OverrideCatalog<L>;
142
157
  /** Slug retranslations and page on/off switches. See `overrideMessages`. */
143
158
  readonly overrideRoutes: RouteOverrides<Routes, L>;
144
159
  }
@@ -289,7 +304,20 @@ export type ProjectOverrideChecks<
289
304
  Routes,
290
305
  T["overrideRoutes"]
291
306
  >;
292
- };
307
+ } & (T extends { readonly addMessages: infer A }
308
+ ? {
309
+ readonly addMessages: ValidateAdditions<
310
+ A,
311
+ Catalog,
312
+ T["enabledLocales"][number]
313
+ >;
314
+ }
315
+ : unknown);
316
+
317
+ /** A project's own messages, as a catalog: none when it adds none. */
318
+ export type AddedCatalog<P> = P extends { readonly addMessages: infer A }
319
+ ? A
320
+ : Record<never, never>;
293
321
 
294
322
  export function defineProject<
295
323
  const C extends SiteConfigShape,
@@ -324,5 +352,22 @@ export function defineProject<
324
352
  );
325
353
  }
326
354
  }
355
+ // The same two rules the type states, for the same reason: the config is
356
+ // evaluated before the type check runs.
357
+ for (const [key, entry] of Object.entries(project.addMessages ?? {})) {
358
+ if (key in _messages) {
359
+ throw new Error(
360
+ `Project "${project.siteName}": addMessages "${key}" is already in the default catalog. Reword it in overrideMessages instead.`
361
+ );
362
+ }
363
+ const missing = project.enabledLocales.filter(
364
+ (locale) => typeof entry[locale] !== "string"
365
+ );
366
+ if (missing.length > 0) {
367
+ throw new Error(
368
+ `Project "${project.siteName}": addMessages "${key}" has no ${missing.join(", ")}. A message a project adds is written for every locale it publishes.`
369
+ );
370
+ }
371
+ }
327
372
  return project;
328
373
  }
@@ -22,7 +22,7 @@ import {
22
22
  buildNotFoundMeta,
23
23
  resolveShareImage,
24
24
  } from "../meta/index.ts";
25
- import type { ProjectInput, ProjectRouteId } from "../project.ts";
25
+ import type { AddedCatalog, ProjectInput, ProjectRouteId } from "../project.ts";
26
26
  import {
27
27
  buildRedirects,
28
28
  type RedirectRule,
@@ -102,7 +102,7 @@ export function createSite<
102
102
  project: P & { readonly enabledLocales: readonly Published[] }
103
103
  ): Site<
104
104
  Published,
105
- Catalog,
105
+ Catalog & AddedCatalog<P>,
106
106
  ProjectRouteId<Routes, P>,
107
107
  SectionPrefixOf<Routes, P["overrideRoutes"]>,
108
108
  OrphanSectionPrefixOf<Routes, P["overrideRoutes"]>
@@ -179,8 +179,15 @@ export function createSite<
179
179
  const llmsUrl: HttpsUrl | undefined =
180
180
  config_.llms === false ? undefined : joinUrl(siteUrl, `/${llmsName}`);
181
181
 
182
- const catalog = mergeCatalog<L>(messages, project.overrideMessages);
183
- const translateFactory = createTranslateFactory<Catalog, L>();
182
+ // The project's own messages join the catalog here, typed and resolved like
183
+ // any other: `Full` is what every reader below knows the keys by.
184
+ type Full = Catalog & AddedCatalog<P>;
185
+ const catalog = mergeCatalog<L>(
186
+ messages,
187
+ project.overrideMessages,
188
+ project.addMessages
189
+ );
190
+ const translateFactory = createTranslateFactory<Full, L>();
184
191
 
185
192
  const resolved = mergeRoutes<L, Routes>(routes, project.overrideRoutes);
186
193
 
@@ -831,14 +838,14 @@ export function createSite<
831
838
 
832
839
  translate: (locale) => translateFactory(catalog, locale),
833
840
  rich: (locale) =>
834
- createRichText<Catalog, L, RouteId>({
841
+ createRichText<Full, L, RouteId>({
835
842
  catalog,
836
843
  locale,
837
844
  link: richLink(locale),
838
845
  origin: siteUrl,
839
846
  }),
840
847
  plain: (locale) =>
841
- createPlainText<Catalog, L>({
848
+ createPlainText<Full, L>({
842
849
  catalog,
843
850
  locale,
844
851
  link: richLink(locale),