Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Generated list of another document type as a field? #524

Unanswered
iamnbutler asked this question in Q&A
Discussion options

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.

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /