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

[Blog]: Improve tags field shape #32

Closed
melanieseltzer opened this issue Mar 22, 2023 · 0 comments · Fixed by #34
Closed

[Blog]: Improve tags field shape #32

melanieseltzer opened this issue Mar 22, 2023 · 0 comments · Fixed by #34
Labels
✨ enhancement New feature or request

Comments

@melanieseltzer
Copy link
Owner

melanieseltzer commented Mar 22, 2023

When defining tags in the mdx file, I want it to be as simple as typing their full names, like so:

tags:
  - React
  - Next.js
  - React Query

Currently, there's a kebabCase util that does the work of transforming it into a slug for the tag URL.

But this work occurs via the client selectors/components that are consuming the tags, and I would prefer that work be done already at the point of consumption by the component(s).

Contentlayer allows you to define a field and computed field with the same name, so I think this work can be done in the schema, something like this:

export const blogSchema = {
  fields: {
    tags: { type: 'list', required: true, of: { type: 'string' } },
  },
  computedFields: {
    tags: {
      type: 'list',
      resolve: doc => 
        doc.tags._array.map(tag => ({
          displayName: tag, // the tag name as defined in the mdx frontmatter
          slug: kebabCase(tag), // transform it to a slug
        })
      )
    },
  }
}

Ultimately, the array of objects in the computed field is what the component(s) will consume.

I think there might be a typing issue with this: contentlayerdev/contentlayer#398 but I think since it can be fixed on our end by omitting tags from the original type and re-typing it (in src/content/blog/types.ts).

@melanieseltzer melanieseltzer added the ✨ enhancement New feature or request label Mar 22, 2023
@melanieseltzer melanieseltzer changed the title [Blog]: Improve tags field and output [Blog]: Improve tags field shape Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant