Enhancement: Better content type creation for Astro #559

Closed
opened 2026-10-05 02:52:51 +02:00 by adam · 12 comments
Owner
          Nice, it's detecting content correctly. It doesn't generate appropriate types in some cases though. Below is zod configuration and resulting content types generated:
zod and resulting config type
const blogCollection = defineCollection({
  // Type-check frontmatter using a schema
  schema: z.object({
    title: z.string(),
    description: z.string(),
    heroImage: z.string().optional(),
    tags: z.array(z.string()).optional(),
    categories: z.array(z.string()).optional(),
    draft: z.boolean().optional().default(true),
    published: z
      .string().datetime({offset: true})
      .or(z.date())
      // Transform string to Date object
      .transform((val) => new Date(val)),
    modified: z
      .string().datetime({offset: true})
      .or(z.date())
      .optional()
      .transform((val) => (val ? new Date(val) : undefined)),
  })
});

Result content types:

"frontMatter.taxonomy.contentTypes": [
  {
    "name": "default",
    "pageBundle": false,
    "previewPath": null,
    "fields": [
      {
        "title": "Title",
        "name": "title",
        "type": "string"
      },
      {
        "title": "Description",
        "name": "description",
        "type": "string"
      },
      {
        "title": "Publishing date",
        "name": "date",
        "type": "datetime",
        "default": "{{now}}",
        "isPublishDate": true
      },
      {
        "title": "Content preview",
        "name": "preview",
        "type": "image"
      },
      {
        "title": "Is in draft",
        "name": "draft",
        "type": "draft"
      },
      {
        "title": "Tags",
        "name": "tags",
        "type": "tags"
      },
      {
        "title": "Categories",
        "name": "categories",
        "type": "categories"
      }
    ]
  },
  {
    "name": "blog",
    "previewPath": "'blog'",
    "pageBundle": false,
    "clearEmpty": true,
    "fields": [
      {
        "name": "title",
        "type": "string",
        "single": true,
        "required": true
      },
      {
        "name": "description",
        "type": "string",
        "single": true,
        "required": true
      },
      {
        "name": "heroImage",
        "type": "string",
        "single": true,
        "required": true,
        "default": true
      },
      {
        "name": "tags",
        "type": "list",
        "required": true,
        "default": true
      },
      {
        "name": "categories",
        "type": "list",
        "required": true,
        "default": true
      },
      {
        "name": "draft",
        "type": "boolean",
        "required": true,
        "default": true
      }
    ]
  }
]

In words:

  • possibly needles 'default' content type (created on "initialize project" stage)
  • optional from zod not respected, all fields are required (possibly messed up function definition/arguments order after some refactoring?)
  • all fields are default: true
  • the "Create content types" step doesn't get a checkmark even after marking all content collections (1 in my case)
  • tags, categories, heroImage, title, getting assigned improper types (I understand those types may not be always right, but some obvious heuristics could be put in place, some based on zods validation like .string().datetime(), some based on name)
  • some fields like published and modified are completely missing

Aside from that I would also think about the UX of the "use template" and "detect content" steps because using "blog template" — which looks like a required step — finalizes the whole configuration and opens the dashboard. I clicked it the first time thinking it would just append some defaults to configs and let me proceed with initialization, but it basically finished everything on it's own. Maybe make it the "step 0" where those buttons are on the very top.

Not sure if above comments belong to this thread or should be moved to separate issues though.

Originally posted by @T3sT3ro in https://github.com/estruyf/vscode-front-matter/issues/703#issuecomment-1804827033

