@uniweb/core 0.31.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 +4 -3
- package/src/block.js +40 -94
- package/src/fetch-config.js +3 -3
- package/src/index.js +3 -3
- package/src/insets.js +99 -0
- package/src/query-evaluation.js +7 -4
- package/src/route-match.js +14 -13
- package/src/scope.js +41 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniweb/core",
|
|
3
|
-
"version": "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/
|
|
51
|
-
"@uniweb/
|
|
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
|
-
//
|
|
95
|
-
//
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
//
|
|
100
|
-
//
|
|
101
|
-
|
|
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 =
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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.
|
|
215
|
-
const ref = lifted.
|
|
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/fetch-config.js
CHANGED
|
@@ -688,9 +688,9 @@ function bindWhere(where, variables) {
|
|
|
688
688
|
|
|
689
689
|
/**
|
|
690
690
|
* ⭐ `scope:` IS ITS OWN FIELD, on both lanes. The service takes it natively; on
|
|
691
|
-
* the compiled file the evaluators apply it to each record
|
|
692
|
-
* the folder `records/folder.yml` put it in) — `@uniweb/core`'s `applyScope`,
|
|
693
|
-
* build uses too. ⛔ Until 2026-09-11 it was folded here into
|
|
691
|
+
* the compiled file the evaluators apply it to the branch each record is held with
|
|
692
|
+
* (`$branch`, the folder `records/folder.yml` put it in) — `@uniweb/core`'s `applyScope`,
|
|
693
|
+
* the one the build uses too. ⛔ Until 2026-09-11 it was folded here into
|
|
694
694
|
* `where: { path: { under } }`, an authored form that is now retired in its
|
|
695
695
|
* favour [Diego]: a branch is a scope, and `where` stays the author's predicate.
|
|
696
696
|
*
|
package/src/index.js
CHANGED
|
@@ -55,9 +55,9 @@ export {
|
|
|
55
55
|
isDataUrl
|
|
56
56
|
} from './data-paths.js'
|
|
57
57
|
export { evaluate as evaluateWhere, match as matchWhere, whereOutsideLanguage } from './where.js'
|
|
58
|
-
// A query's `scope:` over records
|
|
59
|
-
// default fetcher and by `@uniweb/build
|
|
60
|
-
export { applyScope, withinScope } from './scope.js'
|
|
58
|
+
// A query's `scope:` over compiled records, which hold their branch under `BRANCH_KEY` —
|
|
59
|
+
// read by the runtime's default fetcher and by `@uniweb/build`, which writes it.
|
|
60
|
+
export { applyScope, withinScope, withoutBranch, BRANCH_KEY } from './scope.js'
|
|
61
61
|
// The one sort evaluator and the one href encoder — both read by `@uniweb/build`
|
|
62
62
|
// (materialization, the `route:` bake) and by `@uniweb/runtime` (the fallback),
|
|
63
63
|
// which is what keeps the static and live lanes answering a query identically.
|
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 — `{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
|
+
}
|
package/src/query-evaluation.js
CHANGED
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
26
|
import { match as matchWhere } from './where.js'
|
|
27
|
-
import { applyScope } from './scope.js'
|
|
27
|
+
import { applyScope, withoutBranch } from './scope.js'
|
|
28
28
|
import { sortRecords, parseSort } from './sort.js'
|
|
29
29
|
import { recordKeyValues } from './route-match.js'
|
|
30
30
|
|
|
@@ -42,7 +42,10 @@ import { recordKeyValues } from './route-match.js'
|
|
|
42
42
|
* @returns {Array<Object>|*} the records the config selects, in its order
|
|
43
43
|
*/
|
|
44
44
|
export function evaluateQuery(records, config, { locale = null, sort = sortRecords } = {}) {
|
|
45
|
-
if (!Array.isArray(records)
|
|
45
|
+
if (!Array.isArray(records)) return records
|
|
46
|
+
// ⛔ What this returns is an ANSWER, and no answer carries a record's branch: `scope` reads
|
|
47
|
+
// the one a compiled file holds, and it goes no further (`withoutBranch`, ruled 2026-09-27).
|
|
48
|
+
if (!config || typeof config !== 'object') return withoutBranch(records)
|
|
46
49
|
const order = (items, expr) => sort(items, expr, { locale })
|
|
47
50
|
|
|
48
51
|
let out = records
|
|
@@ -52,12 +55,12 @@ export function evaluateQuery(records, config, { locale = null, sort = sortRecor
|
|
|
52
55
|
if (isLimit(config.limit)) out = out.slice(0, config.limit)
|
|
53
56
|
|
|
54
57
|
const narrow = config.narrow
|
|
55
|
-
if (!narrow || typeof narrow !== 'object') return out
|
|
58
|
+
if (!narrow || typeof narrow !== 'object') return withoutBranch(out)
|
|
56
59
|
if (narrow.where) out = matchWhere(narrow.where, out)
|
|
57
60
|
if (narrow.match && typeof narrow.match === 'object') out = out.filter(matching(narrow.match))
|
|
58
61
|
if (narrow.sort) out = order(out, narrow.sort)
|
|
59
62
|
if (isLimit(narrow.limit)) out = out.slice(0, narrow.limit)
|
|
60
|
-
return out
|
|
63
|
+
return withoutBranch(out)
|
|
61
64
|
}
|
|
62
65
|
|
|
63
66
|
/**
|
package/src/route-match.js
CHANGED
|
@@ -228,8 +228,10 @@ export function splitPathCapture(capture) {
|
|
|
228
228
|
}
|
|
229
229
|
|
|
230
230
|
/**
|
|
231
|
-
* The inverse of `splitPathCapture` — a
|
|
232
|
-
* `
|
|
231
|
+
* The inverse of `splitPathCapture` — a capture from its directory and its last
|
|
232
|
+
* segment. `dir` may be empty. ⛔ Not a record's URL: a record carries no branch, so its
|
|
233
|
+
* own URL under `[...path]` is its handle alone (`fillRoutePattern`). Until 2026-09-27 this
|
|
234
|
+
* composed a record's placement and handle into one.
|
|
233
235
|
*
|
|
234
236
|
* @param {{ dir?: string|null, slug?: string|null }} parts
|
|
235
237
|
* @returns {string|null} null when there is no slug to name the record by
|
|
@@ -587,18 +589,17 @@ export function fillRoutePattern(pattern, values, { field = null } = {}) {
|
|
|
587
589
|
let tailHref = ''
|
|
588
590
|
const tail = pattern.match(CATCH_ALL)
|
|
589
591
|
if (tail) {
|
|
590
|
-
// A catch-all is filled
|
|
591
|
-
//
|
|
592
|
-
//
|
|
593
|
-
//
|
|
594
|
-
// `
|
|
592
|
+
// ⭐ A catch-all is filled with the record's handle ALONE — one segment, whatever it
|
|
593
|
+
// contains (a `/` inside it is a value). A record carries no branch [Diego,
|
|
594
|
+
// 2026-09-27], so its own URL under `[...path]` is the one it has under `[slug]`:
|
|
595
|
+
// `/logbook/<name>`. A URL that names a directory still reaches the page, and
|
|
596
|
+
// `scope: :dir` is what makes the directory count. ⛔ Until 2026-09-27 this prefixed the
|
|
597
|
+
// record's `path`, so one record was linked at `/logbook/field/river-survey` on a static
|
|
598
|
+
// site and at `/logbook/river-survey` where a records service, which answers no branch,
|
|
599
|
+
// served it.
|
|
595
600
|
const [handle] = routeParamValues(values, 'slug', field)
|
|
596
|
-
if (
|
|
597
|
-
|
|
598
|
-
const segments = dir.split('/').filter(Boolean).map((seg) => encodeURIComponent(seg))
|
|
599
|
-
// The handle is ONE segment whatever it contains: a `/` inside it is a value.
|
|
600
|
-
segments.push(encodeURIComponent(String(handle)))
|
|
601
|
-
tailHref = '/' + segments.join('/')
|
|
601
|
+
if (handle === undefined) return null
|
|
602
|
+
tailHref = '/' + encodeURIComponent(String(handle))
|
|
602
603
|
head = pattern.slice(0, tail.index)
|
|
603
604
|
}
|
|
604
605
|
const href = head.replace(new RegExp(`:(${PARAM_NAME})`, 'g'), (_, name) => {
|
package/src/scope.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* A query's `scope:` — the folder branch it reads — evaluated over records
|
|
3
|
-
*
|
|
2
|
+
* A query's `scope:` — the folder branch it reads — evaluated over compiled records,
|
|
3
|
+
* which hold their branch.
|
|
4
4
|
*
|
|
5
|
-
* A record
|
|
6
|
-
* query-processor.js`). A scope
|
|
7
|
-
*
|
|
8
|
-
* The root (`''`) holds everything.
|
|
5
|
+
* A compiled record holds the folder `records/folder.yml` placed it in under
|
|
6
|
+
* `$branch` (`BRANCH_KEY`, written by `build/src/site/query-processor.js`). A scope
|
|
7
|
+
* contains that folder and every folder below it, at SEGMENT boundaries: `field`
|
|
8
|
+
* holds `field` and `field/2025`, never `fieldwork`. The root (`''`) holds everything.
|
|
9
|
+
*
|
|
10
|
+
* ⛔ A RECORD DOES NOT CARRY ITS BRANCH (ruled 2026-09-27 [Diego]: *"branches are to
|
|
11
|
+
* organize the content pool … queries can filter by branch using `scope`"*). `$branch`
|
|
12
|
+
* is the compiled file's own key, there because the browser evaluates `scope` over that
|
|
13
|
+
* file. `evaluateQuery` reads it and answers no record with it (`withoutBranch`), just as
|
|
14
|
+
* a records service answers none. ⛔ Until then every compiled record carried `path`,
|
|
15
|
+
* which reached components, overwrote an authored field named `path`, and made a
|
|
16
|
+
* `[...path]` record's link `<branch>/<name>`.
|
|
9
17
|
*
|
|
10
18
|
* ⭐ Why this is its own field and not a `where` operator: a branch is a scope,
|
|
11
19
|
* and `where` stays the author's predicate over the records' own fields. Ruled
|
|
@@ -18,6 +26,31 @@
|
|
|
18
26
|
* Zero-dependency leaf.
|
|
19
27
|
*/
|
|
20
28
|
|
|
29
|
+
/**
|
|
30
|
+
* The key a compiled record holds its branch under. The static lane's store only; no
|
|
31
|
+
* answer carries it.
|
|
32
|
+
*/
|
|
33
|
+
export const BRANCH_KEY = '$branch'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Records as a query answers them: without the branch their compiled file holds. A list
|
|
37
|
+
* in which no record holds one is returned as it is.
|
|
38
|
+
*
|
|
39
|
+
* @param {Array<Object>} records
|
|
40
|
+
* @returns {Array<Object>}
|
|
41
|
+
*/
|
|
42
|
+
export function withoutBranch(records) {
|
|
43
|
+
if (!Array.isArray(records)) return records
|
|
44
|
+
if (!records.some((r) => r && typeof r === 'object' && Object.prototype.hasOwnProperty.call(r, BRANCH_KEY))) {
|
|
45
|
+
return records
|
|
46
|
+
}
|
|
47
|
+
return records.map((r) => {
|
|
48
|
+
if (!r || typeof r !== 'object' || !Object.prototype.hasOwnProperty.call(r, BRANCH_KEY)) return r
|
|
49
|
+
const { [BRANCH_KEY]: _branch, ...rest } = r
|
|
50
|
+
return rest
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
21
54
|
function trimSlashes(s) {
|
|
22
55
|
return s.replace(/^\/+/, '').replace(/\/+$/, '')
|
|
23
56
|
}
|
|
@@ -29,7 +62,7 @@ function trimSlashes(s) {
|
|
|
29
62
|
* `2024`, the classic prefix bug, which silently returns records from a sibling
|
|
30
63
|
* folder the author never named.
|
|
31
64
|
*
|
|
32
|
-
* @param {*} placement - a record's `
|
|
65
|
+
* @param {*} placement - a compiled record's branch (`BRANCH_KEY`)
|
|
33
66
|
* @param {string} scope
|
|
34
67
|
* @returns {boolean}
|
|
35
68
|
*/
|
|
@@ -52,5 +85,5 @@ export function withinScope(placement, scope) {
|
|
|
52
85
|
export function applyScope(records, scope) {
|
|
53
86
|
if (!Array.isArray(records)) return records
|
|
54
87
|
if (typeof scope !== 'string' || trimSlashes(scope) === '') return records
|
|
55
|
-
return records.filter((r) => r && typeof r === 'object' && withinScope(r
|
|
88
|
+
return records.filter((r) => r && typeof r === 'object' && withinScope(r[BRANCH_KEY], scope))
|
|
56
89
|
}
|