contentlayerdev / contentlayerdev/contentlayer

Add support for named scopes ("computed collection")

未关闭
#71 1 条评论 6 个 reaction 已指派 0 人 在 GitHub 查看
feature
主要语言
TypeScript
星标
3.5k
派生
192
PR 合并指标
30 天内没有已合并 PR

描述

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.)

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。