contentlayerdev / contentlayerdev/contentlayer

Add support for named scopes ("computed collection")

Open
#71 1 comment 6 reactions 0 assignees View on GitHub
feature
Dominant language
TypeScript
Stars
3.5k
Forks
192
PR merge metrics
No merged PRs in 30d

Description

Defining a document type brings the entirety of content for that document into the data object that we import into pages. I'm potentially importing much more information than I need to work with. It also forces me to write filtering logic in the template. And while this is a fairly typical pattern with Next.js, it's not a pleasant developer experience.

Proposal is to allow for named scopes when defining a document type. Something like this:

```js
const Doc = defineDocumentType(() => ({
name: "Doc",
// ...
fields: { /* ... */ },
scopes: [
{
name: "mostRecent",
resolve: (docs) => /* sorting/filtering logic ... */
}
]
))
```

I don't have a strong opinion on how this would then be used, but maybe something like this:

```js
import { allDocs, mostRecentDocs } from ".contentlayer/data"
```

The key decision here is finding a balance between being too magical (appending "Docs" to the scope name) and having to validate that scopes across all types be unique.

(Inpsired from a conversation with @smnh.)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.