WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 28936a0

Browse files
committed
Extract an abstract fs node matcher: matcha
Use it in all models to reduce boilerplate and duplication in matchers. Delete the obsolete matchers.js which was actually only used by index. Now, what's duplicating is the matcha expressions. Maybe to be solved in new iterations. Tests needed: - matcha - isDataFile - subCategory recursion - root file doesn't register as subpage
1 parent f6fb2b5 commit 28936a0

File tree

9 files changed

+256
-233
lines changed

9 files changed

+256
-233
lines changed

src/compiler/contentModel/index.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const _ = require('lodash')
33
const frontMatter = require('front-matter')
44
const ImmutableStack = require('../../lib/ImmutableStack')
55
const { removeExtension, isTemplateFile } = require('../../lib/contentModelHelpers')
6-
const createMatchers = require('./matchers')
6+
const matcha = require('../../lib/matcha')
77

88
const models = {
99
Homepage: require('./models/homepage'),
@@ -157,7 +157,48 @@ class ContentModel {
157157
...contentModelSettings
158158
}
159159
this.contentTypes = contentTypes
160-
this.matchers = createMatchers(this.settings)
160+
}
161+
162+
getSubtreeMatchers() {
163+
return {
164+
collectionIndexFile: matcha.indexFile({
165+
nameOptions: this.collectionAliases.concat('collection').filter(Boolean)
166+
}),
167+
168+
collection: matcha.directory({
169+
children: matcha.either(
170+
matcha.indexFile({
171+
nameOptions: this.collectionAliases.concat('collection')
172+
}),
173+
matcha.dataFile({
174+
nameOptions: (fsNode) => ([fsNode.name])
175+
}),
176+
)
177+
}),
178+
179+
homepage: matcha.namedFolderable({
180+
nameOptions: {
181+
folder: [this.settings.homepageDirectory, 'homepage', 'home'],
182+
index: ['homepage', 'home', 'index']
183+
}
184+
}),
185+
186+
subpage: matcha.folderable({
187+
nameOptions: {
188+
index: ['page', 'index']
189+
}
190+
}),
191+
192+
pagesDirectory: matcha.directory({
193+
nameOptions: [this.settings.pagesDirectory, 'subpages', 'pages']
194+
}),
195+
196+
asset: matcha.true(),
197+
198+
assetsDirectory: matcha.directory({
199+
nameOptions: [this.settings.assetsDirectory, 'assets']
200+
})
201+
}
161202
}
162203

