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

feature request: possibility for datetime widgets to be typed as Date #15

Open
kjetilhartveit opened this issue Feb 27, 2023 · 2 comments

Comments

@kjetilhartveit
Copy link

kjetilhartveit commented Feb 27, 2023

When generating fields with the widget datetime I'd like them to be typed as Date because gray-matter automatically parses these fields to Date.

Example Netlify config.yml:

collections:
    - label: "Blog"
      name: "blog"
      fields:
          - {
                label: "Published date",
                name: "publishedDate",
                widget: "datetime",
                required: true,
            }

Generated types:

export interface Blog {
  publishedDate: string;
}

Desired generated type:

export interface Blog {
  publishedDate: Date;
}

My setup:

    "gray-matter": "^4.0.3",
    "netlify-ts": "^2.0.0",
@jsaari97
Copy link
Owner

jsaari97 commented Mar 1, 2023

I've thought about adding support for defining types for custom widgets, and in this case override existing widget types.

Regarding your issue, to get the correct types you could use the JSON_SCHEMA when parsing the markdown files, which parses dates into strings:

import yaml from "js-yaml";

matter(markdown, {
  engines: {
    yaml: (input) => yaml.load(input, { schema: yaml.JSON_SCHEMA }),
  },
});

@kjetilhartveit
Copy link
Author

Ah thanks, yeah that would work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants