@@ -2,12 +2,13 @@ const { resolve } = require('path')
22const _ = require ( 'lodash' )
33const frontMatter = require ( 'front-matter' )
44const ImmutableStack = require ( '../../lib/ImmutableStack' )
5- const { isTemplateFile } = require ( './helpers ' )
5+ const { removeExtension , isTemplateFile } = require ( '../../lib/contentModelHelpers ' )
66const createMatchers = require ( './matchers' )
7+
78const models = {
89 Homepage : require ( './models/homepage' ) ,
910 Subpage : require ( './models/subpage' ) ,
10- collection : require ( './models/collection' ) ,
11+ Collection : require ( './models/collection' ) ,
1112 Asset : require ( './models/asset' )
1213}
1314
@@ -88,7 +89,7 @@ const linkBack = (post, entry, key) => {
8889
8990const linkEntries = ( contentModel ) => {
9091 contentModel . collections . forEach ( collection => {
91- collection . posts . forEach ( post => {
92+ collection . subtree . posts . forEach ( post => {
9293 const fields = Object . keys ( post )
9394 Object . keys ( post ) . forEach ( key => {
9495 const value = post [ key ]
@@ -141,6 +142,15 @@ const defaultContentModelSettings = {
141142 mode : 'start'
142143}
143144class ContentModel {
145+ static serialize ( contentModel ) {
146+ return {
147+ homepage : models . Homepage . serialize ( contentModel . homepage ) ,
148+ subpages : contentModel . subpages . map ( models . Subpage . serialize ) ,
149+ collections : contentModel . collections . map ( models . Collection . serialize ) ,
150+ assets : contentModel . assets . map ( models . Asset . serialize )
151+ }
152+ }
153+
144154 constructor ( contentModelSettings = defaultContentModelSettings , contentTypes = [ ] ) {
145155 this . settings = {
146156 ...defaultContentModelSettings ,
@@ -172,47 +182,35 @@ class ContentModel {
172182 permalink : this . settings . permalinkPrefix
173183 } ] )
174184
175- this . models = {
176- collection : models . collection ( {
177- defaultCategoryName : this . settings . defaultCategoryName ,
178- collectionAliases : [
179- ...this . contentTypes
180- . filter ( ct => ct . model === 'collection' )
181- . map ( ct => ct . collectionAlias ) ,
182- ...( indexProps . attributes ?. collectionAliases || [ ] )
183- ] ,
184- mode : this . settings . mode
185- } , this . contentTypes , this . matchers ) ,
186-
187- Subpage : models . Subpage ,
188-
189- Homepage : models . Homepage ,
190-
191- Asset : models . Asset
192- }
193-
194185 this . contentModel = {
195- homepage : new this . models . Homepage ( {
196- name : 'index' ,
197- extension : 'md' ,
198- content : ''
199- } , context , { homepageDirectory : this . settings . homepageDirectory } ) ,
186+ homepage : new models . Homepage (
187+ { name : 'index' , extension : 'md' , content : '' } ,
188+ context ,
189+ { homepageDirectory : this . settings . homepageDirectory }
190+ ) ,
200191 subpages : [ ] ,
201192 collections : [ ] ,
202193 assets : [ ]
203194 }
204195
196+ this . collectionAliases = [
197+ ...this . contentTypes
198+ . filter ( ct => ct . model === 'collection' )
199+ . map ( ct => ct . collectionAlias ) ,
200+ ...( indexProps . attributes ?. collectionAliases || [ ] )
201+ ]
202+
205203 fileSystemTree . forEach ( node => {
206204 if ( this . matchers . homepage ( node ) ) {
207- this . contentModel . homepage = new this . models . Homepage ( node , context , {
205+ this . contentModel . homepage = new models . Homepage ( node , context , {
208206 homepageDirectory : this . settings . homepageDirectory
209207 } )
210208 return
211209 }
212210
213211 if ( this . matchers . subpage ( node ) ) {
214212 return this . contentModel . subpages . push (
215- new this . models . Subpage ( node , context , {
213+ new models . Subpage ( node , context , {
216214 pagesDirectory : this . settings . pagesDirectory
217215 } )
218216 )
@@ -222,22 +220,39 @@ class ContentModel {
222220 return node . children . forEach ( childNode => {
223221 if ( this . matchers . subpage ( childNode ) ) {
224222 this . contentModel . subpages . push (
225- new this . models . subpage ( childNode , context , {
223+ new models . subpage ( childNode , context , {
226224 pagesDirectory : this . settings . pagesDirectory
227225 } )
228226 )
229227 } else if ( this . matchers . asset ( childNode ) ) {
230228 this . contentModel . assets . push (
231- new this . models . Asset ( childNode , context , {
229+ new models . Asset ( childNode , context , {
232230 assetsDirectory : this . settings . assetsDirectory
233231 } )
234232 )
235233 }
236234 } )
237235 }
238236
239- if ( this . models . collection . match ( node ) ) {
240- const collection = this . models . collection . create ( node , context )
237+ if ( this . matchers . collection ( node , this . collectionAliases ) ) {
238+ const indexFile = node . children . find (
239+ this . matchers . collectionIndexFile ( this . collectionAliases )
240+ )
241+
242+ const contentType = this . contentTypes
243+ . filter ( ct => ct . model === 'collection' )
244+ . find ( ct => {
245+ return ct . collectionAlias === ( indexFile ? removeExtension ( indexFile . name ) : node . name )
246+ } )
247+
248+ const collection = new models . Collection ( node , context , {
249+ defaultCategoryName : this . settings . defaultCategoryName ,
250+ collectionAliases : this . collectionAliases ,
251+ mode : this . settings . mode ,
252+ contentTypes : this . contentTypes ,
253+ contentType
254+ } )
255+
241256 if ( this . draftCheck ( collection ) ) {
242257 this . contentModel . collections . push ( collection )
243258 }
@@ -247,7 +262,7 @@ class ContentModel {
247262 if ( this . matchers . assetsDirectory ( node ) ) {
248263 return this . contentModel . assets . push (
249264 ...node . children . map ( childNode => {
250- return new this . models . Asset ( childNode , context , {
265+ return new models . Asset ( childNode , context , {
251266 assetsDirectory : this . settings . assetsDirectory
252267 } )
253268 } )
@@ -256,7 +271,7 @@ class ContentModel {
256271
257272 if ( this . matchers . asset ( node ) ) {
258273 return this . contentModel . assets . push (
259- new this . models . Asset ( node , context , {
274+ new models . Asset ( node , context , {
260275 assetsDirectory : this . settings . assetsDirectory
261276 } )
262277 )
@@ -271,7 +286,7 @@ class ContentModel {
271286 linkEntries ( this . contentModel )
272287
273288 this . contentModel . collections . forEach ( collection => {
274- this . models . collection . afterEffects ( this . contentModel , collection )
289+ collection . afterEffects ( this . contentModel )
275290 } )
276291
277292 this . contentModel . subpages . forEach ( subpage => {
@@ -288,7 +303,7 @@ class ContentModel {
288303 render ( renderer ) {
289304 const renderHomepage = ( ) => {
290305 return this . contentModel . homepage . render ( renderer , {
291- contentModel : this . contentModel ,
306+ contentModel : ContentModel . serialize ( this . contentModel ) ,
292307 settings : this . settings ,
293308 debug : this . settings . debug
294309 } )
@@ -297,8 +312,8 @@ class ContentModel {
297312 const renderCollections = ( ) => {
298313 return Promise . all (
299314 this . contentModel . collections . map ( collection => {
300- return this . models . collection . render ( renderer , collection , {
301- contentModel : this . contentModel ,
315+ return collection . render ( renderer , {
316+ contentModel : ContentModel . serialize ( this . contentModel ) ,
302317 settings : this . settings ,
303318 debug : this . settings . debug
304319 } )
@@ -310,7 +325,7 @@ class ContentModel {
310325 return Promise . all (
311326 this . contentModel . subpages . map ( subpage => {
312327 return subpage . render ( renderer , {
313- contentModel : this . contentModel ,
328+ contentModel : ContentModel . serialize ( this . contentModel ) ,
314329 settings : this . settings ,
315330 debug : this . settings . debug
316331 } )
0 commit comments