163204
draftCheck(node) {
@@ -200,7 +241,13 @@ class ContentModel {
200241
...(indexProps.attributes?.collectionAliases || [])
201242
]
202243

244+
this.matchers = this.getSubtreeMatchers()
245+
203246
fileSystemTree.forEach(node => {
247+
if (isRootIndexFile(node)) {
248+
return
249+
}
250+
204251
if (this.matchers.homepage(node)) {
205252
this.contentModel.homepage = new models.Homepage(node, context, {
206253
homepageDirectory: this.settings.homepageDirectory
@@ -234,10 +281,8 @@ class ContentModel {
234281
})
235282
}
236283

237-
if (this.matchers.collection(node, this.collectionAliases)) {
238-
const indexFile = node.children.find(
239-
this.matchers.collectionIndexFile(this.collectionAliases)
240-
)
284+
if (this.matchers.collection(node)) {
285+
const indexFile = node.children.find(this.matchers.collectionIndexFile)
241286

242287
const contentType = this.contentTypes
243288
.filter(ct => ct.model === 'collection')

src/compiler/contentModel/matchers.js

Lines changed: 0 additions & 115 deletions
This file was deleted.

src/compiler/contentModel/models/collection/category.js

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const makeSlug = require('slug')
22
const _ = require('lodash')
33
const ContentModelEntryNode = require('../../../../lib/ContentModelEntryNode')
44
const { makePermalink, isTemplateFile, sort } = require('../../../../lib/contentModelHelpers')
5+
const matcha = require('../../../../lib/matcha')
56

67
const models = {
78
Post: require('./post'),
@@ -106,39 +107,28 @@ class Category extends ContentModelEntryNode {
106107
getSubtreeMatchers() {
107108
// copy-pasted from matchers.js
108109
return {
109-
indexFile: fsNode => {
110-
const indexFileNameOptions = [this.settings.categoryAlias, 'category'].filter(Boolean)
111-
return isTemplateFile(fsNode) && fsNode.name.match(
112-
new RegExp(`^(${indexFileNameOptions.join('|')})\\..+$`)
113-
)
114-
},
115-
116-
category: (fsNode, level) => {
117-
return fsNode.children?.find(childNode => {
118-
const containsPosts = this.matchers.post(childNode)
119-
if (level > 3) {
120-
return containsPosts
121-
}
122-
const containsSubCategories = this.matchers.category(childNode, level)
123-
return containsSubCategories || containsPosts
124-
})
125-
},
126-
127-
postIndexFile: fsNode => {
128-
const indexFileNameOptions = [this.settings.entryAlias, 'post', 'index'].filter(Boolean)
129-
return (
130-
isTemplateFile(fsNode) &&
131-
fsNode.name.match(
132-
new RegExp(`^(${indexFileNameOptions.join('|')})\\..+$`)
133-
)
134-
)
135-
},
136-
137-
post: fsNode => {
138-
return isTemplateFile(fsNode) || fsNode.children?.find(this.matchers.postIndexFile)
139-
},
140-
141-
attachment: fsNode => true
110+
indexFile: matcha.indexFile({
111+
nameOptions: [this.settings.categoryAlias, 'category']
112+
}),
113+
114+
category: matcha.directory({
115+
childSearchDepth: 3,
116+
children: [
117+
matcha.folderable({
118+
nameOptions: {
119+
index: [this.settings.entryAlias, 'post', 'index']
120+
}
121+
})
122+
]
123+
}),
124+
125+
post: matcha.folderable({
126+
nameOptions: {
127+
index: [this.settings.entryAlias, 'post', 'index']
128+
}
129+
}),
130+
131+
attachment: matcha.true()
142132
}
143133
}
144134

@@ -187,7 +177,7 @@ class Category extends ContentModelEntryNode {
187177
return
188178
}
189179

190-
if (this.matchers.category(childNode, this.settings.level)) {
180+
if (this.matchers.category(childNode)) {
191181
const subCategorySettings = {
192182
contentTypes: this.settings.contentTypes,
193183
entryContentType: this.entryContentType || this.settings.entryContentType,

src/compiler/contentModel/models/collection/index.js

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const {
1010
sort
1111
} = require('../../../../lib/contentModelHelpers')
1212

13+
const matcha = require('../../../../lib/matcha')
14+
1315
const models = {
1416
Attachment: require('../attachment'),
1517
Category: require('./category'),
@@ -96,47 +98,32 @@ class Collection extends ContentModelEntryNode {
9698
getSubtreeMatchers() {
9799
// copy-pasted from matchers.js
98100
return {
99-
indexFile: (node) => {
100-
const indexFileNameOptions = [
101-
...this.settings.collectionAliases,
102-
'collection'
103-
].filter(Boolean)
104-
105-
return isTemplateFile(node) && node.name.match(
106-
new RegExp(`^(${indexFileNameOptions.join('|')})\\..+$`)
107-
)
108-
},
109-
110-
dataFile: (node, parentNode) => {
111-
return node.name.match(new RegExp(`^${parentNode.name}\\.json$`, 'i'))
112-
},
113-
114-
category: (fsNode, level) => {
115-
return fsNode.children?.find(childNode => {
116-
const containsPosts = this.matchers.post(childNode)
117-
if (level > 3) {
118-
return containsPosts
119-
}
120-
const containsSubCategories = this.matchers.category(childNode, level)
121-
return containsSubCategories || containsPosts
122-
})
123-
},
124-
125-
postIndexFile: fsNode => {
126-
const indexFileNameOptions = [this.entryAlias, this.settings.contentType?.entryAlias, 'post', 'index'].filter(Boolean)
127-
return (
128-
isTemplateFile(fsNode) &&
129-
fsNode.name.match(
130-
new RegExp(`^(${indexFileNameOptions.join('|')})\\..+$`)
131-
)
132-
)
133-
},
101+
indexFile: matcha.indexFile({
102+
nameOptions: (this.settings.collectionAliases || []).concat('collection').filter(Boolean)
103+
}),
104+
105+
dataFile: matcha.dataFile({
106+
nameOptions: [this.fsNode.name]
107+
}),
108+
109+
category: matcha.directory({
110+
childSearchDepth: 3,
111+
children: [
112+
matcha.folderable({
113+
nameOptions: {
114+
index: [this.settings.entryAlias, 'post', 'index']
115+
}
116+
})
117+
]
118+
}),
134119

135-
post: fsNode => {
136-
return isTemplateFile(fsNode) || fsNode.children?.find(this.matchers.postIndexFile)
137-
},
120+
post: matcha.folderable({
121+
nameOptions: {
122+
index: [this.entryAlias, this.settings.contentType?.entryAlias, 'post', 'index']
123+
}
124+
}),
138125

139-
attachment: fsNode => true
126+
attachment: matcha.true()
140127
}
141128
}
142129

@@ -176,7 +163,7 @@ class Collection extends ContentModelEntryNode {
176163
return this.addUncategorizedPost(childNode, null, tree)
177164
}
178165

179-
if (this.matchers.category(childNode, 1)) {
166+
if (this.matchers.category(childNode)) {
180167
const categorySettings = {
181168
defaultCategoryName: this.defaultCategoryName || this.settings.contentType?.defaultCategoryName || this.settings.defaultCategoryName,
182169
contentTypes: this.settings.contentTypes,
@@ -418,4 +405,4 @@ class Collection extends ContentModelEntryNode {
418405
}
419406
}
420407

421-
module.exports = Collection
408+
module.exports = Collection

0 commit comments

Comments
 (0)