@uniweb/core 0.32.0 → 0.32.1

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": "0.32.0",
3
+ "version": "0.32.1",
4
4
  "description": "Core classes for the Uniweb platform - Uniweb, Website, Page, Block",
5
5
  "type": "module",
6
6
  "exports": {
@@ -15,6 +15,7 @@
15
15
  "./layout-name": "./src/layout-name.js",
16
16
  "./page-data": "./src/page-data.js",
17
17
  "./icon-corpus": "./src/icon-corpus.js",
18
+ "./insets": "./src/insets.js",
18
19
  "./locale-config": "./src/locale-config.js",
19
20
  "./route-match": "./src/route-match.js",
20
21
  "./section-id": "./src/section-id.js",
@@ -47,8 +48,8 @@
47
48
  "vitest": "^4.1.7"
48
49
  },
49
50
  "dependencies": {
50
- "@uniweb/theming": "^0.1.15",
51
- "@uniweb/semantic-parser": "^1.4.1"
51
+ "@uniweb/semantic-parser": "^1.4.1",
52
+ "@uniweb/theming": "^0.1.15"
52
53
  },
53
54
  "scripts": {
54
55
  "test": "vitest run"
package/src/block.js CHANGED
@@ -11,70 +11,7 @@ import {
11
11
  } from '@uniweb/semantic-parser'
12
12
  import { normalizeTokenValue } from '@uniweb/theming'
13
13
  import { sectionDomId } from './section-id.js'
14
-
15
- /**
16
- * Lift container fences out of a content document.
17
- *
18
- * A ` ```@Component{params} ` fence parses to an `inset_block` node carrying a
19
- * body of real block content. This rewrites each one to the `inset_placeholder`
20
- * a leaf inset leaves behind, so a container resolves through exactly the same
21
- * path: `getInset(refId)` → a Block → the foundation's component. Kit and the
22
- * SSR renderer already handle placeholders, so neither needs to know containers
23
- * exist.
24
- *
25
- * PURE with respect to the input. `blockData.content` is shared with the sync /
26
- * pull machinery, which must keep seeing `inset_block` — that is the canonical
27
- * stored shape. Nodes on the path to a container are cloned; everything else is
28
- * passed through by reference, so a document with no containers costs one array
29
- * walk and allocates nothing.
30
- *
31
- * Containers are NOT recursed into here. A container's body becomes its child
32
- * Block's content, and that Block's constructor lifts its own containers — so
33
- * nesting resolves one level at a time, each at the level that owns it.
34
- *
35
- * @param {Object} content - ProseMirror document (never mutated)
36
- * @returns {{ content: Object, refs: Array<{refId, type, params, content}> }}
37
- */
38
- function liftContainers(content) {
39
- if (!content || !Array.isArray(content.content)) return { content, refs: [] }
40
-
41
- const refs = []
42
-
43
- const visit = (nodes) => {
44
- let changed = false
45
- const out = nodes.map((node) => {
46
- if (!node) return node
47
-
48
- if (node.type === 'inset_block') {
49
- const { component, ...params } = node.attrs || {}
50
- const refId = `container_${refs.length}`
51
- refs.push({
52
- refId,
53
- type: component,
54
- params,
55
- content: { type: 'doc', content: node.content || [] },
56
- })
57
- changed = true
58
- return { type: 'inset_placeholder', attrs: { refId, embedKind: 'block' } }
59
- }
60
-
61
- if (Array.isArray(node.content)) {
62
- const inner = visit(node.content)
63
- if (inner !== node.content) {
64
- changed = true
65
- return { ...node, content: inner }
66
- }
67
- }
68
- return node
69
- })
70
- return changed ? out : nodes
71
- }
72
-
73
- const next = visit(content.content)
74
- return next === content.content
75
- ? { content, refs }
76
- : { content: { ...content, content: next }, refs }
77
- }
14
+ import { liftInsets } from './insets.js'
78
15
 
79
16
  export default class Block {
80
17
  constructor(blockData, id, page) {
@@ -91,14 +28,16 @@ export default class Block {
91
28
  // 2. Pre-parsed content with main/items structure
92
29
  // For now, store raw and parse on demand
93
30
  //
94
- // Container fences (```@Component around a body) arrive as `inset_block`
95
- // nodes and are lifted out HERE, into the same placeholder + refId shape
96
- // the build gives leaf insets. Doing it at render-graph construction
97
- // rather than at build time is deliberate: `inset_block` is the canonical
98
- // STORED shape, so the content that syncs and round-trips must keep
99
- // carrying it. `blockData.content` is left untouched — the lift produces a
100
- // new tree and only this Block's view of it changes.
101
- const lifted = liftContainers(blockData.content)
31
+ // Insets arrive as the author wrote them — `inset_ref` for a leaf, `inset_block`
32
+ // for a ```@Component fence — and are lifted out HERE into placeholders plus
33
+ // inset Blocks (`liftInsets`). Doing it at render-graph construction rather than
34
+ // at build time is deliberate: what the author wrote is the canonical STORED
35
+ // shape, so the content that syncs and round-trips keeps carrying it.
36
+ // `blockData.content` is left untouched — the lift produces a new tree and only
37
+ // this Block's view of it changes. Leaves are numbered after a stored `insets[]`
38
+ // (content from before the lift moved here), so the two cannot share a refId.
39
+ const storedInsets = Array.isArray(blockData.insets) ? blockData.insets : []
40
+ const lifted = liftInsets(blockData.content, storedInsets.length)
102
41
  this.rawContent = lifted.content || {}
103
42
  this.parsedContent = this.parseContent(lifted.content)
104
43
 
@@ -182,26 +121,26 @@ export default class Block {
182
121
  ? blockData.subsections.map((block, i) => new Block(block, `${id}_${i}`, this.page))
183
122
  : []
184
123
 
185
- // Insets — inline @-referenced components positioned in content flow
124
+ // Insets — inline @-referenced components positioned in content flow: a stored
125
+ // `insets[]` first, then the leaves lifted from the content, in document order.
126
+ // Each receives its `[…]` text as `content.title` and its `{…}` as params.
186
127
  this.insets = []
187
- const insetData = blockData.insets
188
- if (insetData?.length > 0) {
189
- for (let i = 0; i < insetData.length; i++) {
190
- const ref = insetData[i]
191
- const title = ref.title || ''
192
- const child = new Block(
193
- {
194
- type: ref.type,
195
- params: ref.params || {},
196
- content: { title },
197
- stableId: ref.refId,
198
- refId: ref.refId,
199
- },
200
- `${id}_inset_${i}`,
201
- this.page
202
- )
203
- this.insets.push(child)
204
- }
128
+ const insetData = [...storedInsets, ...lifted.leaves]
129
+ for (let i = 0; i < insetData.length; i++) {
130
+ const ref = insetData[i]
131
+ const title = ref.title || ''
132
+ const child = new Block(
133
+ {
134
+ type: ref.type,
135
+ params: ref.params || {},
136
+ content: { title },
137
+ stableId: ref.refId,
138
+ refId: ref.refId,
139
+ },
140
+ `${id}_inset_${i}`,
141
+ this.page
142
+ )
143
+ this.insets.push(child)
205
144
  }
206
145
 
207
146
  // Containers, appended AFTER the leaf insets so `block.insets[0]` keeps
@@ -210,9 +149,9 @@ export default class Block {
210
149
  // content, so the foundation's component receives a fully parsed
211
150
  // `content` — title, paragraphs, items, sequence — exactly as a section
212
151
  // does. Nested containers resolve for free: the child Block runs this
213
- // same constructor over its own body.
214
- for (let i = 0; i < lifted.refs.length; i++) {
215
- const ref = lifted.refs[i]
152
+ // same constructor over its own body, leaf insets included.
153
+ for (let i = 0; i < lifted.containers.length; i++) {
154
+ const ref = lifted.containers[i]
216
155
  this.insets.push(
217
156
  new Block(
218
157
  {
@@ -232,6 +171,13 @@ export default class Block {
232
171
  // Supports local files (path) or remote URLs (url)
233
172
  this.fetch = blockData.fetch || null
234
173
 
174
+ // The layout the author chose for this section's child sections — the reserved
175
+ // `grid:` section key [Diego, 2026-09-27]: `3` (equal columns) or `'40/60'`
176
+ // (relative widths). Carried as written; kit's `ChildGrid` lays the children out
177
+ // from it (`@uniweb/schemas/grid`). A section key, never a param: a component
178
+ // declares the layouts it offers in `meta.js` `children.grid`.
179
+ this.grid = blockData.grid ?? null
180
+
235
181
  // Data loading state — set by BlockRenderer when a runtime fetch is in progress
236
182
  // Components check this to show loading UI (spinners, skeletons)
237
183
  this.dataLoading = false
package/src/insets.js ADDED
@@ -0,0 +1,99 @@
1
+ /**
2
+ * Insets — lifted out of a content document at render-graph construction.
3
+ *
4
+ * A leaf module so a tool can show exactly what a component receives without building
5
+ * a Block: `uniweb inspect` imports it. `Block`'s constructor is the one runtime caller.
6
+ */
7
+
8
+ /**
9
+ * Lift the insets out of a content document — both forms, as the author wrote them.
10
+ *
11
+ * - A LEAF inset — `![alt](@Component){params}`, `[text](@Component)`, and the
12
+ * `[@key]` / `[#id]` shorthands — parses to an `inset_ref` node. It becomes an
13
+ * `inset_placeholder` in place, inline where it was inline, and a leaf ref:
14
+ * the component, its params, and the `[…]` text as the inset's title.
15
+ * - A CONTAINER — a ` ```@Component{params} ` fence — parses to an `inset_block`
16
+ * node carrying a body of real block content. It becomes a placeholder too, and
17
+ * its body becomes the child Block's content.
18
+ *
19
+ * Both then resolve through one path: `getInset(refId)` → a Block → the
20
+ * foundation's component. Kit and the SSR renderer handle placeholders, so neither
21
+ * needs to know which form a placeholder came from.
22
+ *
23
+ * ⭐ Doing it HERE, at render-graph construction, is the point: what the author wrote
24
+ * is the stored shape. `blockData.content` is shared with the sync / pull machinery
25
+ * and an editor, which keep seeing `inset_ref` and `inset_block`, and a document that
26
+ * never went through a build — a record body, a free-form translation — resolves its
27
+ * insets the same way. ⛔ *Until 2026-09-27 only containers were lifted here; leaf
28
+ * insets were extracted by the site build into the section's `insets[]`, so any
29
+ * document the build did not extract rendered none of them.* A stored `insets[]` is
30
+ * still read (the constructor below), and `leafStart` numbers the lifted leaves after
31
+ * it so their refIds cannot collide.
32
+ *
33
+ * PURE with respect to the input: nodes on the path to an inset are cloned; everything
34
+ * else is passed through by reference, so a document with no insets costs one walk and
35
+ * allocates nothing.
36
+ *
37
+ * Containers are NOT recursed into here — neither for containers nor for leaves. A
38
+ * container's body becomes its child Block's content, and that Block's constructor
39
+ * lifts its own — so nesting resolves one level at a time, each at the level that owns
40
+ * it.
41
+ *
42
+ * @param {Object} content - ProseMirror document (never mutated)
43
+ * @param {number} [leafStart=0] - the first leaf refId number (`inset_<leafStart>`)
44
+ * @returns {{
45
+ * content: Object,
46
+ * leaves: Array<{refId, type, params, title, embedKind}>,
47
+ * containers: Array<{refId, type, params, content}>,
48
+ * }}
49
+ */
50
+ export function liftInsets(content, leafStart = 0) {
51
+ if (!content || !Array.isArray(content.content)) return { content, leaves: [], containers: [] }
52
+
53
+ const leaves = []
54
+ const containers = []
55
+
56
+ const visit = (nodes) => {
57
+ let changed = false
58
+ const out = nodes.map((node) => {
59
+ if (!node) return node
60
+
61
+ if (node.type === 'inset_ref') {
62
+ const { component, alt, embedKind, ...params } = node.attrs || {}
63
+ const refId = `inset_${leafStart + leaves.length}`
64
+ const kind = embedKind || 'visual'
65
+ leaves.push({ refId, type: component, params, title: alt || null, embedKind: kind })
66
+ changed = true
67
+ return { type: 'inset_placeholder', attrs: { refId, embedKind: kind } }
68
+ }
69
+
70
+ if (node.type === 'inset_block') {
71
+ const { component, ...params } = node.attrs || {}
72
+ const refId = `container_${containers.length}`
73
+ containers.push({
74
+ refId,
75
+ type: component,
76
+ params,
77
+ content: { type: 'doc', content: node.content || [] },
78
+ })
79
+ changed = true
80
+ return { type: 'inset_placeholder', attrs: { refId, embedKind: 'block' } }
81
+ }
82
+
83
+ if (Array.isArray(node.content)) {
84
+ const inner = visit(node.content)
85
+ if (inner !== node.content) {
86
+ changed = true
87
+ return { ...node, content: inner }
88
+ }
89
+ }
90
+ return node
91
+ })
92
+ return changed ? out : nodes
93
+ }
94
+
95
+ const next = visit(content.content)
96
+ return next === content.content
97
+ ? { content, leaves, containers }
98
+ : { content: { ...content, content: next }, leaves, containers }
99
+ }