-
Notifications
You must be signed in to change notification settings - Fork 194
Generated list of another document type as a field? #524
-
I'm trying to determine the viability of nesting documents with the current functionality within ContentLayer.
I have ContentType: Foo in content/foo/**, and ContentType: Bar in content/bar/**.
const Foo = defineDocumentType(() => ({ name: 'Foo', filePathPattern: `foo/**/*.mdx`, contentType: 'mdx', fields: { id: { type: 'string', required: true, }, name: { type: 'string', required: true, } // more fields }}) const Bar = defineDocumentType(() => ({ name: 'Bar', filePathPattern: `bar/**/*.mdx`, contentType: 'mdx', fields: { id: { type: 'string', required: true, }, name: { type: 'string', required: true, } // more fields }})
I'd like to have something like:
// Foo object { id: 'foo1', name: 'Foo1', // ... more fields // List of bars from content/bar/[id_of_foo] bars: [ { id: 'bar1', name: 'Bar1' }, { id: 'bar2', name: 'Bar2' }, // ... more fields ] }
But I specifically don't want to have to handwrite a list of all the bar's that exist; I want that to be generated from the content/bar/[id_of_foo]/** directory.
I'm aware of the reference type:
const Bar = defineNestedType(() => ({ name: 'Bar', filePathPattern: 'content/bar/*.md', fields: { title: { type: 'string', required: true }, }, }))
But this outputs a list of strings, not a list of objects.
I can do things post-Contentlayer generation, but ideally, I'd rather have Contentlayer do the whole thing.
Is this currently possible, either via a native field type or by some calculated field type shenanigans? Apologies if this has been asked already; I searched through the discussions and the issues and didn't see anything obvious.
Beta Was this translation helpful? Give feedback.