Skip to content

Commit d20e2a0

Browse files
committed
Matcha improvements & tests
- indexFile -> templateFile - homepage folder's index file can be now named only 'index' - re-use a postMatcher for both category child and post - upgrade matcha.either and matcha.and usable as standalone matchers - fix nameOptions override bug in computeOptions - guard nameoptions before [].joining it in regexes - add standalone nameOption to folderable and remove namedFolderable
1 parent 259aa2f commit d20e2a0

File tree

8 files changed

+629
-83
lines changed

8 files changed

+629
-83
lines changed

src/compiler/contentModel/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ class ContentModel {
161161

162162
getSubtreeMatchers() {
163163
return {
164-
collectionIndexFile: matcha.indexFile({
164+
collectionIndexFile: matcha.templateFile({
165165
nameOptions: this.collectionAliases.concat('collection').filter(Boolean)
166166
}),
167167

168168
collection: matcha.directory({
169169
children: matcha.either(
170-
matcha.indexFile({
170+
matcha.templateFile({
171171
nameOptions: this.collectionAliases.concat('collection')
172172
}),
173173
matcha.dataFile({
@@ -176,10 +176,11 @@ class ContentModel {
176176
)
177177
}),
178178

179-
homepage: matcha.namedFolderable({
179+
homepage: matcha.folderable({
180180
nameOptions: {
181181
folder: [this.settings.homepageDirectory, 'homepage', 'home'],
182-
index: ['homepage', 'home', 'index']
182+
index: ['index'],
183+
standalone: ['homepage', 'home', 'index']
183184
}
184185
}),
185186

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,28 +105,23 @@ class Category extends ContentModelEntryNode {
105105
}
106106

107107
getSubtreeMatchers() {
108-
// copy-pasted from matchers.js
108+
const postMatcher = matcha.folderable({
109+
nameOptions: {
110+
index: [this.settings.entryAlias, 'post', 'index']
111+
}
112+
})
113+
109114
return {
110-
indexFile: matcha.indexFile({
115+
indexFile: matcha.templateFile({
111116
nameOptions: [this.settings.categoryAlias, 'category']
112117
}),
113118

114119
category: matcha.directory({
115120
childSearchDepth: 3,
116-
children: [
117-
matcha.folderable({
118-
nameOptions: {
119-
index: [this.settings.entryAlias, 'post', 'index']
120-
}
121-
})
122-
]
121+
children: [ postMatcher ]
123122
}),
124123

125-
post: matcha.folderable({
126-
nameOptions: {
127-
index: [this.settings.entryAlias, 'post', 'index']
128-
}
129-
}),
124+
post: postMatcher,
130125

131126
attachment: matcha.true()
132127
}

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,14 @@ class Collection extends ContentModelEntryNode {
9696
}
9797

9898
getSubtreeMatchers() {
99-
// copy-pasted from matchers.js
99+
const postMatcher = matcha.folderable({
100+
nameOptions: {
101+
index: [this.entryAlias, this.settings.contentType?.entryAlias, 'post', 'index']
102+
}
103+
})
104+
100105
return {
101-
indexFile: matcha.indexFile({
106+
indexFile: matcha.templateFile({
102107
nameOptions: (this.settings.collectionAliases || []).concat('collection').filter(Boolean)
103108
}),
104109

@@ -108,20 +113,10 @@ class Collection extends ContentModelEntryNode {
108113

109114
category: matcha.directory({
110115
childSearchDepth: 3,
111-
children: [
112-
matcha.folderable({
113-
nameOptions: {
114-
index: [this.settings.entryAlias, 'post', 'index']
115-
}
116-
})
117-
]
116+
children: [ postMatcher ]
118117
}),
119118

120-
post: matcha.folderable({
121-
nameOptions: {
122-
index: [this.entryAlias, this.settings.contentType?.entryAlias, 'post', 'index']
123-
}
124-
}),
119+
post: postMatcher,
125120

126121
attachment: matcha.true()
127122
}
@@ -405,4 +400,4 @@ class Collection extends ContentModelEntryNode {
405400
}
406401
}
407402

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Post extends ContentModelEntryNode {
3333

3434
getSubtreeMatchers() {
3535
return {
36-
indexFile: matcha.indexFile({
36+
indexFile: matcha.templateFile({
3737
nameOptions: [this.settings.entryAlias, 'post', 'index']
3838
}),
3939

@@ -125,4 +125,4 @@ class Post extends ContentModelEntryNode {
125125
}
126126
}
127127

128-
module.exports = Post
128+
module.exports = Post

src/compiler/contentModel/models/homepage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Homepage extends ContentModelEntryNode {
3333

3434
getSubtreeMatchers() {
3535
return {
36-
indexFile: matcha.indexFile({
36+
indexFile: matcha.templateFile({
3737
nameOptions: ['homepage', 'home', 'index']
3838
}),
3939

@@ -113,4 +113,4 @@ class Homepage extends ContentModelEntryNode {
113113
}
114114
}
115115

116-
module.exports = Homepage
116+
module.exports = Homepage

src/compiler/contentModel/models/subpage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Subpage extends ContentModelEntryNode {
2525

2626
getSubtreeMatchers() {
2727
return {
28-
indexFile: matcha.indexFile({
28+
indexFile: matcha.templateFile({
2929
nameOptions: ['page', 'index']
3030
}),
3131

@@ -108,4 +108,4 @@ class Subpage extends ContentModelEntryNode {
108108
}
109109
}
110110

111-
module.exports = Subpage
111+
module.exports = Subpage

src/lib/matcha.js

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
11
const { isDataFile, isTemplateFile } = require('./contentModelHelpers')
22

3-
const logicFnMap = {
4-
'or': 'some',
5-
'and': 'every'
6-
}
7-
83
const matcha = {
94
helpers: {}
105
}
116

127
matcha.helpers.computeOptions = (options, fsNode) => {
138
const result = {}
149
if (options.nameOptions) {
10+
result.nameOptions = options.nameOptions
1511
if (typeof options.nameOptions === 'function') {
1612
result.nameOptions = options.nameOptions(fsNode)
1713
}
18-
result.nameOptions = options.nameOptions
1914
}
2015
return result
2116
}
2217

2318
matcha.true = () => fsNode => true
2419
matcha.false = () => fsNode => false
2520

26-
matcha.either = (...matchers) => {
27-
return {
28-
logic: 'or',
29-
matchers
21+
matcha.either = (...matchers) => (fsNode) => {
22+
if (Array.isArray(fsNode)) {
23+
return matchers.some(matcher => fsNode.some(matcher))
3024
}
25+
return matchers.some(matcher => matcher(fsNode))
3126
}
3227

33-
matcha.and = (...matchers) => {
34-
return {
35-
logic: 'and',
36-
matchers
28+
matcha.and = (...matchers) => (fsNode) => {
29+
if (Array.isArray(fsNode)) {
30+
return matchers.every(matcher => fsNode.some(matcher))
3731
}
32+
return matchers.every(matcher => matcher(fsNode))
3833
}
3934

4035
matcha.directory = (options = {}) => {
@@ -56,12 +51,15 @@ matcha.directory = (options = {}) => {
5651

5752
let childrenMatch = true
5853
if (options.children) {
59-
const logicFn = logicFnMap[options.children.logic || 'and']
60-
const matchers = options.children.matchers || options.children
61-
childrenMatch = matchers[logicFn](childMatcher => {
62-
return fsNode.children.some(childMatcher)
63-
})
54+
if (typeof options.children === 'function') {
55+
childrenMatch = options.children(fsNode.children)
56+
} else {
57+
childrenMatch = options.children.every(childMatcher => {
58+
return fsNode.children.some(childMatcher)
59+
})
60+
}
6461
}
62+
6563
if (childrenMatch) {
6664
return true
6765
}
@@ -78,51 +76,48 @@ matcha.directory = (options = {}) => {
7876
return recursionMatch
7977
}
8078
}
79+
8180
matcha.dataFile = (options = {}) => {
8281
return (fsNode) => {
8382
const { nameOptions } = matcha.helpers.computeOptions(options, fsNode)
8483
return (
85-
isDataFile(fsNode) &&
86-
fsNode.name.match(
87-
new RegExp(`^(${nameOptions.join('|')})\\..+$`)
84+
isDataFile(fsNode) && (
85+
!nameOptions || fsNode.name.match(
86+
new RegExp(`^(${nameOptions.join('|')})\\..+$`)
87+
)
8888
)
8989
)
9090
}
9191
}
9292

93-
matcha.indexFile = (options = {}) => {
93+
matcha.templateFile = (options = {}) => {
9494
return (fsNode) => {
9595
const { nameOptions } = matcha.helpers.computeOptions(options, fsNode)
9696
return (
97-
isTemplateFile(fsNode) &&
98-
fsNode.name.match(
99-
new RegExp(`^(${nameOptions.join('|')})\\..+$`)
97+
isTemplateFile(fsNode) && (
98+
!nameOptions || fsNode.name.match(
99+
new RegExp(`^(${nameOptions.join('|')})\\..+$`)
100+
)
100101
)
101102
)
102103
}
103104
}
104105

105106
matcha.folderable = (options = {}) => {
106107
return (fsNode) => {
107-
const { nameOptions } = matcha.helpers.computeOptions(options, fsNode)
108-
return isTemplateFile(fsNode) || fsNode.children?.find(
109-
matcha.indexFile({
110-
nameOptions: nameOptions.index
111-
})
112-
)
113-
}
114-
}
115-
116-
matcha.namedFolderable = (options = {}) => {
117-
return fsNode => {
118-
const { nameOptions } = matcha.helpers.computeOptions(options, fsNode)
119-
const namedFile = matcha.indexFile({
120-
nameOptions: nameOptions.index
121-
})(fsNode)
122-
return namedFile || matcha.directory({
108+
const { nameOptions = {} } = matcha.helpers.computeOptions(options, fsNode)
109+
const standaloneFileMatcher = matcha.templateFile({
110+
nameOptions: nameOptions.standalone
111+
})
112+
const indexedDirectoryMatcher = matcha.directory({
123113
nameOptions: nameOptions.folder,
124-
children: [namedFile]
125-
})(fsNode)
114+
children: [
115+
matcha.templateFile({
116+
nameOptions: nameOptions.index
117+
})
118+
]
119+
})
120+
return standaloneFileMatcher(fsNode) || indexedDirectoryMatcher(fsNode)
126121
}
127122
}
128123

0 commit comments

Comments
 (0)