Skip to content

Commit 99f3e92

Browse files
committed
Automate childNode matching and parsing in ContentModelEntryNode
Introduce a getSubtreeConfig method, in which all necessities of matching childNodes and building subtree out of them are defined. For each key in subtree, a definition like this is added to the config array: { matcher: matcha; key?: string; settings?: object; model?: ContentModelNode constructor; singular?: boolean (false); sideEffect?: function; } Then parseSubtree template method is actually implemented in the ContentModelEntryNode. It loops over childNodes and subtree configs. For everything to look and feel smooth some additional changes are made: - ContentModel does not pass a contentType property to collection any more. Instead, collection itself computes it based on the settings.contentTypes in its getSchema method - Compiler passes the { children: fileSystemTree } to the contentModel - static draftCheck is now a class method of ContentModelEntryNode - A getChildContext template method is added for models to be able to customize the context they pass to their childNodes. E.g. ContentModel, AssetsDirectory, PagesDirectory
1 parent e57e508 commit 99f3e92

12 files changed

Lines changed: 384 additions & 489 deletions

File tree

src/compiler/contentModel/index.js

Lines changed: 97 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -172,159 +172,134 @@ class ContentModel extends ContentModelEntryNode {
172172

173173
this.contentTypes = contentTypes
174174

175-
const collectionAliasesFromContentTypes = contentTypes
176-
.filter(ct => ct.model === 'collection')
177-
.map(ct => ct.collectionAlias)
178-
179-
const collectionAliasesFromFrontMatter = this.collectionAliases || []
175+
this.subtreeConfig = this.getSubtreeConfig()
180176

181-
this.collectionAliases = [
182-
...collectionAliasesFromContentTypes,
183-
...collectionAliasesFromFrontMatter
184-
]
177+
this.subtree = this.parseSubtree({
178+
homepage: new models.Homepage(
179+
{ name: 'index', extension: 'md', content: '' },
180+
this.context,
181+
{ homepageDirectory: this.settings.homepageDirectory }
182+
),
183+
pagesDirectory: [],
184+
subpages: [],
185+
collections: [],
186+
assets: []
187+
})
185188

186-
this.matchers = this.getSubtreeMatchers()
187-
this.subtree = this.parseSubtree()
188189
this.afterEffects()
189190
}
190191

