mikser-io-layouts 11.4.0 → 11.5.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/lib/matching.js CHANGED
@@ -22,7 +22,22 @@ import path from 'node:path'
22
22
 
23
23
  import _ from 'lodash'
24
24
  import { primaryDestination } from './destination.js'
25
- import { isFullCycle } from 'mikser-io'
25
+ import { isFullCycle, readFrontMatter } from 'mikser-io'
26
+
27
+ // A layout as synced, with its front-matter block lifted off — attributes
28
+ // onto meta so `destination:` and `match:` still work, body as the template.
29
+ // Returns the entry untouched when there is no front matter, and when the
30
+ // block cannot be parsed: a renderer showing the author their own malformed
31
+ // YAML is more use than one silently dropping it.
32
+ function withoutFrontMatter(entry) {
33
+ const parsed = readFrontMatter(entry?.content)
34
+ if (!parsed || parsed.error) return entry
35
+ return {
36
+ ...entry,
37
+ meta: { ...(entry.meta ?? {}), ...parsed.attributes },
38
+ content: parsed.body,
39
+ }
40
+ }
26
41
 
27
42
  export function createOnProcessed({
28
43
  runtime, useLogger, useJournal,
@@ -122,7 +137,21 @@ export function createOnProcessed({
122
137
  async function resolveLayout(name) {
123
138
  const stateEntry = layouts[name]
124
139
  if (!stateEntry) return undefined
125
- return (await findEntity({ id: stateEntry.id })) || stateEntry
140
+ const fromCatalog = await findEntity({ id: stateEntry.id })
141
+ if (fromCatalog) return fromCatalog
142
+ // The catalog has not caught up — a sync race, or a setup where
143
+ // the front-matter plugin has not run. The state map holds the
144
+ // file AS SYNCED, so its content still carries the front-matter
145
+ // block, and handing that to a renderer has now failed twice in
146
+ // two different ways: Handlebars emitted the YAML into the page,
147
+ // and LiquidJS refuses to parse it at all, reporting a syntax
148
+ // error at the front-matter line and taking the layout down.
149
+ //
150
+ // Resolving from the catalog was the fix for the first. This is
151
+ // the same fix for the path that bypasses it — stripping through
152
+ // core's reader rather than a regex here, so there is one
153
+ // implementation of what front matter is.
154
+ return withoutFrontMatter(stateEntry)
126
155
  }
127
156
 
128
157
  // `match:` in a layout's own front-matter — the colocated twin of a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mikser-io-layouts",
3
- "version": "11.4.0",
3
+ "version": "11.5.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",
@@ -18,7 +18,7 @@
18
18
  },
19
19
  "homepage": "https://github.com/almero-digital-marketing/mikser-io-layouts#readme",
20
20
  "peerDependencies": {
21
- "mikser-io": "^11.1.0",
21
+ "mikser-io": "^11.10.0",
22
22
  "zod": "^4.0.0"
23
23
  },
24
24
  "dependencies": {
@@ -27,7 +27,7 @@
27
27
  "lodash": "^4.17.0"
28
28
  },
29
29
  "devDependencies": {
30
- "mikser-io": "^11.1.0",
30
+ "mikser-io": "^11.10.0",
31
31
  "mikser-io-render-liquid": "^11.0.1",
32
32
  "zod": "^4.0.0"
33
33
  }
package/test/unit.test.js CHANGED
@@ -210,6 +210,81 @@ describe('layouts plugin', () => {
210
210
  })
211
211
  })
212
212
 
213
+ it('never hands a renderer a layout with its front matter still attached', async () => {
214
+ // resolveLayout falls back to the state map when the catalog has not
215
+ // caught up, and the state map holds the file AS SYNCED. That raw
216
+ // text has failed twice, in two engines: Handlebars emitted the YAML
217
+ // into the page, and LiquidJS refuses to parse it at all — reported
218
+ // as "invalid range syntax" at line 2, the front-matter line, which
219
+ // takes the whole layout down.
220
+ //
221
+ // The catalog is deliberately EMPTY here, which is the fallback.
222
+ await withTempWorking(async (workingFolder) => {
223
+ const h = createHarness({
224
+ entities: [],
225
+ options: { workingFolder, outputFolder: path.join(workingFolder, 'out'), force: true },
226
+ })
227
+ layouts({ autoLayouts: false })(h.core)
228
+ await h.runHook('loaded')
229
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post.hbs' } })
230
+ // The file as synced: front matter still on it.
231
+ h.runtime.state.layouts.layouts['post'].content =
232
+ '---\ndestination: "/{{ entity.name }}.html"\n---\n<h1>body</h1>\n'
233
+
234
+ const doc = { id: '/documents/hello.md', collection: 'documents', name: 'hello', format: 'md', meta: { layout: 'post' } }
235
+ h.journal.push({ id: 1, entity: doc, operation: 'create', context: {}, options: {}, output: null })
236
+ await h.runHook('processed', { aborted: false })
237
+
238
+ assert.ok(!doc.layout.content.trimStart().startsWith('---'),
239
+ `front matter reached the renderer: ${JSON.stringify(doc.layout.content.slice(0, 40))}`)
240
+ assert.match(doc.layout.content, /<h1>body<\/h1>/, 'the template survived')
241
+ })
242
+ })
243
+
244
+ it('lifts the fallback layout\'s front matter onto meta, so destination still works', async () => {
245
+ // Stripping without lifting would trade a parse error for a silently
246
+ // ignored `destination:` — the state-map entry carries no meta at all
247
+ // otherwise, which is why a front-matter `match:` saw nothing there.
248
+ await withTempWorking(async (workingFolder) => {
249
+ const h = createHarness({
250
+ entities: [],
251
+ options: { workingFolder, outputFolder: path.join(workingFolder, 'out'), force: true },
252
+ })
253
+ layouts({ autoLayouts: false })(h.core)
254
+ await h.runHook('loaded')
255
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post.hbs' } })
256
+ h.runtime.state.layouts.layouts['post'].content =
257
+ '---\ndestination: "/cards/{{entity.name}}.html"\n---\n<h1>body</h1>\n'
258
+
259
+ const doc = { id: '/documents/hello.md', collection: 'documents', name: 'hello', format: 'md', meta: { layout: 'post' } }
260
+ h.journal.push({ id: 1, entity: doc, operation: 'create', context: {}, options: {}, output: null })
261
+ await h.runHook('processed', { aborted: false })
262
+
263
+ assert.equal(doc.layout.meta.destination, '/cards/{{entity.name}}.html')
264
+ })
265
+ })
266
+
267
+ it('leaves a malformed front-matter block alone rather than dropping it', async () => {
268
+ // Unparseable YAML shown to the author beats silently discarded.
269
+ await withTempWorking(async (workingFolder) => {
270
+ const h = createHarness({
271
+ entities: [],
272
+ options: { workingFolder, outputFolder: path.join(workingFolder, 'out'), force: true },
273
+ })
274
+ layouts({ autoLayouts: false })(h.core)
275
+ await h.runHook('loaded')
276
+ await h.runSync('layouts', { action: 'create', context: { relativePath: 'post.hbs' } })
277
+ const broken = "---\ndestination: '/{{ before (after entity.name '/') '_' }}'\n---\n<h1>body</h1>\n"
278
+ h.runtime.state.layouts.layouts['post'].content = broken
279
+
280
+ const doc = { id: '/documents/hello.md', collection: 'documents', name: 'hello', format: 'md', meta: { layout: 'post' } }
281
+ h.journal.push({ id: 1, entity: doc, operation: 'create', context: {}, options: {}, output: null })
282
+ await h.runHook('processed', { aborted: false })
283
+
284
+ assert.equal(doc.layout.content, broken, 'left as written')
285
+ })
286
+ })
287
+
213
288
  it('a layout matches through `match:` in its own front-matter', async () => {
214
289
  // Documented in the README beside `destination:` and never
215
290
  // implemented. A layout carrying it produced no render task, logged