@museumwnf/viewer-core 2.25.2 → 2.27.0

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/README.md CHANGED
@@ -157,6 +157,7 @@ What a website's `main.js` still writes, and why it cannot move here:
157
157
  | `media` | `{ legacyHost }` | no | the host of the legacy media server, for [`mediaUrl()`](#the-declaration-outside-a-component) |
158
158
  | `links` | object | no | the addresses a website links out to: `portal`, `galleries`, `myCollection`, `about`, `contact`, `legalNotice`, `credits`, `cookies` |
159
159
  | `site.origin` | string | no | the absolute origin this build is deployed at, base path included when there is one (`https://metanull.github.io/islamicart`, no trailing slash) — declared once here rather than baked into the package, since the package cannot know where it will be served; read by [`sourceUrl()`](#the-declaration-outside-a-component) |
160
+ | `site.legacyHost` | string | no | the legacy host this site replaces (`books.museumwnf.org`): once that host points at the site, an old address on it (`/book/5/en`) opens the page it names (see [Old addresses](#old-addresses-at-the-domain-switch)); nothing without it |
160
161
  | `site.slug` | string | no | the site's slug, as in the exporters' instance files; defaults to the last segment of `site.origin`. The global search's link sends it as `from` |
161
162
  | `globalSearch` | `true` or `{ url }` | no | **off by default.** Turns on the link to the global search (inventory-app M15, ADR 005): `true` for its default address (`GLOBAL_SEARCH_URL`), `{ url }` for another https address. viewer-layout's header then shows its search box. The parity check turns it on for the sites whose legacy had a similar feature |
162
163
 
@@ -233,7 +234,7 @@ normally reaches for that instead of calling `mergeMessages` itself.
233
234
  | `<I18nText keypath tag="div">` | a text rendered as Markdown (block) |
234
235
  | `<I18nTextInline keypath tag="span">` | the same, without the surrounding paragraph |
235
236
  | `mergeMessages(...catalogues)` | the merge rule above |
236
- | `negotiateLanguage`, `isRtl` | the language rules, for a website that needs them directly |
237
+ | `negotiateLanguage`, `isRtl`, `LEGACY_LANGUAGE_CODES` | the language rules, for a website that needs them directly |
237
238
  | `renderBlock(text, { breaks, glossary })`, `renderInline(text, { breaks, glossary })`, `renderPlain(text)` | the Markdown pipeline `I18nText`/`I18nTextInline` render through, for a website that needs it directly (a data-package field, not a text) |
238
239
  | `md(text, { glossary, breaks = true })`, `mdInline(text, { glossary })`, `mdStrip(text)` | the same three, under the record-field convention: `''` for a missing text, line breaks kept by default (a text in the dictionary is prose; a record's field is typed with breaks on purpose) |
239
240
 
@@ -284,8 +285,10 @@ visitor's remembered choice, then their browser's preferences, then English —
284
285
  skipping at every step anything the website does not offer. On a website
285
286
  offering more than one language the URL carries `?lang=`, so a page can be
286
287
  linked and shared in the language it was read in; a single-language website
287
- never gets the parameter. `lang` and `dir` are set on `<html>`, so a
288
- right-to-left language renders right to left.
288
+ never gets the parameter. Legacy MWNF's own codes `se`, `ch` and `si`
289
+ (`LEGACY_LANGUAGE_CODES`) select `sv`, `zh` and `sl` on a website that offers
290
+ the ISO code, so an old link keeps its language. `lang` and `dir` are set on
291
+ `<html>`, so a right-to-left language renders right to left.
289
292
 
290
293
  ### Which languages a website offers
291
294
 
@@ -483,6 +486,7 @@ under the main partner it belongs to.
483
486
  | `useSection()` | the `meta.section` of the current route — see Routing |
484
487
  | `useFeaturedRecord(entity, { withImage = true, seed, filter })` | one record at random for a landing page's spotlight, among those with an image; null until the entity is loaded; `seed` pins the pick; `filter(record)` applies a site's own visible rule first |
485
488
  | `sectionMeta(chrome = [])` | returns `meta(section, ...entities) => ({ section, entities: [...chrome, ...entities] })` — a route's `meta` in one call, `chrome` being the entities every page of the site loads regardless of which one it is |
489
+ | `mwnfPortalPages` | the portal's own pages a site's footer links to (`chronology`, `support`, `volunteers`, `honoraryCommittee`, `commemoration`, `pressLounge`, `languagePolicy`, `leagueOfArabStates`, `cafeteria`, `partnerList`) — frozen, kept apart from `mwnfLinks` so the DXA configs' `links` do not change |
486
490
  | `mwnfLinks` | the portal and sibling-site addresses the four DXA configs each repeat under `links` — frozen, spread into a site's own `links` rather than retyped |
487
491
 
488
492
  #### Projects, from the data package (epic metanull/inventory-app#1727)
@@ -706,6 +710,9 @@ The convention, applied by the router every website shares:
706
710
  location (`{ name, params }` or a path) or nothing, which is the not-found
707
711
  page. It may be async, so a website can load the entity it maps a legacy
708
712
  key through `backward_compatibility` with;
713
+ - an old address of the legacy site this one replaces, once its host points
714
+ here, is a path the hash router never sees: see
715
+ [Old addresses](#old-addresses-at-the-domain-switch);
709
716
  - a route says what section it belongs to — `meta: { section: 'collection' }`
710
717
  — and a shell reads it with `useSection()` for the banner title or the
711
718
  active menu entry, instead of deriving it from the path;
@@ -734,6 +741,23 @@ export default {
734
741
  }
735
742
  ```
736
743
 
744
+ ### Old addresses at the domain switch
745
+
746
+ The router uses hash history, so an old address of the legacy site
747
+ (`https://books.museumwnf.org/book/5/en`) is a path it never sees, and a
748
+ static host has no file for it. Once the legacy host points at the new site:
749
+
750
+ 1. the build adds a `404.html` that is the site itself:
751
+ `defineViewerConfig({ ..., notFoundPage: true })`;
752
+ 2. the site declares the host it replaces: `site.legacyHost`;
753
+ 3. before the router starts, `createViewer` calls
754
+ `openLegacyAddress(config.site)`: the address becomes
755
+ `<base>#<route>`, the route `legacyAddressRoute()` gives (the same
756
+ table `toNewSite()` rewrites links with), or the not-found page.
757
+
758
+ `legacyAddressRoute(href)` answers the new site's route of any legacy MWNF
759
+ address, or null. A site without `site.legacyHost` is unchanged.
760
+
737
761
  ### Views
738
762
 
739
763
  Every website has three slots — the home page, a list page, a record page —
@@ -806,7 +830,7 @@ The package exports shared smoke-test helpers at @museumwnf/viewer-core/testing:
806
830
  | checkRoutes(config, { names, legacyPaths }) | Verify every route has a name, no route is a catch-all, and expected names and legacy paths are present. Returns the list of problems found. |
807
831
  | checkSectionMeta(config) | Verify every route has a meta.section string (used by the menu to highlight the current page). Returns the list of problems found. |
808
832
  | checkTextsRendered(host, { namespaces }) | Match text keys in the rendered host against a regex like /\b(ns1\|ns2)\.[a-z]/i. Returns the list of namespaces found (empty if none). |
809
- | defineViewerConfig({ dataPackage, inline, plugins, root }) | Return the Vite config object every website shares: the @inventory-data alias, optimizeDeps to inline viewer packages, testTimeout of 60s, and the test environment. Exported from `@museumwnf/viewer-core/vite` for use in `vite.config.js` (separate entry because Vite's config loader runs under Node.js, which cannot parse `.vue` files; the `/testing` barrel reaches a `.vue` file). dataPackage is the npm package name; inline is an optional array of extra packages; plugins is the Vite plugins array (e.g. @vitejs/plugin-vue); root is the project root path (default `process.cwd()`), used to resolve the data package from the project root. |
833
+ | defineViewerConfig({ dataPackage, inline, plugins, notFoundPage, root }) | Return the Vite config object every website shares: the @inventory-data alias, optimizeDeps to inline viewer packages, testTimeout of 60s, and the test environment. Exported from `@museumwnf/viewer-core/vite` for use in `vite.config.js` (separate entry because Vite's config loader runs under Node.js, which cannot parse `.vue` files; the `/testing` barrel reaches a `.vue` file). dataPackage is the npm package name; inline is an optional array of extra packages; plugins is the Vite plugins array (e.g. @vitejs/plugin-vue); root is the project root path (default `process.cwd()`), used to resolve the data package from the project root. notFoundPage (default false) adds the build's `404.html`, a copy of its `index.html`, for [old addresses](#old-addresses-at-the-domain-switch). |
810
834
  | checkOfferedLanguages(config) | Check that the website offers only languages with content, in the declared order, with labels on the switcher. Returns the list of problems found. |
811
835
 
812
836
  @museumwnf/viewer-core/testing is not imported by websites, only by their tests. A website test might be:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@museumwnf/viewer-core",
3
- "version": "2.25.2",
3
+ "version": "2.27.0",
4
4
  "description": "Application engine for MWNF websites: turns a dataset.config.js into a mounted Vue application.",
5
5
  "type": "module",
6
6
  "license": "SEE LICENSE IN LICENSE.md",
@@ -166,3 +166,22 @@ export const mwnfLinks = Object.freeze({
166
166
  baroqueArt: 'https://baroqueart.museumwnf.org',
167
167
  sharingHistory: 'https://sharinghistory.museumwnf.org',
168
168
  })
169
+
170
+ /**
171
+ * The portal's own pages a site's footer links to (the Publications footer,
172
+ * as legacy's books pages drew it, read from the portal's footer). Kept here
173
+ * so they change in one place when the portal does; apart from `mwnfLinks`,
174
+ * which the DXA configs copy whole into their `links`.
175
+ */
176
+ export const mwnfPortalPages = Object.freeze({
177
+ chronology: 'https://www.museumwnf.org/about/chronology',
178
+ support: 'https://www.museumwnf.org/about/support',
179
+ volunteers: 'https://www.museumwnf.org/about/volunteers',
180
+ honoraryCommittee: 'https://www.museumwnf.org/about/honorary-committee',
181
+ commemoration: 'https://www.museumwnf.org/about/commemoration',
182
+ pressLounge: 'https://www.museumwnf.org/about/press-lounge',
183
+ languagePolicy: 'https://www.museumwnf.org/about/language-policy',
184
+ leagueOfArabStates: 'https://www.museumwnf.org/league-of-arab-states/',
185
+ cafeteria: 'https://www.museumwnf.org/cafeteria/',
186
+ partnerList: 'https://www.museumwnf.org/partner_list.php',
187
+ })
@@ -7,7 +7,7 @@ import { connectLanguageToRouter, resolveInitialLanguage } from './i18n/language
7
7
  import { VIEWER_CONFIG } from './injectionKeys.js'
8
8
  import { createViewerRouter } from './router/index.js'
9
9
  import { setSiteConfig } from './siteConfig.js'
10
- import { watchNewSiteLinks } from './legacySites.js'
10
+ import { openLegacyAddress, watchNewSiteLinks } from './legacySites.js'
11
11
  import './styles/base.css'
12
12
 
13
13
  export function createViewer(config = {}) {
@@ -16,6 +16,9 @@ export function createViewer(config = {}) {
16
16
  const messages = config.messages ?? {}
17
17
  const resolved = { ...config, languages }
18
18
 
19
+ // An old link to the legacy site this one replaces opens the page it
20
+ // names (`site.legacyHost`; nothing without it).
21
+ openLegacyAddress(config.site)
19
22
  const router = createViewerRouter(config)
20
23
  const i18n = createI18n({ messages, locale: resolveInitialLanguage(languages) })
21
24
 
@@ -17,20 +17,30 @@ export function isRtl(code) {
17
17
  return RTL_LANGUAGES.has(String(code ?? '').toLowerCase().split('-')[0])
18
18
  }
19
19
 
20
+ /**
21
+ * Legacy MWNF's own codes where they differ from ISO 639-1: Swedish, Chinese
22
+ * and Slovenian. The sites offer the ISO code; an old address (`?lang=se`) or
23
+ * a choice remembered before the switch still selects the language.
24
+ */
25
+ export const LEGACY_LANGUAGE_CODES = Object.freeze({ se: 'sv', ch: 'zh', si: 'sl' })
26
+
20
27
  /**
21
28
  * The URL first, then the visitor's own earlier choice, then what their
22
29
  * browser asks for, then English. A language that is not offered is ignored at
23
30
  * every step, so nothing here can select a language the website does not have.
31
+ * A legacy code (LEGACY_LANGUAGE_CODES) matches its ISO language when the
32
+ * website does not offer the legacy code itself.
24
33
  */
25
34
  export function negotiateLanguage(offered, { requested, stored, preferred = [] } = {}) {
26
35
  const list = offered?.length ? offered : ['en']
27
36
  const match = (code) => {
28
37
  if (!code) return undefined
29
38
  const wanted = String(code).toLowerCase()
30
- return (
31
- list.find((candidate) => candidate.toLowerCase() === wanted) ??
32
- list.find((candidate) => candidate.toLowerCase().split('-')[0] === wanted.split('-')[0])
33
- )
39
+ const found = (code) =>
40
+ list.find((candidate) => candidate.toLowerCase() === code) ??
41
+ list.find((candidate) => candidate.toLowerCase().split('-')[0] === code.split('-')[0])
42
+ const iso = LEGACY_LANGUAGE_CODES[wanted.split('-')[0]]
43
+ return found(wanted) ?? (iso ? found(iso) : undefined)
34
44
  }
35
45
  return (
36
46
  match(requested) ??
package/src/index.js CHANGED
@@ -13,14 +13,14 @@ export { useDataPackage } from './composables/useDataPackage.js'
13
13
  export { byId, entityRef, loadEntities, useEntities } from './composables/useEntities.js'
14
14
  export { resolveRecordLanguage, useRecordLanguage } from './composables/useRecordLanguage.js'
15
15
  export { createI18n, mergeMessages, useI18n, useLocale } from './i18n/index.js'
16
- export { isRtl, negotiateLanguage } from './i18n/language.js'
16
+ export { LEGACY_LANGUAGE_CODES, isRtl, negotiateLanguage } from './i18n/language.js'
17
17
  export { md, mdInline, mdStrip, renderBlock, renderInline, renderPlain } from './i18n/markdown.js'
18
18
  export { languageLabels, offeredLanguages } from './languages.js'
19
19
  export { defaultScrollBehavior, resolveViews } from './router/index.js'
20
20
  export { mediaUrl, useSiteConfig } from './siteConfig.js'
21
21
  export { bindDisplayLanguage, displayLanguage } from './displayLanguage.js'
22
22
  export { siblingSiteUrl, sourceUrl, useSiteRights } from './rights.js'
23
- export { LEGACY_SITES, toNewSite, watchNewSiteLinks, withScheme } from './legacySites.js'
23
+ export { LEGACY_SITES, legacyAddressRoute, openLegacyAddress, toNewSite, watchNewSiteLinks, withScheme } from './legacySites.js'
24
24
  export { GLOBAL_SEARCH_URL, globalSearchUrl, siteSlug, useGlobalSearch } from './globalSearch.js'
25
25
  export { legacyRecordId } from './legacyIds.js'
26
26
 
@@ -53,7 +53,7 @@ export { groupByCountry, partnerHierarchy, partnerView } from './record/partners
53
53
 
54
54
  // Conventions every website had written for itself.
55
55
  export {
56
- mwnfLinks, projectLabel, projectLinks, sectionMeta, useFeaturedRecord,
56
+ mwnfLinks, mwnfPortalPages, projectLabel, projectLinks, sectionMeta, useFeaturedRecord,
57
57
  useProjects, useSection,
58
58
  } from './conventions.js'
59
59
 
@@ -1,5 +1,6 @@
1
1
  import { legacyRecordId } from './legacyIds.js'
2
2
  import { siblingSiteUrl } from './rights.js'
3
+ import { LEGACY_LANGUAGE_CODES } from './i18n/language.js'
3
4
 
4
5
  // Links to legacy MWNF sites that have a new one (inventory-app#2482). The
5
6
  // legacy sites linked to each other by address, and those addresses are in
@@ -171,11 +172,10 @@ function publicationsRoute(path, url) {
171
172
  // A legacy address writes its language as legacy MWNF wrote it; the sites
172
173
  // offer it as ISO 639-1. The two differ only for Swedish, Chinese and
173
174
  // Slovenian; the other legacy codes are ISO already.
174
- const LEGACY_LANGUAGES = { se: 'sv', ch: 'zh', si: 'sl' }
175
175
  function isoLanguage(code) {
176
176
  if (!/^[a-z]{2}$/i.test(code ?? '')) return null
177
177
  const lang = code.toLowerCase()
178
- return LEGACY_LANGUAGES[lang] ?? lang
178
+ return LEGACY_LANGUAGE_CODES[lang] ?? lang
179
179
  }
180
180
 
181
181
  // Legacy travels.museumwnf.org's Exhibition Trails: a country's or a theme's
@@ -334,3 +334,51 @@ export function toNewSite(href) {
334
334
  if (route === null) return href
335
335
  return siblingSiteUrl(row.slug, route) ?? href
336
336
  }
337
+
338
+ /**
339
+ * The route on the new site that a legacy MWNF address lands on (a book's
340
+ * `/publication/<id>/<language>` for `books.museumwnf.org/book/5/en`), or
341
+ * null when the address is no legacy site's, or names nothing there.
342
+ */
343
+ export function legacyAddressRoute(href) {
344
+ let url
345
+ try {
346
+ url = new URL(String(href ?? '').trim())
347
+ } catch {
348
+ return null
349
+ }
350
+ const row = rowFor(url)
351
+ return row ? routeFor(row, url) : null
352
+ }
353
+
354
+ /**
355
+ * Turns the page's own address into the route it names when it is one of
356
+ * the site's legacy addresses: a site declares the legacy host it replaces
357
+ * (`site.legacyHost`, e.g. `books.museumwnf.org`) and its address
358
+ * (`site.origin`). Once that host points at the site, an old link such as
359
+ * `/book/5/en` reaches the build's 404 page (`defineViewerConfig({
360
+ * notFoundPage: true })`), which is the site itself: before the router
361
+ * starts, the address becomes `<base>#/publication/<id>/en`. An address
362
+ * that names nothing goes to the not-found page. Does nothing without
363
+ * `legacyHost`, on the site's home, or when the address already has a
364
+ * route. Returns the address it moved to, or null.
365
+ */
366
+ export function openLegacyAddress(site, win = typeof window === 'undefined' ? undefined : window) {
367
+ const host = site?.legacyHost
368
+ if (!host || !win?.location || !win.history?.replaceState) return null
369
+ const { pathname, search, hash } = win.location
370
+ if (hash && hash !== '#' && hash !== '#/') return null
371
+ let base = '/'
372
+ try {
373
+ base = new URL(site.origin).pathname.replace(/\/?$/, '/')
374
+ } catch {
375
+ // No origin: the site is served at the root.
376
+ }
377
+ if (!pathname.startsWith(base)) return null
378
+ const rest = `/${pathname.slice(base.length)}`
379
+ if (rest === '/' || rest === '/index.html' || rest === '/404.html') return null
380
+ const route = legacyAddressRoute(`https://${bareHost(host)}${rest}${search}`) ?? rest
381
+ const target = `${base}#${route}`
382
+ win.history.replaceState(win.history.state, '', target)
383
+ return target
384
+ }
@@ -11,6 +11,9 @@ import path from 'node:path'
11
11
  * the required `'@museumwnf/viewer-core'` and `'@museumwnf/viewer-layout'`.
12
12
  * `plugins` is an optional array of Vite plugins (e.g. `@vitejs/plugin-vue`),
13
13
  * which the site must supply because this package does not depend on them.
14
+ * `notFoundPage`, when true, adds the build's `404.html`: a copy of its
15
+ * `index.html`, so GitHub Pages serves the site itself for an address it
16
+ * has no file for, and an old link reaches `openLegacyAddress`.
14
17
  * `root` is an optional project root path (default `process.cwd()`); use it when
15
18
  * loading the config from a context other than the project directory.
16
19
  */
@@ -18,10 +21,11 @@ export function defineViewerConfig({
18
21
  dataPackage,
19
22
  inline = [],
20
23
  plugins = [],
24
+ notFoundPage = false,
21
25
  root = process.cwd(),
22
26
  } = {}) {
23
27
  return {
24
- plugins,
28
+ plugins: notFoundPage ? [...plugins, notFoundPagePlugin()] : plugins,
25
29
  resolve: {
26
30
  alias: {
27
31
  // viewer-core reads every JSON of the data package through this alias.
@@ -73,3 +77,16 @@ export function defineViewerConfig({
73
77
  },
74
78
  }
75
79
  }
80
+
81
+ /** The build's `404.html`, a copy of its `index.html` (`notFoundPage`). */
82
+ export function notFoundPagePlugin() {
83
+ return {
84
+ name: 'viewer-not-found-page',
85
+ apply: 'build',
86
+ enforce: 'post',
87
+ generateBundle(_options, bundle) {
88
+ const index = bundle['index.html']
89
+ if (index?.type === 'asset') this.emitFile({ type: 'asset', fileName: '404.html', source: index.source })
90
+ },
91
+ }
92
+ }