#content collection for images
7 messages · Page 1 of 1 (latest)
Hi @astral hatch. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To create a content collection for images in Astro, you would follow similar steps to creating any content collection. Here's a simplified step-by-step guide:
-
Create a new collection (folder) inside
src/content/. Let's call itimages/. -
Inside this
images/folder, you can add your image files. -
Create a
src/content/config.tsfile to define a schema for yourimagesCollection. The schema can be very simple, just validating the image file name and perhaps some metadata like alt text. Here's an example:
// Import utilities from `astro:content`
import { z, defineCollection } from "astro:content";
// Define a `type` and `schema` for each collection
const imagesCollection = defineCollection({
type: 'content',
schema: z.object({
filename: z.string(),
alt: z.string().optional(),
})
});
// Export a single `collections` object to register your collection(s)
export const collections = {
images: imagesCollection,
};
- After defining your schema, run
npx astro syncto update theastro:contentmodule for the Content Collections API.
Remember, the filename should match the name of the image file in your images/ collection, and alt is optional metadata you can include for each image.
For more detailed information, you can refer to the Astro documentation on Content Collections (https://docs.astro.build/en/guides/content-collections/#defining-a-collection-schema) and Astro documentation on Images in content collections (https://docs.astro.build/en/guides/images/#images-in-content-collections).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@astral hatch kapa.ai is still learning and improving, please let us know how it did by reacting below
@dark yoke can you elaborate more on
Remember, the filename should match the name of the image file in your images/ collection, and alt is optional metadata you can include for each image.
Where do I store the filenames?
Let me check the knowledge base for you. One moment! :books:
In Astro, the filenames of the images are stored as the names of the image files themselves in your images/ collection. For example, if you have an image named my-image.jpeg, you would store it in the src/content/images/ directory as my-image.jpeg.
The filename and alt fields in the schema are meant to be used in the frontmatter of Markdown or MDX files in your collection. For example, you might have a Markdown file for each image that looks like this:
---
filename: "my-image.jpeg"
alt: "A description of my image"
---
# My Image
In this case, filename should match the name of the image file in your images/ collection, and alt is optional metadata you can include for each image. The filename and alt fields are then validated by the schema you defined in your config.ts file.
Remember to run npx astro sync after adding new files to your collection to update the astro:content module for the Content Collections API.