@uniweb/build 1.12.0 → 1.12.2

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/build",
3
- "version": "1.12.0",
3
+ "version": "1.12.2",
4
4
  "description": "Build tooling for the Uniweb Component Web Platform",
5
5
  "type": "module",
6
6
  "exports": {
@@ -59,9 +59,9 @@
59
59
  "@uniweb/content-reader": "~1.5.7",
60
60
  "@uniweb/schemas": "~1.6.7",
61
61
  "@uniweb/theming": "~1.2.1",
62
+ "@uniweb/projections": "~1.2.8",
62
63
  "@uniweb/semantic-parser": "~1.13.0",
63
- "@uniweb/content-writer": "~1.3.1",
64
- "@uniweb/projections": "~1.2.8"
64
+ "@uniweb/content-writer": "~1.3.1"
65
65
  },
66
66
  "optionalDependencies": {
67
67
  "@uniweb/runtime": "~1.2.14"
@@ -73,8 +73,8 @@
73
73
  "@tailwindcss/vite": "^4.0.0",
74
74
  "@vitejs/plugin-react": "^4.0.0 || ^5.0.0",
75
75
  "vite-plugin-svgr": "^4.0.0",
76
- "@uniweb/core": "~1.5.0",
77
- "@uniweb/runtime": "~1.2.14"
76
+ "@uniweb/runtime": "~1.2.14",
77
+ "@uniweb/core": "~1.5.0"
78
78
  },
