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

Assets: A more complete content collections example #2829

Merged
merged 4 commits into from
Mar 13, 2023
Merged

Conversation

Jutanium
Copy link
Contributor

@Jutanium Jutanium commented Mar 10, 2023

What kind of changes does this PR include?

  • New or updated content

Description

  • Change ImageAsset to ImageMetadata, which is the importable name
  • Use ImageMetadata in the content collections example so you don't get a red squiggly when refining img
  • Make it clear that you use images in frontmatter by declaring its path relative to the src/assets folder
  • Show an example Markdown frontmatter
  • Show example code that renders the image from the frontmatter
  • Add a coverAlt frontmatter property because you can't use <Image/> without alt text

@netlify
Copy link

netlify bot commented Mar 10, 2023

Deploy Preview for astro-docs-2 ready!

Name Link
🔨 Latest commit b34ce85
🔍 Latest deploy log https://app.netlify.com/sites/astro-docs-2/deploys/640f8dc9461b1c0008fea540
😎 Deploy Preview https://deploy-preview-2829--astro-docs-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this Dan! Love seeing the usage example! See below for just a couple of thoughts.


This is a blog post
```
A blog index page might render the cover photo and title of each blog post. This example destructures the image metadata as props to `<Image/>`:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A blog index page might render the cover photo and title of each blog post. This example destructures the image metadata as props to `<Image/>`:
Image metadata can then be passed to the `<Image />` component. Use spread notation to pass all properties from the `data` object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't named a data object, it's an object on the data object.

Suggested change
A blog index page might render the cover photo and title of each blog post. This example destructures the image metadata as props to `<Image/>`:
Image metadata can then be passed to the `<Image />` component. Use spread notation to pass all properties from the image metadata object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!


The image metadata will be transformed to match the signature of imported images, and therefore can be passed directly to the `<Image />` component or the `getImage()` function.
A new `image` helper for content collections lets you validate the image metadata using Zod.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to put this phrase, and the example of the schema, below the example for basic usage that's described above.

Right now this feels like:

  • paragraph explaining how to use it with frontmatter
  • note explaining the schema helper
  • example in a schema
  • example of basic usage

So maybe instead??

  • paragraph explaining how to use it with frontmatter
  • example of basic usage
  • note explaining the schema helper
  • example in a schema

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reorganized!

import { image, defineCollection, z } from "astro:content";
```ts title="src/content/config.ts"
import { defineCollection, z, image } from "astro:content";
import type { ImageMetadata } from "astro/dist/assets/types";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, this type being importable from there is great, but not reliable. I wouldn't necessarily suggest people to import it at the moment. I'll update the exports to export something better on Monday

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will wait on this, as without it the code yells at you!

{
allBlogPosts.map((post) => (
<div>
<Image {...post.data.cover} alt={post.data.coverAlt} />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work, you need to do src={post.data.cover}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does work, this is a copy-paste from my working setup!

@sarah11918
Copy link
Member

Thanks, Dan! This one's fine when you are happy with it, and have the updated info from Erika! 🙌

Copy link
Member

@yanthomasdev yanthomasdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also happy with it, LGTM if no more changes are needed. Nice work! 🙌

@sarah11918 sarah11918 added the improve documentation Enhance existing documentation (e.g. add an example, improve description) label Mar 13, 2023
@Jutanium Jutanium merged commit d7a57d8 into main Mar 13, 2023
@Jutanium Jutanium deleted the assets-cc-example branch March 13, 2023 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improve documentation Enhance existing documentation (e.g. add an example, improve description)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants