1- import { defaultsDeep } from 'lodash-es' ;
1+ import slugify from '@sindresorhus/slugify' ;
2+ import { defaultsDeep , merge } from 'lodash-es' ;
23import { read } from 'to-vfile' ;
34
4- import type { LoadedFlatbreadConfig , SourcePlugin } from '@flatbread/core' ;
5+ import type {
6+ CollectionEntry ,
7+ LoadedFlatbreadConfig ,
8+ SourcePlugin ,
9+ } from '@flatbread/core' ;
510import type { VFile } from 'vfile' ;
611import type {
712 FileNode ,
@@ -18,16 +23,27 @@ import gatherFileNodes from './utils/gatherFileNodes';
1823 * @returns An array of content nodes
1924 */
2025async function getNodesFromDirectory (
21- path : string ,
26+ collectionEntry : CollectionEntry ,
2227 config : InitializedSourceFilesystemConfig
2328) : Promise < VFile [ ] > {
2429 const { extensions } = config ;
25- const nodes : FileNode [ ] = await gatherFileNodes ( path , { extensions } ) ;
30+ const nodes : FileNode [ ] = await gatherFileNodes ( collectionEntry . path , {
31+ extensions,
32+ } ) ;
2633
2734 return Promise . all (
2835 nodes . map ( async ( node : FileNode ) : Promise < VFile > => {
2936 const file = await read ( node . path ) ;
30- file . data = node . data ;
37+ file . data = merge ( node . data , {
38+ _flatbread : {
39+ referenceField : collectionEntry . referenceField ,
40+ collection : collectionEntry . collection ,
41+ filename : file . basename ,
42+ path : file . path ,
43+ slug : slugify ( file . stem ?? '' ) ,
44+ } ,
45+ } ) ;
46+
3147 return file ;
3248 } )
3349 ) ;
@@ -40,16 +56,16 @@ async function getNodesFromDirectory(
4056 * @returns
4157 */
4258async function getAllNodes (
43- allContentTypes : Record < string , any > [ ] ,
59+ allCollectionEntries : CollectionEntry [ ] ,
4460 config : InitializedSourceFilesystemConfig
4561) : Promise < Record < string , VFile [ ] > > {
4662 const nodeEntries = await Promise . all (
47- allContentTypes . map (
63+ allCollectionEntries . map (
4864 async ( contentType ) : Promise < Record < string , any > > =>
4965 new Promise ( async ( res ) =>
5066 res ( [
5167 contentType . collection ,
52- await getNodesFromDirectory ( contentType . path , config ) ,
68+ await getNodesFromDirectory ( contentType , config ) ,
5369 ] )
5470 )
5571 )
@@ -76,9 +92,7 @@ const source: SourcePlugin = (sourceConfig?: sourceFilesystemConfig) => {
7692 const { extensions } = flatbreadConfig . loaded ;
7793 config = defaultsDeep ( sourceConfig ?? { } , { extensions } ) ;
7894 } ,
79- fetchByType : ( path : string ) => getNodesFromDirectory ( path , config ) ,
80- fetch : ( allContentTypes : Record < string , any > [ ] ) =>
81- getAllNodes ( allContentTypes , config ) ,
95+ fetch : ( content : CollectionEntry [ ] ) => getAllNodes ( content , config ) ,
8296 } ;
8397} ;
8498
0 commit comments