Skip to content

Commit fc3e0f6

Browse files
committed
Extract pagesDirectory as separate model
It just parses its child nodes. Rendering of them are still handled by ContentModel index. Add basic fixture data so tests confirm this works.
1 parent aad2eb4 commit fc3e0f6

6 files changed

Lines changed: 105 additions & 20 deletions

File tree

src/compiler/contentModel/index.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
const { resolve } = require('path')
2-
const _ = require('lodash')
3-
const frontMatter = require('front-matter')
42
const ImmutableStack = require('../../lib/ImmutableStack')
5-
const { removeExtension, isTemplateFile } = require('../../lib/contentModelHelpers')
3+
const { removeExtension } = require('../../lib/contentModelHelpers')
64
const ContentModelEntryNode = require('../../lib/ContentModelEntryNode')
75
const matcha = require('../../lib/matcha')
86

97
const models = {
108
Homepage: require('./models/homepage'),
9+
PagesDirectory: require('./models/pagesDirectory'),
1110
Subpage: require('./models/subpage'),
1211
Collection: require('./models/collection'),
1312
Asset: require('./models/asset')
@@ -231,11 +230,11 @@ class ContentModel extends ContentModelEntryNode {
231230
nameOptions: [this.settings.pagesDirectory, 'subpages', 'pages']
232231
}),
233232

234-
asset: matcha.true(),
235-
236233
assetsDirectory: matcha.directory({
237234
nameOptions: [this.settings.assetsDirectory, 'assets']
238-
})
235+
}),
236+
237+
asset: matcha.true()
239238
}
240239
}
241240

@@ -246,6 +245,7 @@ class ContentModel extends ContentModelEntryNode {
246245
this.context,
247246
{ homepageDirectory: this.settings.homepageDirectory }
248247
),
248+
pagesDirectory: [],
249249
subpages: [],
250250
collections: [],
251251
assets: []
@@ -272,21 +272,14 @@ class ContentModel extends ContentModelEntryNode {
272272
}
273273

274274
if (this.matchers.pagesDirectory(node)) {
275-
return node.children.forEach(childNode => {
276-
if (this.matchers.subpage(childNode)) {
277-
tree.subpages.push(
278-
new models.Subpage(childNode, this.context, {
279-
pagesDirectory: this.settings.pagesDirectory
280-
})
281-
)
282-
} else if (this.matchers.asset(childNode)) {
283-
tree.assets.push(
284-
new models.Asset(childNode, this.context, {
285-
assetsDirectory: this.settings.assetsDirectory
286-
})
287-
)
288-
}
275+
tree.pagesDirectory = new models.PagesDirectory(node, this.context, {
276+
pagesDirectory: this.settings.pagesDirectory,
277+
assetsDirectory: this.settings.assetsDirectory,
278+
debug: this.settings.debug
289279
})
280+
tree.subpages.push(...tree.pagesDirectory.subtree.subpages)
281+
tree.assets.push(...tree.pagesDirectory.subtree.assets)
282+
return
290283
}
291284

292285
if (this.matchers.collection(node)) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const ContentModelEntryNode = require('../../../lib/ContentModelEntryNode')
2+
const matcha = require('../../../lib/matcha')
3+
4+
const models = {
5+
Subpage: require('./subpage'),
6+
Asset: require('./asset')
7+
}
8+
9+
const defaultSettings = {
10+
pagesDirectory: 'pages',
11+
assetsDirectory: 'assets',
12+
mode: 'start',
13+
debug: false
14+
}
15+
class PagesDirectory extends ContentModelEntryNode {
16+
static serialize(pagesDirectory) {
17+
return {
18+
subpages: pagesDirectory.subtree.subpages.map(models.Subpage.serialize),
19+
assets: pagesDirectory.subtree.assets.map(models.Asset.serialize)
20+
}
21+
}
22+
23+
constructor(fsNode, context, settings) {
24+
super(fsNode, context, settings)
25+
this.matchers = this.getSubtreeMatchers()
26+
this.subtree = this.parseSubtree()
27+
this.afterEffects()
28+
}
29+
30+
getSubtreeMatchers() {
31+
return {
32+
subpage: matcha.folderable({
33+
nameOptions: {
34+
index: ['page', 'index']
35+
}
36+
}),
37+
38+
asset: matcha.true()
39+
}
40+
}
41+
42+
parseSubtree() {
43+
const tree = {
44+
subpages: [],
45+
assets: []
46+
}
47+
48+
this.fsNode.children.forEach(childNode => {
49+
if (this.matchers.subpage(childNode)) {
50+
return tree.subpages.push(
51+
new models.Subpage(childNode, this.context, {
52+
pagesDirectory: this.settings.pagesDirectory
53+
})
54+
)
55+
}
56+
if (this.matchers.asset(childNode)) {
57+
return tree.assets.push(
58+
new models.Asset(childNode, this.context, {
59+
assetsDirectory: this.settings.assetsDirectory
60+
})
61+
)
62+
}
63+
})
64+
return tree
65+
}
66+
}
67+
68+
module.exports = PagesDirectory
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a page
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
template: CustomPage
3+
---
4+
A page there
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is another subpage in the pages directory

src/e2e-tests/magazine/fixtures/model.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,24 @@ const FIXTURE_CONTENT_MODEL = {
10801080
permalink: '/newsletter.html',
10811081
date: '2025-11-16',
10821082
content: 'Sign up now'
1083+
},
1084+
{
1085+
title: 'A page here',
1086+
permalink: '/a-page-here.html',
1087+
date: '2025-11-18',
1088+
content: 'This is a page'
1089+
},
1090+
{
1091+
title: 'A page there',
1092+
permalink: '/a-page-there.html',
1093+
date: '2025-11-20',
1094+
content: 'A page there'
1095+
},
1096+
{
1097+
title: 'And this one',
1098+
permalink: '/and-this-one',
1099+
date: '2025-11-22',
1100+
content: 'This is another subpage in the pages directory'
10831101
}
10841102
]
10851103
}

0 commit comments

Comments
 (0)