mikser-io-layouts 11.3.0 → 11.4.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.
@@ -23,6 +23,15 @@ on:
23
23
  jobs:
24
24
  publish:
25
25
  runs-on: ubuntu-latest
26
+ # A hung test must not cost six hours.
27
+ #
28
+ # Without this the job runs to GitHub's own limit, reporting nothing:
29
+ # a flaky teardown in one suite sat on `npm test` for seventeen
30
+ # minutes before anyone looked, and would have sat there all day.
31
+ # The longest suite in the family is a couple of minutes including
32
+ # install, so fifteen is generous and still fails while someone is
33
+ # still watching.
34
+ timeout-minutes: 15
26
35
  permissions:
27
36
  contents: read
28
37
  # The whole mechanism. Without it npm has nothing to verify.
package/README.md CHANGED
@@ -126,6 +126,33 @@ destination: '/cards/{{entity.name}}.html'
126
126
  ---
127
127
  ```
128
128
 
129
+ ### `match:` — a layout claiming its own entities
130
+
131
+ A layout can declare the pattern that selects it, instead of the config
132
+ naming it from the other side. These two are the same rule:
133
+
134
+ ```js
135
+ layouts({ match: { '@/blog/*': 'post-card' } })
136
+ ```
137
+
138
+ ```yaml
139
+ # layouts/post-card.html.hbs
140
+ ---
141
+ match: '@/blog/*' # or a list: ['@/blog/*', '@/news/*']
142
+ ---
143
+ ```
144
+
145
+ They are **peers**, not alternatives — an entity matched by a config pattern
146
+ and by a layout's own pattern gets both layouts, exactly as two config
147
+ patterns would. A document's own `meta.layout` still wins over both, and
148
+ auto-layout still only runs when nothing matched at all.
149
+
150
+ Patterns follow the same rule everywhere: `matchEntity` globs `entity.name`
151
+ only for a pattern starting `@/`; anything else globs `entity.id`, which
152
+ carries the `/<collection>/` prefix and the extension. A pattern that selects
153
+ nothing is reported once per run on a full cycle, naming the layout that
154
+ declared it.
155
+
129
156
  ```yaml
130
157
  # Pull from meta
131
158
  ---
