agentleFS
Lexington-ThemesLexington-Themes/astro-skills/skills/astro-content-collections/SKILL.md

astro-content-collections

Skill2 starsChanged 2 months agoMITView on GitHub

Working with Astro content collections, covering schemas with Zod, the glob loader, image() fields, querying, drafts, and rendering. Use when adding or editing content entries, changing a collection schema, or building pages from collection data.

1 other project uses a version of this.

Adopted by

Most recently taken up by

  • Other versions1 project uses a version of this.

    Word for word · 0

    None found.

    Edited · 0

    None found.

    Wrote their own version · 1

    1. zerocms-io/astro-content-collections★ 2same name

    How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.

  • In this repository2 other files it gives its agents.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Import Zod from astro/zod, per the docs.

    line 44
  2. Distinctive:

    Never invent frontmatter fields: unknown fields are either rejected by a strict schema or silently dropped, and both are bugs.

    line 71
  3. Distinctive:

    Keep frontmatter values consistent with the collection's existing style (date formats, tag casing, image paths) rather than introducing …

    line 73
  4. Very specific:

    Every getCollection() call site must apply the filter, including RSS feeds and sitemaps, which are the classic leak (field convention).

    line 86

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Astro content collections

How to define, edit, and query content collections without breaking the build. Each rule either follows the official content collections docs (linked) or is a field convention from maintaining large content sites, marked as such.

Where collections live

  • Collections are defined in src/content.config.ts at the project root of src/ (older projects: src/content/config.ts). One defineCollection() per collection, exported in a single collections object.
  • The standard local setup uses the glob() loader with a base directory and file pattern:

    import { defineCollection } from "astro:content";
    import { glob } from "astro/loaders";
    import { z } from "astro/zod";
    
    const posts = defineCollection({
      loader: glob({ base: "./src/content/posts", pattern: "**/*.{md,mdx}" }),
      schema: z.object({
        title: z.string(),
        description: z.string(),
        pubDate: z.coerce.date(),
      }),
    });
    
    export const collections = { posts };
  • Entry ids are generated from filenames (URL-friendly, lowercased). An entry can override its own id with a slug frontmatter property.

Schema rules

  • Import Zod from astro/zod, per the docs.
  • Dates in frontmatter arrive as strings or dates depending on quoting, so use z.coerce.date() to handle both.
  • Image fields use the image() helper, not z.string():

    schema: ({ image }) =>
      z.object({
        cover: image(),
      }),

    The field then resolves to an ImageMetadata object with real dimensions. Pass it straight to <Image /> from astro:assets; never treat it as a string path. (Note: image().refine() custom checks are unsupported.)

  • Link entries across collections with reference("collection-name") instead of storing raw ids in strings.
  • Changing a schema is a three-part task (field convention): update the schema, update every existing entry that no longer validates, and update any AGENTS.md or docs describing the frontmatter. A schema change that only touches the config file is almost certainly incomplete.

Editing and adding entries

Field conventions:

  • New entries start as a copy of an existing entry's frontmatter, then edit. Never invent frontmatter fields: unknown fields are either rejected by a strict schema or silently dropped, and both are bugs.
  • Keep frontmatter values consistent with the collection's existing style (date formats, tag casing, image paths) rather than introducing variants.

Drafts

Two working patterns; check which one the project already uses:

  • A draft boolean in the schema, filtered at query time, per the docs:

    const published = await getCollection("posts", ({ data }) => !data.draft);

    Every getCollection() call site must apply the filter, including RSS feeds and sitemaps, which are the classic leak (field convention).

  • Underscore-prefixed filenames excluded by the loader pattern (field convention: one exclusion point, no filters to forget):

    loader: glob({ base: "./src/content/posts", pattern: ["**/*.md", "!**/_*"] }),

Querying and rendering

  • getCollection() returns entries in non-deterministic order, per the docs. Anything user-facing sorts explicitly:

    const posts = (await getCollection("posts")).sort(
      (a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
    );
  • Render Markdown/MDX bodies with render() from astro:content:

    ---
    import { getEntry, render } from "astro:content";
    const entry = await getEntry("posts", Astro.params.slug);
    if (!entry) return Astro.redirect("/404");
    const { Content, headings } = await render(entry);
    ---
    <Content />
  • Build routes from entries with getStaticPaths() mapping over getCollection(), using post.id for the URL param.

Before finishing

  • No invented frontmatter fields; new entries validate against the schema
  • Image fields use image() and are passed as metadata, not strings
  • Every user-facing getCollection() call sorts explicitly
  • Draft filtering applied everywhere entries are listed (pages, RSS, sitemap)
  • Schema changes shipped together with entry and docs updates
  • astro build passes (schema errors surface at build time)

Maintained by Michael Andreuzza at Lexington Themes: Astro + Tailwind themes that ship with AGENTS.md, scoped rules, and design skills out of the box.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.