Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine fields and computedFields #91

Open
seancdavis opened this issue Jan 5, 2022 · 2 comments
Open

Combine fields and computedFields #91

seancdavis opened this issue Jan 5, 2022 · 2 comments
Labels
feature New feature or request pkg/source-files

Comments

@seancdavis
Copy link
Collaborator

Today we have fields and computedFields. The definitions in computedFields expects a function to which Contentlayer passes the processed document.

I'd rather be able to define all my fields in one place and allow the options of that field definition (presence of resolve) to determine whether or not it should be computed. Thus, I could have a doc type def like this:

const Page = defineDocumentType(() => ({
  // ...
  fields: {
    title: { type: 'string', required: true },
    url_path: {
      type: 'string',
      resolve: (doc) => doc._raw.flattenedPath.replace(/pages\/?/, ''),
    },
  },
}))

To do this and still support current use cases, it'd be beneficial to not just pass the processed doc object, but also input data. (See #76 for an example use case.) IOW the shape of the doc passed to the resolve function might include the following properties:

  • _raw would still be there as it is today
  • frontmatter would contain the raw frontmatter content
  • body (configurable) would contain the processed body
@seancdavis seancdavis added the feature New feature or request label Jan 5, 2022
@schickling
Copy link
Collaborator

Related to #76

@airtonix
Copy link

A few things the current format has going for it are:

  • simplicity: the type definitions for fields and computedFields is easier to manage as separate signatures, combining them smells like the poision of D.R.Y zealotry.
  • it's obvious what they do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request pkg/source-files
Projects
None yet
Development

No branches or pull requests

3 participants