-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
✅ Deploy Preview for astro-docs-2 ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
There was a problem hiding this 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/>`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
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.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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} /> |
There was a problem hiding this comment.
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}
There was a problem hiding this comment.
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!
Thanks, Dan! This one's fine when you are happy with it, and have the updated info from Erika! 🙌 |
There was a problem hiding this 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! 🙌
What kind of changes does this PR include?
Description
ImageAsset
toImageMetadata
, which is the importable nameImageMetadata
in the content collections example so you don't get a red squiggly when refiningimg
src/assets
foldercoverAlt
frontmatter property because you can't use<Image/>
without alt text