@uniweb/core 1.6.10 → 1.6.12

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": "@uniweb/core",
3
- "version": "1.6.10",
3
+ "version": "1.6.12",
4
4
  "description": "Core classes for the Uniweb platform - Uniweb, Website, Page, Block",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,7 +16,7 @@
16
16
 
17
17
  import { resolveFetchConfigs } from './fetch-config.js'
18
18
  import { fillRoutePattern } from './route-match.js'
19
- import { blockRouteQuery, blockFetchOptions, blockFillsOf, pageRecordConfig, planFor, runPlan, runPlanSync } from './page-data.js'
19
+ import { blockRouteQuery, blockFillsOf, pageRecordPlan, planFor, runPlan, runPlanSync } from './page-data.js'
20
20
 
21
21
  export default class EntityStore {
22
22
  /**
@@ -72,30 +72,14 @@ export default class EntityStore {
72
72
  * when the page's route query resolves to nothing
73
73
  */
74
74
  async fetchPageRecord(page, { dispatcher = this.website?.fetcher } = {}) {
75
- const dynamicContext = page?.dynamicContext
76
- if (!dynamicContext || !dispatcher) return null
75
+ if (!page?.dynamicContext || !dispatcher) return null
77
76
 
78
- // A block-shaped probe standing for the page itself: its levels, its route query.
79
- const probe = { fetch: null, page, website: this.website, dynamicContext: null }
80
- const route = blockRouteQuery(probe)
81
- const cfg = route && pageRecordConfig({
82
- pageFetch: page.fetch,
83
- parent: page.parent ?? null,
84
- route,
85
- site: this.website?.config?.fetch,
86
- options: blockFetchOptions(probe),
87
- })
88
- if (!cfg) return null
89
-
90
- const plan = planFor(new Map([[route.key, { cfg }]]), {
91
- dynamicContext,
92
- route,
93
- // The page is about ONE record, whatever a section's `current:` says.
94
- current: 'only',
95
- peekRecord: (id) => dispatcher.peekRecord?.(id),
96
- })
97
- const ctx = this._ctx(probe)
98
- return runPlan(plan, {
77
+ // The question, as `heldPageRecord` reads its answer back (`./page-data.js`).
78
+ const asked = pageRecordPlan(page, { website: this.website, peekRecord: (id) => dispatcher.peekRecord?.(id) })
79
+ if (!asked) return null
80
+ // A block-shaped probe standing for the page itself, as the context a dispatcher is handed.
81
+ const ctx = this._ctx({ fetch: null, page, website: this.website, dynamicContext: null })
82
+ return runPlan(asked.plan, {
99
83
  dispatch: (request) => dispatcher.dispatch(request, ctx),
100
84
  peek: (request) => dispatcher.peek?.(request, ctx),
101
85
  })
package/src/page-data.js CHANGED
@@ -240,6 +240,66 @@ export function blockFillsOf(block, meta, route = blockRouteQuery(block)) {
240
240
  })
241
241
  }
242
242
 
243
+ /**
244
+ * The plan for a parametric page's OWN record — its route query's program under `current: only` —
245
+ * read off the graph: the question the page's data step asks (`EntityStore#fetchPageRecord`), and the
246
+ * one `heldPageRecord` answers from what is held.
247
+ *
248
+ * @param {Object} page - a concrete parametric page (it carries `dynamicContext`)
249
+ * @param {Object} where
250
+ * @param {Object} where.website
251
+ * @param {Function} [where.peekRecord] - the record index, for the R1 gate
252
+ * @returns {{ key: string, plan: Map<string, Generator> }|null} null off a parametric page, or when
253
+ * its route query resolves to nothing
254
+ */
255
+ export function pageRecordPlan(page, { website, peekRecord = null } = {}) {
256
+ const dynamicContext = page?.dynamicContext
257
+ if (!dynamicContext) return null
258
+ // A block-shaped probe standing for the page itself: its levels, its route query.
259
+ const probe = { fetch: null, page, website, dynamicContext: null }
260
+ const route = blockRouteQuery(probe)
261
+ const cfg = route && pageRecordConfig({
262
+ pageFetch: page.fetch,
263
+ parent: page.parent ?? null,
264
+ route,
265
+ site: website?.config?.fetch,
266
+ options: blockFetchOptions(probe),
267
+ })
268
+ if (!cfg) return null
269
+ return {
270
+ key: route.key,
271
+ plan: planFor(new Map([[route.key, { cfg }]]), {
272
+ dynamicContext,
273
+ route,
274
+ // The page is about ONE record, whatever a section's `current:` says.
275
+ current: 'only',
276
+ peekRecord,
277
+ }),
278
+ }
279
+ }
280
+
281
+ /**
282
+ * A parametric page's own record, from what is held: the answer `pageRecordPlan`'s question got,
283
+ * read back after the page's data step. For a caller reporting what a page held, a record page's
284
+ * Markdown among them (`@uniweb/runtime`'s `section-records.js`).
285
+ *
286
+ * @param {Object} page
287
+ * @param {Object} where
288
+ * @param {Object} where.website
289
+ * @param {(request: Object) => ({ data: * }|null|undefined)} where.peek - the cache
290
+ * @param {Function} [where.peekRecord] - the record index
291
+ * @returns {Object|null|undefined} the record; null when the route names none (its answer is `[]`);
292
+ * undefined off a parametric page, or when nothing held answers the question
293
+ */
294
+ export function heldPageRecord(page, { website, peek, peekRecord = null } = {}) {
295
+ const asked = pageRecordPlan(page, { website, peekRecord })
296
+ if (!asked) return undefined
297
+ const out = runPlanSync(asked.plan, { peek })
298
+ const found = out.status === 'ready' ? out.data?.[asked.key] : undefined
299
+ if (found === undefined) return undefined
300
+ return Array.isArray(found) ? (found[0] ?? null) : found
301
+ }
302
+
243
303
  /**
244
304
  * The fetch a parametric page's OWN record is asked for with — its route query, resolved.
245
305
  *
package/src/website.js CHANGED
@@ -42,6 +42,24 @@ const SERVICE_RESOLVERS = {
42
42
  search: (website) => website.isSearchEnabled(),
43
43
  }
44
44
 
45
+ /**
46
+ * A value's entry for a language: by its tag, then each shorter tag, ignoring case (RFC 4647 §3.4
47
+ * lookup). `zh-Hant-TW` tries `zh-Hant-TW`, `zh-Hant`, `zh`. Returns the first non-empty entry.
48
+ *
49
+ * @param {Object} obj - entries keyed by language tag
50
+ * @param {string} lang - a language tag
51
+ * @returns {*} the entry, or undefined
52
+ */
53
+ function entryForLanguage(obj, lang) {
54
+ if (!obj || !lang) return undefined
55
+ const keys = Object.keys(obj)
56
+ for (let tag = String(lang); tag; tag = tag.slice(0, Math.max(tag.lastIndexOf('-'), 0))) {
57
+ const key = Object.hasOwn(obj, tag) ? tag : keys.find((k) => k.toLowerCase() === tag.toLowerCase())
58
+ if (key !== undefined && obj[key]) return obj[key]
59
+ }
60
+ return undefined
61
+ }
62
+
45
63
  /**
46
64
  * Website — orchestration root for a single site instance.
47
65
  *
@@ -1151,6 +1169,12 @@ export default class Website {
1151
1169
 
1152
1170
  /**
1153
1171
  * Localize a value
1172
+ *
1173
+ * A language finds its entry by its own tag, then by each shorter one (`zh-Hant-TW`, `zh-Hant`,
1174
+ * `zh`), without regard to case: the lookup of RFC 4647 §3.4. So a value keyed `zh` answers a site
1175
+ * whose languages are `zh-CN` and `zh-TW`, where an exact key alone left it empty (measured
1176
+ * 2026-11-10 on a site's footer).
1177
+ *
1154
1178
  * @param {any} val - Value to localize (object with lang keys, or string)
1155
1179
  * @param {string} defaultVal - Default value if not found
1156
1180
  * @param {string} givenLang - Override language
@@ -1160,23 +1184,17 @@ export default class Website {
1160
1184
  localize(val, defaultVal = '', givenLang = '', fallbackDefaultLangVal = false) {
1161
1185
  const lang = givenLang || this.activeLang
1162
1186
  const defaultLang = this.langs[0]?.value || 'en'
1187
+ const pick = (obj) =>
1188
+ entryForLanguage(obj, lang) || (fallbackDefaultLangVal ? entryForLanguage(obj, defaultLang) : undefined) || defaultVal
1163
1189
 
1164
- if (typeof val === 'object' && !Array.isArray(val)) {
1165
- return fallbackDefaultLangVal
1166
- ? val?.[lang] || val?.[defaultLang] || defaultVal
1167
- : val?.[lang] || defaultVal
1168
- }
1190
+ if (typeof val === 'object' && !Array.isArray(val)) return pick(val)
1169
1191
 
1170
1192
  if (typeof val === 'string') {
1171
1193
  if (!val.startsWith('{') && !val.startsWith('"')) return val
1172
1194
 
1173
1195
  try {
1174
1196
  const obj = JSON.parse(val)
1175
- if (typeof obj === 'object') {
1176
- return fallbackDefaultLangVal
1177
- ? obj?.[lang] || obj?.[defaultLang] || defaultVal
1178
- : obj?.[lang] || defaultVal
1179
- }
1197
+ if (typeof obj === 'object') return pick(obj)
1180
1198
  return obj
1181
1199
  } catch {
1182
1200
  return val