@@ -2,7 +2,7 @@ const { join, resolve } = require('path')
2
2
const _ = require ( 'lodash' )
3
3
const frontMatter = require ( 'front-matter' )
4
4
const makeSlug = require ( 'slug' )
5
- const { isTemplateFile, Markdown } = require ( '../../helpers' )
5
+ const { isTemplateFile, removeExtension , Markdown } = require ( '../../helpers' )
6
6
const models = {
7
7
attachment : require ( '../attachment' ) ,
8
8
category : require ( './category' ) ,
@@ -17,11 +17,19 @@ function parseContent(node, content) {
17
17
}
18
18
19
19
const defaultSettings = {
20
- defaultCategoryName : 'Unclassified'
20
+ defaultCategoryName : 'Unclassified' ,
21
+ collectionAliases : [ ]
21
22
}
22
- module . exports = function collection ( settings = defaultSettings ) {
23
+ module . exports = function collection ( settings = defaultSettings , contentTypes = [ ] ) {
24
+ const indexFileNameOptions = [
25
+ ...settings . collectionAliases ,
26
+ 'collection'
27
+ ] . filter ( Boolean )
28
+
23
29
const isCollectionIndexFile = ( node ) => {
24
- return isTemplateFile ( node ) && node . name . match ( / ^ c o l l e c t i o n \. .+ $ / )
30
+ return isTemplateFile ( node ) && node . name . match (
31
+ new RegExp ( `^(${ indexFileNameOptions . join ( '|' ) } )\\..+$` )
32
+ )
25
33
}
26
34
27
35
return {
@@ -85,45 +93,50 @@ module.exports = function collection(settings = defaultSettings) {
85
93
}
86
94
87
95
const indexFile = node . children . find ( isCollectionIndexFile )
88
- const indexProps = indexFile ? frontMatter ( indexFile . content ) : { }
96
+ const indexProps = frontMatter ( indexFile . content )
97
+ const indexFileName = removeExtension ( indexFile . name )
98
+
99
+ const contentType = contentTypes
100
+ . filter ( ct => ct . model === 'collection' )
101
+ . find ( ct => ct . collectionAlias === indexFileName )
89
102
90
103
const slug = indexProps . attributes ?. slug || makeSlug ( node . name )
91
104
const permalink = context . root . permalink + slug
92
105
const outputPath = join ( context . root . outputPath , slug )
93
106
const collectionContext = {
94
107
...indexProps . attributes ,
95
- contentType : indexProps . attributes ?. contentType || 'default' ,
96
- categoryContentType : indexProps . attributes ?. categoryContentType || 'default' ,
97
- entryContentType : indexProps . attributes ?. entryContentType || 'default' ,
98
- categoryAlias : indexProps . attributes ?. categoryAlias ,
99
- categoriesAlias : indexProps . attributes ?. categoriesAlias ,
100
- entryAlias : indexProps . attributes ?. entryAlias ,
101
- entriesAlias : indexProps . attributes ?. entriesAlias ,
102
- defaultCategoryName : indexProps . attributes ?. defaultCategoryName || settings . defaultCategoryName ,
108
+ contentType : indexProps . attributes ?. contentType || contentType ?. name || 'default' ,
109
+ categoryContentType : indexProps . attributes ?. categoryContentType || contentType ?. categoryContentType || 'default' ,
110
+ entryContentType : indexProps . attributes ?. entryContentType || contentType ?. entryContentType || 'default' ,
111
+ categoryAlias : indexProps . attributes ?. categoryAlias || contentType ?. categoryAlias ,
112
+ categoriesAlias : indexProps . attributes ?. categoriesAlias || contentType ?. categoriesAlias ,
113
+ entryAlias : indexProps . attributes ?. entryAlias || contentType ?. entryAlias ,
114
+ entriesAlias : indexProps . attributes ?. entriesAlias || contentType ?. entriesAlias ,
115
+ defaultCategoryName : indexProps . attributes ?. defaultCategoryName || contentType ?. defaultCategoryName || settings . defaultCategoryName ,
103
116
title : indexProps . attributes ?. title || node . name ,
104
117
slug,
105
118
permalink,
106
119
outputPath
107
120
}
108
121
109
- const categoriesAlias = indexProps . attributes ?. categoriesAlias
122
+ const categoriesAlias = indexProps . attributes ?. categoriesAlias || contentType ?. categoriesAlias
110
123
if ( categoriesAlias ) {
111
124
tree [ categoriesAlias ] = tree . categories
112
125
}
113
126
114
- const entriesAlias = indexProps . attributes ?. entriesAlias
127
+ const entriesAlias = indexProps . attributes ?. entriesAlias || contentType ?. entriesAlias
115
128
if ( entriesAlias ) {
116
129
tree [ entriesAlias ] = tree . posts
117
130
}
118
131
119
132
const childModels = {
120
133
attachment : models . attachment ( ) ,
121
134
category : models . category ( {
122
- categoryAlias : indexProps . attributes ?. categoryAlias ,
123
- entryAlias : indexProps . attributes ?. entryAlias
135
+ categoryAlias : indexProps . attributes ?. categoryAlias || contentType ?. categoryAlias ,
136
+ entryAlias : indexProps . attributes ?. entryAlias || contentType ?. entryAlias
124
137
} ) ,
125
138
post : models . post ( {
126
- entryAlias : indexProps . attributes ?. entryAlias
139
+ entryAlias : indexProps . attributes ?. entryAlias || contentType ?. entryAlias
127
140
} )
128
141
}
129
142
0 commit comments