191192
getIndexFile() {
192-
return this.fsNode.find(
193+
return this.fsNode.children.find(
193194
matcha.templateFile({
194195
nameOptions: ['root']
195196
})
196197
)
197198
}
198199

199-
getSubtreeMatchers() {
200-
return {
201-
collectionIndexFile: matcha.templateFile({
202-
nameOptions: this.collectionAliases.concat('collection')
203-
}),
200+
getCollectionAliases() {
201+
const collectionAliasesFromContentTypes = this.contentTypes
202+
.filter(ct => ct.model === 'collection')
203+
.map(ct => ct.collectionAlias)
204204

205-
collection: matcha.directory({
206-
children: matcha.either(
207-
matcha.templateFile({
208-
nameOptions: this.collectionAliases.concat('collection')
209-
}),
210-
matcha.dataFile({
211-
nameOptions: (fsNode) => ([fsNode.name])
212-
}),
213-
)
214-
}),
205+
const collectionAliasesFromFrontMatter = this.collectionAliases || []
206+
207+
return [
208+
...collectionAliasesFromContentTypes,
209+
...collectionAliasesFromFrontMatter
210+
]
211+
}
215212

216-
homepage: matcha.folderable({
213+
getChildContext() {
214+
return this.context
215+
}
216+
217+
getSubtreeConfig() {
218+
const collectionAliases = this.getCollectionAliases()
219+
220+
return [{
221+
key: 'homepage',
222+
singular: true,
223+
model: models.Homepage,
224+
matcher: matcha.folderable({
217225
nameOptions: {
218226
folder: [this.settings.homepageDirectory, 'homepage', 'home'],
219227
index: ['index'],
220228
standalone: ['homepage', 'home', 'index']
221229
}
222230
}),
223-
224-
subpage: matcha.folderable({
231+
settings: {
232+
homepageDirectory: this.settings.homepageDirectory
233+
}
234+
}, {
235+
key: 'subpages',
236+
model: models.Subpage,
237+
matcher: matcha.folderable({
225238
nameOptions: {
226239
index: ['page', 'index']
227240
}
228241
}),
229-
230-
pagesDirectory: matcha.directory({
242+
settings: {
243+
pagesDirectory: this.settings.pagesDirectory
244+
}
245+
}, {
246+
key: 'pagesDirectory',
247+
singular: true,
248+
model: models.PagesDirectory,
249+
matcher: matcha.directory({
231250
nameOptions: [this.settings.pagesDirectory, 'subpages', 'pages']
232251
}),
233-
234-
assetsDirectory: matcha.directory({
235-
nameOptions: [this.settings.assetsDirectory, 'assets']
236-
}),
237-
238-
asset: matcha.true()
239-
}
240-
}
241-
242-
parseSubtree() {
243-
const tree = {
244-
homepage: new models.Homepage(
245-
{ name: 'index', extension: 'md', content: '' },
246-
this.context,
247-
{ homepageDirectory: this.settings.homepageDirectory }
248-
),
249-
pagesDirectory: [],
250-
subpages: [],
251-
collections: [],
252-
assets: []
253-
}
254-
255-
this.fsNode.forEach(node => {
256-
if (node === this.indexFile) {
257-
return
258-
}
259-
260-
if (this.matchers.homepage(node)) {
261-
tree.homepage = new models.Homepage(node, this.context, {
262-
homepageDirectory: this.settings.homepageDirectory
263-
})
264-
return
252+
settings: {
253+
pagesDirectory: this.settings.pagesDirectory,
254+
assetsDirectory: this.settings.assetsDirectory,
255+
debug: this.settings.debug
256+
},
257+
sideEffect: (tree, entry) => {
258+
tree.subpages.push(...entry.subtree.subpages)
259+
tree.assets.push(...entry.subtree.assets)
265260
}
266-
267-
if (this.matchers.subpage(node)) {
268-
return tree.subpages.push(
269-
new models.Subpage(node, this.context, {
270-
pagesDirectory: this.settings.pagesDirectory
271-
})
261+
}, {
262+
key: 'collections',
263+
model: models.Collection,
264+
matcher: matcha.directory({
265+
children: matcha.either(
266+
matcha.templateFile({
267+
nameOptions: collectionAliases.concat('collection')
268+
}),
269+
matcha.dataFile({
270+
nameOptions: (fsNode) => ([fsNode.name])
271+
}),
272272
)
273+
}),
274+
settings: {
275+
defaultCategoryName: this.settings.defaultCategoryName,
276+
collectionAliases,
277+
mode: this.settings.mode,
278+
contentTypes: this.contentTypes,
279+
sortBy: 'date',
280+
sortOrder: -1
273281
}
274-
275-
if (this.matchers.pagesDirectory(node)) {
276-
tree.pagesDirectory = new models.PagesDirectory(node, this.context, {
277-
pagesDirectory: this.settings.pagesDirectory,
278-
assetsDirectory: this.settings.assetsDirectory,
279-
debug: this.settings.debug
280-
})
281-
tree.subpages.push(...tree.pagesDirectory.subtree.subpages)
282-
tree.assets.push(...tree.pagesDirectory.subtree.assets)
283-
return
284-
}
285-
286-
if (this.matchers.collection(node)) {
287-
const indexFile = node.children.find(this.matchers.collectionIndexFile)
288-
289-
const contentType = this.contentTypes
290-
.filter(ct => ct.model === 'collection')
291-
.find(ct => {
292-
return ct.collectionAlias === (indexFile ? removeExtension(indexFile.name) : node.name)
293-
})
294-
295-
const collection = new models.Collection(node, this.context, {
296-
defaultCategoryName: this.settings.defaultCategoryName,
297-
collectionAliases: this.collectionAliases,
298-
mode: this.settings.mode,
299-
contentTypes: this.contentTypes,
300-
sortBy: 'date',
301-
sortOrder: -1,
302-
contentType
303-
})
304-
305-
if (ContentModel.draftCheck(this.settings.mode, collection)) {
306-
tree.collections.push(collection)
307-
}
308-
return
309-
}
310-
311-
if (this.matchers.assetsDirectory(node)) {
312-
tree.assetsDirectory = new models.AssetsDirectory(node, this.context, {
313-
assetsDirectory: this.settings.assetsDirectory
314-
})
315-
tree.assets.push(...tree.assetsDirectory.subtree.assets)
316-
return
282+
}, {
283+
key: 'assetsDirectory',
284+
singular: true,
285+
model: models.AssetsDirectory,
286+
matcher: matcha.directory({
287+
nameOptions: [this.settings.assetsDirectory, 'assets']
288+
}),
289+
settings: {
290+
assetsDirectory: this.settings.assetsDirectory
291+
},
292+
sideEffect: (tree, entry) => {
293+
tree.assets.push(...entry.subtree.assets)
317294
}
318-
319-
if (this.matchers.asset(node)) {
320-
return tree.assets.push(
321-
new models.Asset(node, this.context, {
322-
assetsDirectory: this.settings.assetsDirectory
323-
})
324-
)
295+
}, {
296+
key: 'assets',
297+
model: models.Asset,
298+
matcher: matcha.true(),
299+
settings: {
300+
assetsDirectory: this.settings.assetsDirectory
325301
}
326-
})
327-
return tree
302+
}]
328303
}
329304

330305
afterEffects() {

src/compiler/contentModel/models/asset.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class Asset extends ContentModelResourceNode {
1212

1313
constructor(fsNode, context, settings = defaultSettings) {
1414
super(fsNode, context, settings)
15-
this.subtree = this.parseSubtree()
1615
}
1716

1817
getPermalink() {

src/compiler/contentModel/models/assetsDirectory.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,25 @@ class AssetsDirectory extends ContentModelEntryNode {
1717

1818
constructor(fsNode, context, settings) {
1919
super(fsNode, context, settings)
20-
this.matchers = this.getSubtreeMatchers()
21-
this.subtree = this.parseSubtree()
22-
this.afterEffects()
20+
this.subtreeConfig = this.getSubtreeConfig()
21+
this.subtree = this.parseSubtree({
22+
assets: []
23+
})
2324
}
2425

25-
getSubtreeMatchers() {
26-
return {
27-
asset: matcha.true()
28-
}
26+
getChildContext() {
27+
return this.context
2928
}
3029

31-
parseSubtree() {
32-
const tree = {
33-
assets: []
34-
}
35-
36-
this.fsNode.children.forEach(childNode => {
37-
if (this.matchers.asset(childNode)) {
38-
return tree.assets.push(
39-
new models.Asset(childNode, this.context, {
40-
assetsDirectory: this.settings.assetsDirectory
41-
})
42-
)
30+
getSubtreeConfig() {
31+
return [{
32+
key: 'assets',
33+
model: models.Asset,
34+
matcher: matcha.true(),
35+
settings: {
36+
assetsDirectory: this.settings.assetsDirectory
4337
}
44-
})
45-
return tree
38+
}]
4639
}
4740
}
4841

src/compiler/contentModel/models/attachment.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class Attachment extends ContentModelResourceNode {
77

88
constructor(fsNode, context, settings) {
99
super(fsNode, context, settings)
10-
this.subtree = this.parseSubtree()
1110
}
1211

1312
render(renderer) {

0 commit comments

Comments
 (0)