79
79
  "peerDependenciesMeta": {
80
80
  "vite": {
@@ -0,0 +1,171 @@
1
+ /**
2
+ * ⭐ A LINK TO A PAGE IS A PATH IN FILES AND AN ID IN THE STORE (2026-10-29) [Diego: "When working
3
+ * on the app, it is convenient. but with md content, it's easier to see paths than ids"].
4
+ *
5
+ * An editor links a page by its stable id (`page:3f58142d`): an author picks the page from a list,
6
+ * and the id keeps the link when the page is renamed there, since a store keeps a link as written.
7
+ * A file is read and typed, and `page:ai` says where it goes. So the sync translates where it
8
+ * already reads rich text: a push stores a link by its page's id when the page has one
9
+ * (`storedPageRef`), and a pull writes a stored link as its page's path (`writtenPageRef`). A pull
10
+ * and then a push change nothing, each turning back what the other wrote.
11
+ *
12
+ * ⭐ ONLY A CODE IS TRANSLATED. An id an author chose is a word (`installation`, `home`), reads as
13
+ * well as a path, and is kept as written both ways; translating it would rewrite a file a round trip
14
+ * should leave alone (measured: a docs site's ten files, on the round trip that caught it). A code
15
+ * (`isCodeId`: hex with a digit, as an editor mints it) is what a file shows as the page's path, and
16
+ * what a push stores for a path to a page that has one. A code an author typed into a file comes
17
+ * back the page's path after one pull.
18
+ *
19
+ * Both read a reference as the runtime does (`core/src/website.js`, `_pageIdMap`): by a page's
20
+ * stable id, else by its route, the homepage's being `page:/`. So a path written into a file names
21
+ * the page its id named. A path that another page's id claims, and a parametric page, stay ids.
22
+ *
23
+ * A reference lives in a link or button mark's `href`, a linked image's `href`, any string in a
24
+ * section's params that is one `page:` reference (a `detailPage`, a button's link), and a page's
25
+ * `redirect`. ⛔ Never in text: a code span that reads `page:about` is words about a link.
26
+ */
27
+
28
+ const PAGE_REF = /^page:([^#]*)(#.*)?$/
29
+
30
+ /** A route as a `page:` reference names it: no leading or trailing slash, the homepage `''`. */
31
+ const routeKey = (route) => String(route).replace(/^\/+/, '').replace(/\/+$/, '')
32
+
33
+ /**
34
+ * Whether a page's id is a code rather than a word: eight or more hex characters with a digit
35
+ * among them, the shape an editor mints (`3f58142d`). `installation`, `home` and `v1-guide` are
36
+ * words, and so is `cafe`.
37
+ *
38
+ * @param {string|null} id
39
+ * @returns {boolean}
40
+ */
41
+ export function isCodeId(id) {
42
+ return typeof id === 'string' && /^(?=[0-9a-f]*[0-9])[0-9a-f]{8,}$/.test(id)
43
+ }
44
+
45
+ /**
46
+ * The site's pages, each with the route a build gives it, in page order.
47
+ *
48
+ * @param {Array<object>} pages - page records (`stable_id`, `slug`, `$children`, …)
49
+ * @param {string} sourceLocale
50
+ * @param {(record: object, parentRoute: string, sourceLocale: string) => string} routeOf -
51
+ * `buildRouteOf`, passed in so this module stays a leaf
52
+ * @returns {Array<{ id: string|null, route: string, dynamic: boolean }>}
53
+ */
54
+ export function pageRefPages(pages, sourceLocale, routeOf) {
55
+ const out = []
56
+ const walk = (list, parentRoute) => {
57
+ for (const record of list || []) {
58
+ if (!record || typeof record !== 'object') continue
59
+ const route = routeOf(record, parentRoute, sourceLocale)
60
+ out.push({ id: typeof record.stable_id === 'string' && record.stable_id ? record.stable_id : null, route, dynamic: record.is_dynamic === true })
61
+ walk(record.$children, route)
62
+ }
63
+ }
64
+ walk(pages, '/')
65
+ return out
66
+ }
67
+
68
+ /**
69
+ * What a `page:` reference resolves to among a site's pages, by the runtime's rule: a page's id
70
+ * (which wins over any route), else its route, a target read the way a route is.
71
+ *
72
+ * @param {Array<{ id: string|null, route: string, dynamic: boolean }>} pages - `pageRefPages`
73
+ * @returns {{ resolve: (target: string) => object|undefined }}
74
+ */
75
+ export function pageRefIndex(pages) {
76
+ const byName = new Map()
77
+ for (const page of pages) {
78
+ if (page.id) byName.set(page.id, page)
79
+ const key = routeKey(page.route)
80
+ if (!byName.has(key)) byName.set(key, page)
81
+ }
82
+ return {
83
+ resolve: (target) =>
84
+ typeof target === 'string' ? (byName.get(target) ?? byName.get(routeKey(target))) : undefined,
85
+ }
86
+ }
87
+
88
+ /**
89
+ * A push's form of a reference: `page:<code>` when the page it names has a code for its id
90
+ * (`isCodeId`), else as written — a path to a page whose id is a word, or none, stays a path. A
91
+ * section after `#` rides along unchanged.
92
+ *
93
+ * @param {string} ref
94
+ * @param {{ resolve: Function }} index - `pageRefIndex`
95
+ * @returns {string}
96
+ */
97
+ export function storedPageRef(ref, index) {
98
+ const m = typeof ref === 'string' ? PAGE_REF.exec(ref) : null
99
+ const page = m && index.resolve(m[1])
100
+ return isCodeId(page?.id) ? `page:${page.id}${m[2] || ''}` : ref
101
+ }
102
+
103
+ /**
104
+ * A pull's form of a reference that names its page by a code: `page:<path>`, when that path
105
+ * names the same page back, else as stored. Any other reference — a path, a word id — is written
106
+ * as stored. The homepage is `page:/`.
107
+ *
108
+ * @param {string} ref
109
+ * @param {{ resolve: Function }} index - `pageRefIndex`
110
+ * @returns {string}
111
+ */
112
+ export function writtenPageRef(ref, index) {
113
+ const m = typeof ref === 'string' ? PAGE_REF.exec(ref) : null
114
+ const page = m && index.resolve(m[1])
115
+ if (!page || page.dynamic || m[1] !== page.id || !isCodeId(page.id)) return ref
116
+ const path = routeKey(page.route) || '/'
117
+ return index.resolve(path) === page ? `page:${path}${m[2] || ''}` : ref
118
+ }
119
+
120
+ /**
121
+ * Every page reference in a site's page and layout records, through `map`: each section's content
122
+ * in every language, its params and its subsections, and each page's `redirect` and child pages.
123
+ * Mutates the records.
124
+ *
125
+ * @param {{ pages?: Array<object>, layoutSections?: Array<object> }} records
126
+ * @param {(ref: string) => string} map - `storedPageRef` or `writtenPageRef`, bound to an index
127
+ */
128
+ export function mapPageRefs({ pages, layoutSections }, map) {
129
+ const section = (record) => {
130
+ if (!record || typeof record !== 'object') return
131
+ if (record.content && typeof record.content === 'object') mapContent(record.content, map)
132
+ if (record.params && typeof record.params === 'object') record.params = mapValue(record.params, map)
133
+ for (const child of record.$children || []) section(child)
134
+ }
135
+ const page = (record) => {
136
+ if (!record || typeof record !== 'object') return
137
+ if (typeof record.redirect === 'string') record.redirect = map(record.redirect)
138
+ for (const s of record.page_sections || []) section(s)
139
+ for (const child of record.$children || []) page(child)
140
+ }
141
+ for (const record of pages || []) page(record)
142
+ for (const record of layoutSections || []) section(record)
143
+ }
144
+
145
+ /** A section's content: one document, or one per language (`{ en: doc, fr: doc }`). */
146
+ function mapContent(content, map) {
147
+ if (content.type === 'doc') return mapDoc(content, map)
148
+ for (const doc of Object.values(content)) if (doc?.type === 'doc') mapDoc(doc, map)
149
+ }
150
+
151
+ /** Each `href` on a node or a mark in a rich-text document — a link, a button, a linked image. */
152
+ function mapDoc(node, map) {
153
+ if (!node || typeof node !== 'object') return
154
+ if (typeof node.attrs?.href === 'string') node.attrs.href = map(node.attrs.href)
155
+ for (const mark of node.marks || []) {
156
+ if (typeof mark?.attrs?.href === 'string') mark.attrs.href = map(mark.attrs.href)
157
+ }
158
+ if (Array.isArray(node.content)) for (const child of node.content) mapDoc(child, map)
159
+ }
160
+
161
+ /** A params value, with each string that is one `page:` reference through `map`. */
162
+ function mapValue(value, map) {
163
+ if (typeof value === 'string') return PAGE_REF.test(value) ? map(value) : value
164
+ if (Array.isArray(value)) return value.map((v) => mapValue(v, map))
165
+ if (value && typeof value === 'object') {
166
+ const out = {}
167
+ for (const [key, v] of Object.entries(value)) out[key] = mapValue(v, map)
168
+ return out
169
+ }
170
+ return value
171
+ }
@@ -37,6 +37,7 @@ import { foldOpenGraph } from './open-graph.js'
37
37
  import { isAuthoredPreview } from './authored-preview.js'
38
38
  import { join, relative, extname, basename, dirname } from 'node:path'
39
39
  import { restoreAssetRefs } from './asset-map.js'
40
+ import { mapPageRefs, pageRefIndex, pageRefPages, writtenPageRef } from './page-refs.js'
40
41
  import { siteFileKeys, siteTree, writtenRefs } from './located-media.js'
41
42
  import { readBackendState, updateBackendState } from './sync-store.js'
42
43
  import { servicesFromDocument, heldServices } from './services-request.js'
@@ -1380,6 +1381,10 @@ export function siteContentDocumentToProject({ document, siteRoot, backend = nul
1380
1381
  // abstract.
1381
1382
  const assetMap = backend ? readBackendState(siteRoot, backend).assets || {} : {}
1382
1383
  report.assets = restoreAssetRefs(document, assetMap)
1384
+ // ⭐ And each stored link to a page as the page's path (`page-refs.js`): what a file shows, where
1385
+ // the store keeps the id an editor links by.
1386
+ const refIndex = pageRefIndex(pageRefPages(document?.pages, sourceLocale, buildRouteOf))
1387
+ mapPageRefs({ pages: document?.pages, layoutSections: document?.layout_sections }, (ref) => writtenPageRef(ref, refIndex))
1383
1388
  // The keys among them that name a site file — a co-located file a push uploaded — for each
1384
1389
  // section's writer to make relative to its own file (`located-media.js`).
1385
1390
  const mediaKeys = siteFileKeys(assetMap)
package/src/uwx/site.js CHANGED
@@ -80,6 +80,7 @@ import { siteDefaultSectionType } from '../site/queries-config.js'
80
80
  import { resolveDefaultLocale, validateLanguageConfig, queryDataUrl } from '@uniweb/core'
81
81
  import { emitEntitySyncPackage } from './entity-document.js'
82
82
  import { locateSectionMedia, siteTree } from './located-media.js'
83
+ import { mapPageRefs, pageRefIndex, pageRefPages, storedPageRef } from './page-refs.js'
83
84
  import { loadLocaleTranslations, localizeScalar, localizeScalarList, localizeContentDoc, localesDir, isLocalizedContent, plainTextTargets } from './locale-sync.js'
84
85
  import { unwrapLocalized } from './backfill.js'
85
86
  import { loadFreeformTranslation } from '../i18n/freeform.js'
@@ -1659,6 +1660,11 @@ export async function siteProjectToDocument(siteRoot, opts = {}) {
1659
1660
  // field with two shapes is a store that cannot validate its own declaration.
1660
1661
  await localizeContentTree(pages, layoutSections, sourceLocale, targetLocales, translations, siteRoot)
1661
1662
 
1663
+ // ⭐ A link to a page is stored by the page's id where it has one (`page-refs.js`), so it
1664
+ // survives a rename in an editor, and a pull writes it back as the path a file shows.
1665
+ const refIndex = pageRefIndex(pageRefPages(pages, sourceLocale, buildRouteOf))
1666
+ mapPageRefs({ pages, layoutSections }, (ref) => storedPageRef(ref, refIndex))
1667
+
1662
1668
  // Collection DECLARATIONS — the merged collections.yml + site.yml::collections
1663
1669
  // config (the records themselves are separate entities; this is just the config).
1664
1670
  const colConfig = await resolveQueriesConfig(siteRoot, { siteYml })
@@ -685,5 +685,9 @@ export async function emitSyncPackages(siteRoot, opts = {}) {
685
685
  // The queries whose records the site's pages show live — what the `records` service
686
686
  // delivers on a published site (`shown-records.js`). Whether it changed or not.
687
687
  recordsShown: siteDoc ? recordQueriesShown(siteDoc) : [],
688
+ // Whether this push states the site's folder at all (`records.js::sendsFolder`). `false`
689
+ // leaves the backend's folder exactly as it is — no records directory, or one holding files
690
+ // that are not records — so a caller can say so where the backend still holds records.
691
+ sendsFolder: folder !== null,
688
692
  }
689
693
  }