package/lib/matching.js CHANGED
@@ -43,9 +43,25 @@ export function createOnProcessed({
43
43
  // author cannot see.
44
44
  const matchTally = { evaluated: 0, hit: new Set(), reported: false }
45
45
 
46
+ // Patterns a LAYOUT declared for itself in its own front-matter, seen
47
+ // during the run. Recorded here rather than derived at report time
48
+ // because by then the cycle's resolved-layout reads are gone, and a
49
+ // layout deleted mid-run should not be reported as a dud pattern.
50
+ const declaredSeen = new Map()
51
+
52
+ // A pattern's identity for the tally. The same glob can legitimately
53
+ // appear in config and in a layout's front-matter; they are different
54
+ // rules and either can be the dead one.
55
+ const tallyKey = (pattern, layoutName) =>
56
+ layoutName ? `layout:${layoutName}:${pattern}` : `config:${pattern}`
57
+
46
58
  function reportUnmatchedPatterns(logger) {
47
59
  if (matchTally.reported) return
48
- const patterns = Object.keys(options.match || {})
60
+ const patterns = [
61
+ ...Object.keys(options.match || {}).map(pattern =>
62
+ ({ pattern, layout: options.match[pattern], source: null })),
63
+ ...Array.from(declaredSeen.values()),
64
+ ]
49
65
  if (!patterns.length || !matchTally.evaluated) return
50
66
  // Only when the cycle evaluated everything. An incremental run
51
67
  // re-evaluates whatever changed, so a healthy pattern matches none of
@@ -56,8 +72,8 @@ export function createOnProcessed({
56
72
  if (!isFullCycle(runtime)) return
57
73
  matchTally.reported = true
58
74
 
59
- for (const pattern of patterns) {
60
- if (matchTally.hit.has(pattern)) continue
75
+ for (const { pattern, layout: layoutName, source } of patterns) {
76
+ if (matchTally.hit.has(tallyKey(pattern, source))) continue
61
77
  // Phrased as what was observed: an incremental cycle only
62
78
  // re-evaluates changed entities, so a pattern can legitimately
63
79
  // match nothing in a run of three. The count lets the reader
@@ -69,12 +85,17 @@ export function createOnProcessed({
69
85
  // So 'post' does NOT match a document named 'post'; '@/post' and
70
86
  // '/documents/post.md' both do.
71
87
  const onName = pattern.startsWith('@/')
88
+ // Where the pattern is written decides where the fix goes, and
89
+ // the two places look nothing alike. Without this the reader of
90
+ // a front-matter typo goes hunting through the config.
91
+ const declaredIn = source ? ` declared in layouts/${source}` : ''
72
92
  logger.warn(
73
- { code: 'layout-pattern-no-match', pattern, layout: options.match[pattern],
93
+ { code: 'layout-pattern-no-match', pattern, layout: layoutName,
94
+ declaredIn: source || 'layouts.match',
74
95
  evaluated: matchTally.evaluated, matchedAgainst: onName ? 'name' : 'id' },
75
- 'Layout pattern %j (→ %j) matched none of the %d entities evaluated. ' +
96
+ 'Layout pattern %j (→ %j)%s matched none of the %d entities evaluated. ' +
76
97
  'It is matched against entity.%s%s.',
77
- pattern, options.match[pattern], matchTally.evaluated,
98
+ pattern, layoutName, declaredIn, matchTally.evaluated,
78
99
  onName ? 'name' : 'id',
79
100
  onName ? '' : " — prefix a pattern with '@/' to match entity.name instead, " +
80
101
  'since an id looks like /<collection>/<name>.<ext>')
@@ -104,6 +125,42 @@ export function createOnProcessed({
104
125
  return (await findEntity({ id: stateEntry.id })) || stateEntry
105
126
  }
106
127
 
128
+ // `match:` in a layout's own front-matter — the colocated twin of a
129
+ // `layouts({ match: … })` rule, and documented alongside
130
+ // `destination:` since before either worked. `destination` was
131
+ // implemented; this was not, and a layout carrying it matched
132
+ // nothing, logged nothing, and looked configured. Reported after it
133
+ // cost a consumer the longest detour of their session.
134
+ //
135
+ // Read through resolveLayout for the same reason destination is
136
+ // (lib/assembly.js): the state map holds the file as synced, before
137
+ // front-matter has been lifted into meta. The raw entry has no
138
+ // `meta.match` to find.
139
+ //
140
+ // Built once per cycle rather than per entity: one findEntity per
141
+ // layout over a 5-20 entry map is nothing, the same work repeated
142
+ // for every document in the catalog is not.
143
+ let declaredPatterns = null
144
+ async function layoutDeclaredPatterns() {
145
+ if (declaredPatterns) return declaredPatterns
146
+ declaredPatterns = []
147
+ for (const name of Object.keys(layouts)) {
148
+ const declared = (await resolveLayout(name))?.meta?.match
149
+ if (!declared) continue
150
+ for (const pattern of Array.isArray(declared) ? declared : [declared]) {
151
+ if (typeof pattern !== 'string' || !pattern) {
152
+ logger.warn(
153
+ 'Layout %j declares a non-string `match` (%j) — expected a glob, or an array of them.',
154
+ name, pattern)
155
+ continue
156
+ }
157
+ declaredPatterns.push({ pattern, layout: name, source: name })
158
+ declaredSeen.set(tallyKey(pattern, name), { pattern, layout: name, source: name })
159
+ }
160
+ }
161
+ return declaredPatterns
162
+ }
163
+
107
164
  // Resolve the set of layouts that should render `entity` this
108
165
  // cycle. Returns an array — empty if none matched. Rules:
109
166
  // 1. meta.layout (string) and meta.layouts (array) can't both
@@ -153,7 +210,7 @@ export function createOnProcessed({
153
210
  matchTally.evaluated++
154
211
  for (const pattern in options.match || []) {
155
212
  if (matchEntity(entity, pattern)) {
156
- matchTally.hit.add(pattern)
213
+ matchTally.hit.add(tallyKey(pattern, null))
157
214
  const name = options.match[pattern]
158
215
  const layout = await resolveLayout(name)
159
216
  if (layout && !matched.find(l => l.name === layout.name)) {
@@ -168,6 +225,22 @@ export function createOnProcessed({
168
225
  }
169
226
  }
170
227
 
228
+ // Then the patterns each layout declared for itself. A peer of
229
+ // the config rules, not a fallback: both are "a pattern claimed
230
+ // this entity", and an entity legitimately matched by one of
231
+ // each gets both layouts, exactly as two config patterns would.
232
+ for (const { pattern, layout: name, source } of await layoutDeclaredPatterns()) {
233
+ if (!matchEntity(entity, pattern)) continue
234
+ matchTally.hit.add(tallyKey(pattern, source))
235
+ const layout = await resolveLayout(name)
236
+ if (layout && !matched.find(l => l.name === layout.name)) {
237
+ // Distinguishable from a config pattern in `--explain`:
238
+ // the same glob can be written in either place, and the
239
+ // fix goes to a different file depending on which.
240
+ matched.push({ ...layout, matchedBy: `${pattern} (layouts/${name})` })
241
+ }
242
+ }
243
+
171
244
  // Auto-layout: only as a fallback when no pattern matched.
172
245
  // The peel ladder is intentionally first-wins — it's a
173
246
  // most-specific-name search, not a multi-match.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mikser-io-layouts",
3
- "version": "11.3.0",
3
+ "version": "11.4.0",
4
4
  "description": "SSG-flavor task-production policy for mikser-io. Multi-match layout assignment, per-layout destinations, postprocess chain attachment, layout inspection.",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/test/unit.test.js CHANGED
@@ -210,6 +210,193 @@ describe('layouts plugin', () => {
210
210
  })
211
211
  })
212
212
 
213
+ it('a layout matches through `match:` in its own front-matter', async () => {
214
+ // Documented in the README beside `destination:` and never
215
+ // implemented. A layout carrying it produced no render task, logged
216
+ // nothing, and looked correctly configured — reported as the longest
217
+ // single detour of a consumer's session.
218
+ //
219
+ // The catalog entity is what carries parsed front-matter; the state
220
+ // map holds the file as synced. Seeding both is what a real build
221
+ // does, and is why reading the state map alone found no `match`.
222
+ await withTempWorking(async (workingFolder) => {
223
+ const catalog = []
224
+ const h = createHarness({
225
+ entities: catalog,
226
+ options: { workingFolder, outputFolder: path.join(workingFolder, 'out'), force: true },
227
+ })
228
+ layouts({ autoLayouts: false })(h.core)
229
+ await h.runHook('loaded')
230
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post-card.hbs' } })
231
+ catalog.push({
232
+ id: h.runtime.state.layouts.layouts['post-card'].id,
233
+ collection: 'layouts', name: 'post-card',
234
+ meta: { match: '@/blog/*' },
235
+ })
236
+
237
+ const doc = { id: '/documents/blog/welcome.md', collection: 'documents', name: 'blog/welcome', format: 'md', meta: {} }
238
+ h.journal.push({ id: 1, entity: doc, operation: 'create', context: {}, options: {}, output: null })
239
+ await h.runHook('processed', { aborted: false })
240
+
241
+ assert.deepEqual(doc.layouts.map(l => l.name), ['post-card'])
242
+ assert.match(doc.layouts[0].matchedBy, /layouts\/post-card/,
243
+ 'explain must say the rule came from the layout, not the config')
244
+ })
245
+ })
246
+
247
+ it('a layout can declare several patterns', async () => {
248
+ await withTempWorking(async (workingFolder) => {
249
+ const catalog = []
250
+ const h = createHarness({
251
+ entities: catalog,
252
+ options: { workingFolder, outputFolder: path.join(workingFolder, 'out'), force: true },
253
+ })
254
+ layouts({ autoLayouts: false })(h.core)
255
+ await h.runHook('loaded')
256
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'card.hbs' } })
257
+ catalog.push({
258
+ id: h.runtime.state.layouts.layouts['card'].id,
259
+ collection: 'layouts', name: 'card',
260
+ meta: { match: ['@/blog/*', '@/news/*'] },
261
+ })
262
+
263
+ const news = { id: '/documents/news/launch.md', collection: 'documents', name: 'news/launch', format: 'md', meta: {} }
264
+ h.journal.push({ id: 1, entity: news, operation: 'create', context: {}, options: {}, output: null })
265
+ await h.runHook('processed', { aborted: false })
266
+
267
+ assert.deepEqual(news.layouts.map(l => l.name), ['card'])
268
+ })
269
+ })
270
+
271
+ it('a front-matter pattern that selected nothing warns, naming the layout', async () => {
272
+ // The silence is the whole finding: without this the author of a
273
+ // typo'd front-matter pattern gets a green build and empty pages.
274
+ // Naming the layout matters because the same glob can be written in
275
+ // the config, and the fix lives in a different file.
276
+ await withTempWorking(async (workingFolder) => {
277
+ const catalog = []
278
+ const h = createHarness({
279
+ entities: catalog,
280
+ options: { workingFolder, outputFolder: path.join(workingFolder, 'out'), force: true },
281
+ })
282
+ layouts({ autoLayouts: false })(h.core)
283
+ await h.runHook('loaded')
284
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post-card.hbs' } })
285
+ catalog.push({
286
+ id: h.runtime.state.layouts.layouts['post-card'].id,
287
+ collection: 'layouts', name: 'post-card',
288
+ meta: { match: '@/blogg/*' }, // typo
289
+ })
290
+
291
+ const doc = { id: '/documents/blog/welcome.md', collection: 'documents', name: 'blog/welcome', format: 'md', meta: {} }
292
+ h.journal.push({ id: 1, entity: doc, operation: 'create', context: {}, options: {}, output: null })
293
+ await h.runHook('processed', { aborted: false })
294
+
295
+ const warnings = h.logs.filter(l =>
296
+ l.level === 'warn' && l.args.join(' ').includes('Layout pattern'))
297
+ assert.equal(warnings.length, 1, `expected one warning, got ${warnings.length}`)
298
+ const [fields, ...message] = warnings[0].args
299
+ assert.equal(fields.code, 'layout-pattern-no-match')
300
+ assert.equal(fields.pattern, '@/blogg/*')
301
+ assert.equal(fields.declaredIn, 'post-card', 'says which layout wrote the pattern')
302
+ assert.match(format(...message), /layouts\/post-card/,
303
+ 'the sentence points at the file to edit')
304
+ })
305
+ })
306
+
307
+ it('reports the one dud front-matter pattern among working ones', async () => {
308
+ // The shape this actually takes in a project: several layouts each
309
+ // claiming their own section, one of them typo'd. A working pattern
310
+ // elsewhere must not absolve the dead one — that would make the
311
+ // warning useless in exactly the configuration it is for.
312
+ await withTempWorking(async (workingFolder) => {
313
+ const catalog = []
314
+ const h = createHarness({
315
+ entities: catalog,
316
+ options: { workingFolder, outputFolder: path.join(workingFolder, 'out'), force: true },
317
+ })
318
+ layouts({ autoLayouts: false })(h.core)
319
+ await h.runHook('loaded')
320
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post-card.hbs' } })
321
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'news-card.hbs' } })
322
+ catalog.push({
323
+ id: h.runtime.state.layouts.layouts['post-card'].id,
324
+ collection: 'layouts', name: 'post-card',
325
+ meta: { match: '@/blog/*' }, // works
326
+ })
327
+ catalog.push({
328
+ id: h.runtime.state.layouts.layouts['news-card'].id,
329
+ collection: 'layouts', name: 'news-card',
330
+ meta: { match: '@/newss/*' }, // typo
331
+ })
332
+
333
+ const doc = { id: '/documents/blog/welcome.md', collection: 'documents', name: 'blog/welcome', format: 'md', meta: {} }
334
+ h.journal.push({ id: 1, entity: doc, operation: 'create', context: {}, options: {}, output: null })
335
+ await h.runHook('processed', { aborted: false })
336
+
337
+ assert.deepEqual(doc.layouts.map(l => l.name), ['post-card'], 'the good pattern still matched')
338
+ const warnings = h.logs.filter(l =>
339
+ l.level === 'warn' && l.args.join(' ').includes('Layout pattern'))
340
+ assert.equal(warnings.length, 1, `expected exactly one warning, got ${warnings.length}`)
341
+ assert.equal(warnings[0].args[0].declaredIn, 'news-card', 'names the dud, not the working one')
342
+ })
343
+ })
344
+
345
+ it('a front-matter pattern is a peer of a config pattern, not a fallback', async () => {
346
+ // Two rules claiming one entity give two layouts, exactly as two
347
+ // config patterns would. Making front-matter a fallback would mean
348
+ // adding a config rule silently switched a layout off.
349
+ await withTempWorking(async (workingFolder) => {
350
+ const catalog = []
351
+ const h = createHarness({
352
+ entities: catalog,
353
+ options: { workingFolder, outputFolder: path.join(workingFolder, 'out'), force: true },
354
+ })
355
+ layouts({ autoLayouts: false, match: { '@/blog/*': 'post' } })(h.core)
356
+ await h.runHook('loaded')
357
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post.hbs' } })
358
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post-card.hbs' } })
359
+ catalog.push({
360
+ id: h.runtime.state.layouts.layouts['post-card'].id,
361
+ collection: 'layouts', name: 'post-card',
362
+ meta: { match: '@/blog/*' },
363
+ })
364
+
365
+ const doc = { id: '/documents/blog/welcome.md', collection: 'documents', name: 'blog/welcome', format: 'md', meta: {} }
366
+ h.journal.push({ id: 1, entity: doc, operation: 'create', context: {}, options: {}, output: null })
367
+ await h.runHook('processed', { aborted: false })
368
+
369
+ assert.deepEqual(doc.layouts.map(l => l.name).sort(), ['post', 'post-card'])
370
+ })
371
+ })
372
+
373
+ it('an author-declared meta.layout still wins over a front-matter pattern', async () => {
374
+ // Precedence is unchanged: what the document asks for beats what a
375
+ // layout claims, the same way it beats a config pattern.
376
+ await withTempWorking(async (workingFolder) => {
377
+ const catalog = []
378
+ const h = createHarness({
379
+ entities: catalog,
380
+ options: { workingFolder, outputFolder: path.join(workingFolder, 'out'), force: true },
381
+ })
382
+ layouts({ autoLayouts: false })(h.core)
383
+ await h.runHook('loaded')
384
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post.hbs' } })
385
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post-card.hbs' } })
386
+ catalog.push({
387
+ id: h.runtime.state.layouts.layouts['post-card'].id,
388
+ collection: 'layouts', name: 'post-card',
389
+ meta: { match: '@/blog/*' },
390
+ })
391
+
392
+ const doc = { id: '/documents/blog/welcome.md', collection: 'documents', name: 'blog/welcome', format: 'md', meta: { layout: 'post' } }
393
+ h.journal.push({ id: 1, entity: doc, operation: 'create', context: {}, options: {}, output: null })
394
+ await h.runHook('processed', { aborted: false })
395
+
396
+ assert.deepEqual(doc.layouts.map(l => l.name), ['post'])
397
+ })
398
+ })
399
+
213
400
  it('stays silent about an unmatched pattern on an incremental cycle', async () => {
214
401
  // The inverse of the test above, and the reason the gate exists. An
215
402
  // incremental run re-evaluates only what changed, so a pattern that