@museumwnf/viewer-core 2.17.0 → 2.18.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@museumwnf/viewer-core",
3
- "version": "2.17.0",
3
+ "version": "2.18.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",
@@ -7,6 +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
11
  import './styles/base.css'
11
12
 
12
13
  export function createViewer(config = {}) {
@@ -27,5 +28,20 @@ export function createViewer(config = {}) {
27
28
  connectLanguageToRouter({ locale: i18n.locale, offered: languages, router })
28
29
  bindDisplayLanguage(i18n.locale)
29
30
 
31
+ // Every link the site draws, its windows' included, goes to the new site
32
+ // of a legacy address that has one (inventory-app#2482).
33
+ const mount = app.mount.bind(app)
34
+ const unmount = app.unmount.bind(app)
35
+ let stopLinks = () => {}
36
+ app.mount = (...args) => {
37
+ const instance = mount(...args)
38
+ stopLinks = watchNewSiteLinks(typeof document === 'undefined' ? null : document.body)
39
+ return instance
40
+ }
41
+ app.unmount = () => {
42
+ stopLinks()
43
+ unmount()
44
+ }
45
+
30
46
  return app
31
47
  }
@@ -5,6 +5,7 @@ import { useDataPackage } from '../../composables/useDataPackage.js'
5
5
  import { byId, entityRef } from '../../composables/useEntities.js'
6
6
  import { projectLabel } from '../../conventions.js'
7
7
  import { useLegacyCountryCodes } from '../countryCodes.js'
8
+ import { toNewSite, withScheme } from '../../legacySites.js'
8
9
 
9
10
  // The data layer every DXA exhibition site (the-use-of-colours-in-art,
10
11
  // water-in-islam, …) wrote for itself: the entities an exhibition reads,
@@ -231,8 +232,9 @@ export function useExhibitionData(config = {}) {
231
232
  () => (exhibition.value?.sibling_sites ?? []).filter((s) => !s.hidden),
232
233
  )
233
234
 
235
+ // A sibling with a new site links to it (inventory-app#2482).
234
236
  function siblingUrl(sibling) {
235
- return sibling?.legacy_host || null
237
+ return sibling?.legacy_host ? toNewSite(withScheme(sibling.legacy_host)) : null
236
238
  }
237
239
 
238
240
  return {
@@ -4,6 +4,7 @@ import { useCatalogueData } from '../../composables/useCatalogueData.js'
4
4
  import { useDataPackage } from '../../composables/useDataPackage.js'
5
5
  import { itemFromUidPath as legacyItemFromUidPath, partnerFromKey as legacyPartnerFromKey } from '../../legacy/index.js'
6
6
  import { useLegacyCountryCodes } from '../countryCodes.js'
7
+ import { toNewSite, withScheme } from '../../legacySites.js'
7
8
 
8
9
  // The data layer every DXA gallery site (carpets, amulets, …) wrote for
9
10
  // itself: the entities a gallery reads, the lookup maps over them, its
@@ -124,8 +125,9 @@ export function useGalleryData(config = {}) {
124
125
  (gallery.value?.sibling_galleries ?? []).filter((g) => !g.hidden),
125
126
  )
126
127
 
128
+ // A sibling with a new site links to it (inventory-app#2482).
127
129
  function siblingUrl(sibling) {
128
- return sibling?.legacy_host || null
130
+ return sibling?.legacy_host ? toNewSite(withScheme(sibling.legacy_host)) : null
129
131
  }
130
132
 
131
133
  /** Four at random, reshuffled per visit. */
@@ -1,4 +1,5 @@
1
1
  import { Marked } from 'marked'
2
+ import { toNewSite } from '../legacySites.js'
2
3
 
3
4
  // One Markdown pipeline for every text on every website, configured here and
4
5
  // nowhere else. Its own instance rather than the shared `marked` singleton, so
@@ -56,10 +57,18 @@ const noCode = {
56
57
  // so a single newline means nothing. A record's field arrives from a database
57
58
  // where a newline was typed to be a line break, and dropping it runs the lines
58
59
  // together.
60
+ // A text's link to a legacy MWNF site that has a new one goes to the new site
61
+ // (inventory-app#2482); the text stays as it was written.
62
+ const newSiteLinks = {
63
+ walkTokens(token) {
64
+ if (token.type === 'link' && token.href) token.href = toNewSite(token.href)
65
+ },
66
+ }
67
+
59
68
  const prose = new Marked({ async: false, gfm: true, breaks: false })
60
69
  const record = new Marked({ async: false, gfm: true, breaks: true })
61
- prose.use(escaping, noCode)
62
- record.use(escaping, noCode)
70
+ prose.use(escaping, noCode, newSiteLinks)
71
+ record.use(escaping, noCode, newSiteLinks)
63
72
 
64
73
  // `blankLines` keeps the number of blank lines between two blocks. Legacy's
65
74
  // text pages were HTML, a blank line a `<br>`, and some put two blank lines
@@ -78,8 +87,8 @@ const blankLineSpacing = {
78
87
  }
79
88
  const proseLines = new Marked({ async: false, gfm: true, breaks: false })
80
89
  const recordLines = new Marked({ async: false, gfm: true, breaks: true })
81
- proseLines.use(escaping, noCode, blankLineSpacing)
82
- recordLines.use(escaping, noCode, blankLineSpacing)
90
+ proseLines.use(escaping, noCode, blankLineSpacing, newSiteLinks)
91
+ recordLines.use(escaping, noCode, blankLineSpacing, newSiteLinks)
83
92
 
84
93
  const pipeline = (breaks, blankLines = false) => {
85
94
  if (blankLines) return breaks ? recordLines : proseLines
package/src/index.js CHANGED
@@ -20,6 +20,7 @@ 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
24
 
24
25
  // The list pages: query state, pagination, facets, dates, keyword search.
25
26
  export { useListQuery } from './catalogue/listQuery.js'
@@ -0,0 +1,235 @@
1
+ import { siblingSiteUrl } from './rights.js'
2
+
3
+ // Links to legacy MWNF sites that have a new one (inventory-app#2482). The
4
+ // legacy sites linked to each other by address, and those addresses are in
5
+ // the texts a site shows (its package's, its locales', extracted from legacy
6
+ // as they were written) and in its configuration. A link to a legacy site
7
+ // that has a new site goes to the new one; the texts themselves stay as
8
+ // imported, and a link with no new site behind it stays as it is.
9
+ //
10
+ // The estate's legacy addresses, one row a site: a gallery, Explore, the
11
+ // galleries hub and the three products by host; an exhibition by host and
12
+ // its first path segment (they share a host); a mini-site by its own page
13
+ // (a Sharing History one by its `eId`). Built from the parity tool's sitemap
14
+ // (the legacy dumps) and the exporters' instance files; the parity `links`
15
+ // pass fails on a link this table misses.
16
+ export const LEGACY_SITES = [
17
+ { host: 'amulets.museumwnf.org', slug: 'amulets', family: 'gallery' },
18
+ { host: 'archaeology.museumwnf.org', slug: 'archaeology', family: 'gallery' },
19
+ { host: 'architectural-elements.museumwnf.org', slug: 'architectural-elements', family: 'gallery' },
20
+ { host: 'arms-armoury.museumwnf.org', slug: 'arms-armoury', family: 'gallery' },
21
+ { host: 'exhibitions.museumwnf.org', path: '/arts_in_dialogue/', slug: 'arts-in-dialogue', family: 'exhibition' },
22
+ { host: 'baroqueart.museumwnf.org', slug: 'baroqueart', family: 'baroqueart' },
23
+ { host: 'baroqueart.museumwnf.org', path: '/exhibitions/bar/absolutism/', slug: 'baroqueart-absolutism', family: 'minisite' },
24
+ { host: 'baroqueart.museumwnf.org', path: '/exhibitions/bar/bourgeoisie/', slug: 'baroqueart-bourgeoisie', family: 'minisite' },
25
+ { host: 'baroqueart.museumwnf.org', path: '/exhibitions/bar/devotion/', slug: 'baroqueart-devotion', family: 'minisite' },
26
+ { host: 'baroqueart.museumwnf.org', path: '/exhibitions/bar/enlightenment/', slug: 'baroqueart-enlightenment', family: 'minisite' },
27
+ { host: 'baroqueart.museumwnf.org', path: '/exhibitions/bar/ephemera/', slug: 'baroqueart-ephemera', family: 'minisite' },
28
+ { host: 'baroqueart.museumwnf.org', path: '/exhibitions/bar/languages_baroque/', slug: 'baroqueart-languages-baroque', family: 'minisite' },
29
+ { host: 'baroqueart.museumwnf.org', path: '/exhibitions/bar/reformation/', slug: 'baroqueart-reformation', family: 'minisite' },
30
+ { host: 'baroqueart.museumwnf.org', path: '/exhibitions/bar/travelling_and_exotism/', slug: 'baroqueart-travelling-and-exotism', family: 'minisite' },
31
+ { host: 'calligraphy.museumwnf.org', slug: 'calligraphy', family: 'gallery' },
32
+ { host: 'carpets.museumwnf.org', slug: 'carpets', family: 'gallery' },
33
+ { host: 'ceramics.museumwnf.org', slug: 'ceramics', family: 'gallery' },
34
+ { host: 'clothing.museumwnf.org', slug: 'clothing', family: 'gallery' },
35
+ { host: 'coins-medals.museumwnf.org', slug: 'coins-medals', family: 'gallery' },
36
+ { host: 'communication.museumwnf.org', slug: 'communication', family: 'gallery' },
37
+ { host: 'explore.museumwnf.org', slug: 'explore', family: 'explore' },
38
+ { host: 'funerary-objects.museumwnf.org', slug: 'funerary-objects', family: 'gallery' },
39
+ { host: 'galleries.museumwnf.org', slug: 'galleries', family: 'galleries' },
40
+ { host: 'glass.museumwnf.org', slug: 'glass', family: 'gallery' },
41
+ { host: 'gold-silver.museumwnf.org', slug: 'gold-silver', family: 'gallery' },
42
+ { host: 'cars.museumwnf.org', slug: 'historical-cars', family: 'gallery' },
43
+ { host: 'islamicart.museumwnf.org', slug: 'islamicart', family: 'islamicart' },
44
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/arabic_calligraphy/', slug: 'islamicart-arabic-calligraphy', family: 'minisite' },
45
+ { host: 'islamicart.museumwnf.org', path: '/gai/isl/', slug: 'islamicart-artistic-introduction', family: 'minisite' },
46
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/crusader/', slug: 'islamicart-crusader', family: 'minisite' },
47
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/fatimids/', slug: 'islamicart-fatimids', family: 'minisite' },
48
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/figural/', slug: 'islamicart-figural', family: 'minisite' },
49
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/floral/', slug: 'islamicart-floral', family: 'minisite' },
50
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/geometric/', slug: 'islamicart-geometric', family: 'minisite' },
51
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/mudejar_art/', slug: 'islamicart-mudejar-art', family: 'minisite' },
52
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/muslim_west/', slug: 'islamicart-muslim-west', family: 'minisite' },
53
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/pilgrimage/', slug: 'islamicart-pilgrimage', family: 'minisite' },
54
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/siculo_norman/', slug: 'islamicart-siculo-norman', family: 'minisite' },
55
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/the_abbasids/', slug: 'islamicart-the-abbasids', family: 'minisite' },
56
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/the_ayyubids/', slug: 'islamicart-the-ayyubids', family: 'minisite' },
57
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/the_mamluks/', slug: 'islamicart-the-mamluks', family: 'minisite' },
58
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/the_ottomans/', slug: 'islamicart-the-ottomans', family: 'minisite' },
59
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/the_umayyads/', slug: 'islamicart-the-umayyads', family: 'minisite' },
60
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/water/', slug: 'islamicart-water', family: 'minisite' },
61
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/western_influence/', slug: 'islamicart-western-influence', family: 'minisite' },
62
+ { host: 'islamicart.museumwnf.org', path: '/exhibitions/isl/women/', slug: 'islamicart-women', family: 'minisite' },
63
+ { host: 'ivory.museumwnf.org', slug: 'ivory', family: 'gallery' },
64
+ { host: 'jewellery.museumwnf.org', slug: 'jewellery', family: 'gallery' },
65
+ { host: 'landscapes.museumwnf.org', slug: 'landscapes', family: 'gallery' },
66
+ { host: 'leatherworks.museumwnf.org', slug: 'leatherworks', family: 'gallery' },
67
+ { host: 'manuscripts.museumwnf.org', slug: 'manuscripts', family: 'gallery' },
68
+ { host: 'metalwork.museumwnf.org', slug: 'metalwork', family: 'gallery' },
69
+ { host: 'mosaics.museumwnf.org', slug: 'mosaics', family: 'gallery' },
70
+ { host: 'musical-instruments.museumwnf.org', slug: 'musical-instruments', family: 'gallery' },
71
+ { host: 'paintings.museumwnf.org', slug: 'paintings', family: 'gallery' },
72
+ { host: 'photographs.museumwnf.org', slug: 'photographs', family: 'gallery' },
73
+ { host: 'porcelain.museumwnf.org', slug: 'porcelain', family: 'gallery' },
74
+ { host: 'portraits.museumwnf.org', slug: 'portraits', family: 'gallery' },
75
+ { host: 'precious-stones.museumwnf.org', slug: 'precious-stones', family: 'gallery' },
76
+ { host: 'prints-drawings.museumwnf.org', slug: 'prints-drawings', family: 'gallery' },
77
+ { host: 'religious-life.museumwnf.org', slug: 'religious-life', family: 'gallery' },
78
+ { host: 'scientific-objects.museumwnf.org', slug: 'scientific-objects', family: 'gallery' },
79
+ { host: 'sculptures.museumwnf.org', slug: 'sculptures', family: 'gallery' },
80
+ { host: 'sharinghistory.museumwnf.org', slug: 'sharinghistory', family: 'sharinghistory' },
81
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '3' }, slug: 'sharinghistory-cities-and-urban-spaces', family: 'minisite' },
82
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '6' }, slug: 'sharinghistory-economy-and-trade', family: 'minisite' },
83
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '5' }, slug: 'sharinghistory-fine-and-applied-arts', family: 'minisite' },
84
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '4' }, slug: 'sharinghistory-great-inventions-of-the-19th-century', family: 'minisite' },
85
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '7' }, slug: 'sharinghistory-international-exhibitions', family: 'minisite' },
86
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '8' }, slug: 'sharinghistory-migrations', family: 'minisite' },
87
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '9' }, slug: 'sharinghistory-music-literature-dance-and-fashion', family: 'minisite' },
88
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '1' }, slug: 'sharinghistory-rediscovering-the-past', family: 'minisite' },
89
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '10' }, slug: 'sharinghistory-reforms-and-social-changes', family: 'minisite' },
90
+ { host: 'sharinghistory.museumwnf.org', path: '/exhibitions/awe/exh_items.php', query: { eId: '11' }, slug: 'sharinghistory-travelling', family: 'minisite' },
91
+ { host: 'textiles.museumwnf.org', slug: 'textiles', family: 'gallery' },
92
+ { host: 'upgrade-exhibitions.museumwnf.org', path: '/the_hijaz_railway/', slug: 'the-hijaz-railway', family: 'exhibition' },
93
+ { host: 'exhibitions.museumwnf.org', path: '/the_table_is_set/', slug: 'the-table-is-set', family: 'exhibition' },
94
+ { host: 'exhibitions.museumwnf.org', path: '/the_use_of_colours_in_art/', slug: 'the-use-of-colours-in-art', family: 'exhibition' },
95
+ { host: 'theatre.museumwnf.org', slug: 'theatre', family: 'gallery' },
96
+ { host: 'toys-games.museumwnf.org', slug: 'toys-games', family: 'gallery' },
97
+ { host: 'wallpaintings.museumwnf.org', slug: 'wallpaintings', family: 'gallery' },
98
+ { host: 'exhibitions.museumwnf.org', path: '/water_in_islam/', slug: 'water-in-islam', family: 'exhibition' },
99
+ { host: 'weights-measures.museumwnf.org', slug: 'weights-measures', family: 'gallery' },
100
+ { host: 'exhibitions.museumwnf.org', path: '/with_brush_and_qalam/', slug: 'with-brush-and-qalam', family: 'exhibition' },
101
+ { host: 'woodwork.museumwnf.org', slug: 'woodwork', family: 'gallery' },
102
+ { host: 'exhibitions.museumwnf.org', path: '/the_hijaz_railway/', slug: 'the-hijaz-railway', family: 'exhibition' },
103
+ // Legacy's three products also answered on their own domains.
104
+ { host: 'discoverislamicart.org', slug: 'islamicart', family: 'islamicart' },
105
+ { host: 'discoverbaroqueart.org', slug: 'baroqueart', family: 'baroqueart' },
106
+ { host: 'sharinghistory.org', slug: 'sharinghistory', family: 'sharinghistory' },
107
+ ]
108
+
109
+ // The new route of each legacy page the products and their siblings link
110
+ // to: legacy's script name → the route. A page that is not here stays on
111
+ // legacy (a page with no counterpart, or one that needs a record id).
112
+ const PAGES = {
113
+ islamicart: {
114
+ '/': '/', '/index.php': '/',
115
+ '/database.php': '/database', '/database_results.php': '/database/results',
116
+ '/partners.php': '/partners', '/pm_partner_list.php': '/partners/results', '/pm_partner_list_eiac.php': '/partners/explore',
117
+ '/pc_entrance.php': '/permanent-collection', '/pclist_all.php': '/permanent-collection/results',
118
+ '/hcr_home.php': '/timeline', '/dynasties/': '/dynasties',
119
+ '/isl_missionstatement.php': '/mission-statement', '/recommendedlinks.php': '/recommended-links',
120
+ '/atrium_sponsor_detail.php': '/sponsors', '/glossarysearch.php': '/glossary-spelling',
121
+ },
122
+ baroqueart: {
123
+ '/': '/', '/index.php': '/',
124
+ '/database.php': '/database', '/database_results.php': '/database/results',
125
+ '/partners.php': '/partners', '/pm_partner_list.php': '/partners/results',
126
+ '/pc_entrance.php': '/permanent-collection', '/pclist_all.php': '/permanent-collection/results',
127
+ '/hcr_home.php': '/timeline', '/atrium_sponsor_detail.php': '/sponsors', '/mwnf_credit.php': '/credits',
128
+ },
129
+ sharinghistory: {
130
+ '/': '/', '/index.php': '/',
131
+ '/about.php': '/about', '/about_team.php': '/about/team', '/about_chronology_list.php': '/about/chronology',
132
+ '/pc_home.php': '/permanent-collection', '/database_searchform.php': '/database',
133
+ '/historical_background.php': '/historical-background', '/hcr_home.php': '/timeline',
134
+ '/exhibitions/awe/index.php': '/exhibitions', '/partners.php': '/partners', '/pm_partner_list.php': '/partners/results',
135
+ '/sponsor_detail.php': '/sponsors', '/sponsor_detail_local.php': '/sponsors/local',
136
+ },
137
+ }
138
+ // The query keys a page's new route reads as legacy's did.
139
+ const KEPT_QUERY = { '/partners/results': ['type'] }
140
+
141
+ const bareHost = (host) => host.toLowerCase().replace(/^www\./, '')
142
+
143
+ function rowFor(url) {
144
+ const host = bareHost(url.host)
145
+ const path = url.pathname.toLowerCase().replace(/\/?$/, '/')
146
+ return LEGACY_SITES.filter(
147
+ (row) =>
148
+ row.host === host &&
149
+ path.startsWith((row.path ?? '/').replace(/\.php$/, '.php/')) &&
150
+ Object.entries(row.query ?? {}).every(([key, value]) => url.searchParams.get(key) === value),
151
+ ).sort((a, b) => (b.path ?? '/').length - (a.path ?? '/').length)[0]
152
+ }
153
+
154
+ /**
155
+ * The route on the new site that a legacy address of `row` lands on, or null
156
+ * when it has none. Galleries, Explore and the hub kept legacy's paths as
157
+ * routes (their legacy redirects); an exhibition drops its own segment and
158
+ * moves the language to the query; a mini-site opens on its home; a
159
+ * product's page goes through PAGES.
160
+ */
161
+ function routeFor(row, url) {
162
+ // A legacy text may write the address with a space before its closing `>`.
163
+ const pathname = decodeURIComponent(url.pathname).trim() || '/'
164
+ const path = pathname.replace(/\/+$/, '') || '/'
165
+ if (row.family === 'gallery' || row.family === 'explore' || row.family === 'galleries') return `${path}${url.search}`
166
+ if (row.family === 'minisite') return '/'
167
+ if (row.family === 'exhibition') {
168
+ const rest = path.split('/').filter(Boolean).slice(1)
169
+ const lang = /^[a-z]{2}$/.test(rest[0] ?? '') ? rest.shift() : null
170
+ return `/${rest.join('/')}${lang ? `?lang=${lang}` : ''}`
171
+ }
172
+ const route = PAGES[row.family]?.[pathname.toLowerCase()]
173
+ if (!route) return null
174
+ const keep = (KEPT_QUERY[route] ?? []).filter((key) => url.searchParams.has(key))
175
+ return keep.length ? `${route}?${keep.map((key) => `${key}=${encodeURIComponent(url.searchParams.get(key))}`).join('&')}` : route
176
+ }
177
+
178
+ function fixAnchor(anchor) {
179
+ const href = anchor.getAttribute('href')
180
+ const next = toNewSite(href)
181
+ if (next !== href) anchor.setAttribute('href', next)
182
+ }
183
+
184
+ function fixWithin(node) {
185
+ if (node.nodeType !== 1) return
186
+ if (node.matches('a[href]')) fixAnchor(node)
187
+ for (const anchor of node.querySelectorAll('a[href]')) fixAnchor(anchor)
188
+ }
189
+
190
+ /**
191
+ * Keeps every link under `root` on the new sites: each anchor drawn, or
192
+ * given a new `href`, whose address is a legacy site's that has a new one
193
+ * gets the new address. Components, Markdown and a site's own markup all
194
+ * link through it, so no family has to repeat the rule. Its own change
195
+ * fires once more and finds nothing to do. Returns the function that stops
196
+ * it.
197
+ */
198
+ export function watchNewSiteLinks(root) {
199
+ if (!root || typeof MutationObserver === 'undefined') return () => {}
200
+ fixWithin(root)
201
+ const observer = new MutationObserver((records) => {
202
+ for (const record of records) {
203
+ if (record.type === 'attributes') fixAnchor(record.target)
204
+ else record.addedNodes.forEach(fixWithin)
205
+ }
206
+ })
207
+ observer.observe(root, { subtree: true, childList: true, attributes: true, attributeFilter: ['href'] })
208
+ return () => observer.disconnect()
209
+ }
210
+
211
+ /** An address as legacy stored a host (`ceramics.museumwnf.org`), with its scheme. */
212
+ export function withScheme(address) {
213
+ return /^[a-z][a-z0-9+.-]*:/i.test(address) ? address : `https://${address}`
214
+ }
215
+
216
+ /**
217
+ * `href` on the new site when it is a legacy MWNF address of a site that has
218
+ * one, else `href` itself. Needs the site's declared origin (`site.origin`):
219
+ * without it every link stays as it is.
220
+ */
221
+ export function toNewSite(href) {
222
+ if (typeof href !== 'string') return href
223
+ let url
224
+ try {
225
+ url = new URL(href.trim())
226
+ } catch {
227
+ return href
228
+ }
229
+ if (!/^https?:$/.test(url.protocol)) return href
230
+ const row = rowFor(url)
231
+ if (!row) return href
232
+ const route = routeFor(row, url)
233
+ if (route === null) return href
234
+ return siblingSiteUrl(row.slug, route) ?? href
235
+ }