Nice, it's detecting content correctly. It doesn't generate appropriate types in some cases though. Below is zod configuration and resulting content types generated: <details> <summary>zod and resulting config type</summary> ```ts const blogCollection = defineCollection({ // Type-check frontmatter using a schema schema: z.object({ title: z.string(), description: z.string(), heroImage: z.string().optional(), tags: z.array(z.string()).optional(), categories: z.array(z.string()).optional(), draft: z.boolean().optional().default(true), published: z .string().datetime({offset: true}) .or(z.date()) // Transform string to Date object .transform((val) => new Date(val)), modified: z .string().datetime({offset: true}) .or(z.date()) .optional() .transform((val) => (val ? new Date(val) : undefined)), }) }); ``` Result content types: ```json "frontMatter.taxonomy.contentTypes": [ { "name": "default", "pageBundle": false, "previewPath": null, "fields": [ { "title": "Title", "name": "title", "type": "string" }, { "title": "Description", "name": "description", "type": "string" }, { "title": "Publishing date", "name": "date", "type": "datetime", "default": "{{now}}", "isPublishDate": true }, { "title": "Content preview", "name": "preview", "type": "image" }, { "title": "Is in draft", "name": "draft", "type": "draft" }, { "title": "Tags", "name": "tags", "type": "tags" }, { "title": "Categories", "name": "categories", "type": "categories" } ] }, { "name": "blog", "previewPath": "'blog'", "pageBundle": false, "clearEmpty": true, "fields": [ { "name": "title", "type": "string", "single": true, "required": true }, { "name": "description", "type": "string", "single": true, "required": true }, { "name": "heroImage", "type": "string", "single": true, "required": true, "default": true }, { "name": "tags", "type": "list", "required": true, "default": true }, { "name": "categories", "type": "list", "required": true, "default": true }, { "name": "draft", "type": "boolean", "required": true, "default": true } ] } ] ``` </details> In words: - possibly needles 'default' content type (created on "initialize project" stage) - `optional` from zod not respected, all fields are `required` ([possibly messed up function definition/arguments order after some refactoring?](https://github.com/estruyf/vscode-front-matter/blob/8db76abf5133563fa2a580420ea09dde1903750a/ssg-scripts/astro.collections.mjs#L29C5-L29C5)) - all fields are `default: true` - the "Create content types" step doesn't get a checkmark even after marking all content collections (1 in my case) - `tags`, `categories`, `heroImage`, `title`, getting assigned improper types (I understand those types may not be always right, but some obvious heuristics could be put in place, some based on zods validation like `.string().datetime()`, some based on name) - some fields like `published` and `modified` are completely missing Aside from that I would also think about the UX of the "use template" and "detect content" steps because using "blog template" — which looks like a required step — finalizes the whole configuration and opens the dashboard. I clicked it the first time thinking it would just append some defaults to configs and let me proceed with initialization, but it basically finished everything on it's own. Maybe make it the "step 0" where those buttons are on the very top. Not sure if above comments belong to this thread or should be moved to separate issues though. _Originally posted by @T3sT3ro in https://github.com/estruyf/vscode-front-matter/issues/703#issuecomment-1804827033_ <!-- github-sync-id:1987957715 -->
adam closed this issue 2026-10-05 02:52:51 +02:00
Author
Owner
  • Zod optional
  • Default values
  • Better highlighting of which content types are already created for Astro
  • Verify types for fields
  • Missing fields
- [x] Zod optional - [x] Default values - [x] Better highlighting of which content types are already created for Astro - [x] Verify types for fields - [x] Missing fields <!-- github-comment-id:1806881953 -->
Author
Owner

@ T3sT3ro, I have been verifying against your Astro Collections, and it should now correctly create the content type.

Also added a small enhancement to show when a content type has not yet been created and when it is:

Screenshot 2023-11-11 at 10 46 16

When it is created:

Screenshot 2023-11-11 at 10 45 53

@ T3sT3ro, I have been verifying against your Astro Collections, and it should now correctly create the content type. Also added a small enhancement to show when a content type has not yet been created and when it is: ![Screenshot 2023-11-11 at 10 46 16](https://github.com/estruyf/vscode-front-matter/assets/2900833/12f11dcf-8002-4bc5-a4bc-76ab90f8342b) When it is created: ![Screenshot 2023-11-11 at 10 45 53](https://github.com/estruyf/vscode-front-matter/assets/2900833/169e8b86-7159-4011-927f-a0184a9c7edb) <!-- github-comment-id:1806896999 -->
Author
Owner

About the templates, they behave like a complete configuration for your site. I agree we can improve the UX on this one, but pushing it to the top would not be optimal as those templates are linked to certain frameworks/SSGs. Adding those to the top would always show them; right now, it is only shown when templates for a framework are available.

About the templates, they behave like a complete configuration for your site. I agree we can improve the UX on this one, but pushing it to the top would not be optimal as those templates are linked to certain frameworks/SSGs. Adding those to the top would always show them; right now, it is only shown when templates for a framework are available. <!-- github-comment-id:1806897610 -->
Author
Owner

I see a small typo on the screenshot:

Screenshot 2023-11-11 at 10 45 53

"The following astro content collections and (?) can be used to generate a content-type (types?)"

As for the templates, I think it doesn't have to be pushed to the top, but it would be good if either this step didn't close the configuration welcome screen or would clearly indicate that it will finalize configuration if you use the template. As for the frameworks step — does it have to be after the "initialize project" step, can't it be a part of it in a form of "choose a framework template or start with empty/generic template"?

I see a small typo on the screenshot: > ![Screenshot 2023-11-11 at 10 45 53](https://user-images.githubusercontent.com/2900833/282261549-169e8b86-7159-4011-927f-a0184a9c7edb.png) "The following astro content collections ___and (?)___ can be used to generate a content-type **(types?)**" As for the templates, I think it doesn't have to be pushed to the top, but it would be good if either this step didn't close the configuration welcome screen or would clearly indicate that it will finalize configuration if you use the template. As for the frameworks step — does it have to be after the "initialize project" step, can't it be a part of it in a form of "choose a framework template or start with empty/generic template"? <!-- github-comment-id:1806899515 -->
Author
Owner

Is this step supposed to leave the circle un-ticked even after the content types are generated?

Is this step supposed to leave the circle un-ticked even after the content types are generated? <!-- github-comment-id:1806902878 -->
Author
Owner

I see a small typo on the screenshot:

Will fix this.

Is this step supposed to leave the circle un-ticked even after the content types are generated?

What do you mean exactly?

> I see a small typo on the screenshot: Will fix this. > Is this step supposed to leave the circle un-ticked even after the content types are generated? What do you mean exactly? <!-- github-comment-id:1806903246 -->
Author
Owner

Is this step supposed to leave the circle un-ticked even after the content types are generated?

What do you mean exactly?

image

"Create content types" step has a single dot in circle, as if "not completed" but the "register content folders" has a ticked circle. Neither have any "(optional step)" annotation giving making an impression that the "create content types" step did not complete.

> > Is this step supposed to leave the circle un-ticked even after the content types are generated? > > What do you mean exactly? ![image](https://github.com/estruyf/vscode-front-matter/assets/5300963/dba6b77e-1d60-4197-a869-f35bcc7ffb0c) "Create content types" step has a single dot in circle, as if "not completed" but the "register content folders" has a ticked circle. Neither have any "(optional step)" annotation giving making an impression that the "create content types" step did not complete. <!-- github-comment-id:1806903908 -->
Author
Owner

That could be an enhancement indeed, but it is also intentional, as the Astro Content Type creation is optional in cases where it couldn't retrieve the content collection configuration.

That could be an enhancement indeed, but it is also intentional, as the Astro Content Type creation is optional in cases where it couldn't retrieve the content collection configuration. <!-- github-comment-id:1806904340 -->
Author
Owner

I stumbled upon some ideas for slightly improving generated field types:

  • draft field could generate with type draft instead of boolean
  • publish field (anything with /.*publish.*/i could have isPublishDate: true
  • modified/updated (some adequate regex) would get a isModifiedDate: true
  • first/only image field, e.g. hero/preview/image would get isPreviewImage: true

Finally, not sure if it would be actually useful or a needless fluff yet, but something like "review your configuration" at the end of initialization process that opens the config file.

I stumbled upon some ideas for slightly improving generated field types: - `draft` field could generate with type `draft` instead of `boolean` - `publish` field (anything with `/.*publish.*/i` could have `isPublishDate: true` - `modified`/`updated` (some adequate regex) would get a `isModifiedDate: true` - first/only image field, e.g. `hero`/`preview`/`image` would get `isPreviewImage: true` Finally, not sure if it would be actually useful or a needless fluff yet, but something like "review your configuration" at the end of initialization process that opens the config file. <!-- github-comment-id:1806906267 -->
Author
Owner
  1. I had changed this but used published instead of draft. A fix is coming up.
  2. Good suggestion
  3. Good suggestion
  4. Good suggestion

Finally, not sure if it would be actually useful or a needless fluff yet, but something like "review your configuration" at the end of initialization process that opens the config file.

Will think of how we can show this in the UI, or if you have an idea, feel free to share.

1. I had changed this but used `published` instead of `draft`. A fix is coming up. 2. Good suggestion 3. Good suggestion 4. Good suggestion > Finally, not sure if it would be actually useful or a needless fluff yet, but something like "review your configuration" at the end of initialization process that opens the config file. Will think of how we can show this in the UI, or if you have an idea, feel free to share. <!-- github-comment-id:1806912276 -->
Author
Owner

Another change coming up for the template section:

image

For the content collection section, if there are collections found, it won't be an optional section anymore:

image

Another change coming up for the template section: ![image](https://github.com/estruyf/vscode-front-matter/assets/2900833/832e2a23-4b8a-4f35-8b10-8c27ccedcaa7) For the content collection section, if there are collections found, it won't be an optional section anymore: ![image](https://github.com/estruyf/vscode-front-matter/assets/2900833/4d16cbce-d93c-414c-bbd4-5d09fc03d5b8) <!-- github-comment-id:1806917992 -->
Author
Owner

When you click the final action to show the dashboard, you will now get a notification which allows you to review your configuration.

Screenshot 2023-11-11 at 13 26 52

When you click the final action to show the dashboard, you will now get a notification which allows you to review your configuration. ![Screenshot 2023-11-11 at 13 26 52](https://github.com/estruyf/vscode-front-matter/assets/2900833/d4f43ede-01c7-4dd1-8e70-358723850df2) <!-- github-comment-id:1806921541 -->
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: adam/vscode-front-matter#559