# Quick start: Astro

> Add Editlet to an Astro site, read entries as content collections and edit your first entry with ?editlet.

Follow these steps to add Editlet to an Astro site (`astro ^6 || ^7`) and edit an entry in `astro dev`. Your entries
become Astro content collections.

## Install with your agent

Give your coding agent the prompt from [Quick start: Next.js](https://editlet.gunkin.dev/docs/quick-start-next.md). The same skill sets up Astro.

## Install by hand

1. Install the package and link the [agent skills](https://editlet.gunkin.dev/docs/agent-skills.md):

   ```bash
   npm install editlet
   node node_modules/editlet/skills/editlet/scripts/link.mjs --hooks
   ```

2. Add the integration. It adds the `?editlet` loader to every page. `astro dev` serves the editor at `/editlet/` and
   the dev file API at `/__editlet`, and `astro build` copies the editor into the output.

   ```js
   // astro.config.mjs
   import editlet from 'editlet/astro';
   import { defineConfig } from 'astro/config';

   export default defineConfig({ integrations: [editlet()] });
   ```

3. Create `cms/schema.json` and one entry, as in steps 5 and 6 of [Quick start: Next.js](https://editlet.gunkin.dev/docs/quick-start-next.md).

4. Create `src/content.config.ts`. It makes one Astro collection per schema collection.

   ```ts
   import { editletCollections } from 'editlet/astro/content';

   export const collections = editletCollections();
   ```

5. Run `npm run dev` and open `http://localhost:4321/?editlet`. In dev, a save changes the local file.

To keep your own collections too, add each Editlet one with `editletLoader`, and give it no `schema`:

```ts
import { defineCollection } from 'astro:content';
import { editletLoader } from 'editlet/astro/content';

export const collections = {
  post: defineCollection({ loader: editletLoader('post') }),
  // your other collections
};
```

## Show content on a page

```astro
---
import { getCollection, getEntries, getEntry, render } from 'astro:content';
import { Image } from 'astro:assets';

const home = await getEntry('home', 'index');        // a singleton: the id is 'index'
const posts = await getCollection('post');           // the id is the slug
const author = await getEntry(posts[0].data.author); // a reference → that entry
const related = await getEntries(posts[0].data.related ?? []); // a list of references; one call per level
const { Content } = await render(posts[0]);          // the Markdown body
---
{posts[0].data.cover && <Image src={posts[0].data.cover.src} alt={posts[0].data.cover.alt} />}
<Content />
```

Mark the element that shows an entry with `data-editlet-entry="post/hello-world"`. The editor can then point at it,
and you can select words in it and press **Edit**.

Tell the schema which page shows an entry: `"url": "/posts/{slug}/"` on the collection, matching your route
`src/pages/posts/[slug].astro` and the trailing `/` of your links. The editor then shows the page's address in *New
entry* and *Details*, and **⋯ → Open on the page** goes there. Add `"pluralLabel": "Posts"` for the list heading. See
[Content model](https://editlet.gunkin.dev/docs/content-model.md#page-urls).

## Options

`editlet()` takes `schemaPath` (default `cms/schema.json`, relative to the Astro root) and `admin`, the settings for
the [live site](https://editlet.gunkin.dev/docs/dev-and-production.md).

## Limits

- Astro `base` is not supported. The editor is always at `/editlet/` and the dev API at `/__editlet`.
- `astro build` leaves out `draft: true` entries, and a reference to a draft resolves to `undefined`.
- A static site has no server, so the [auth handler](https://editlet.gunkin.dev/docs/auth-handler.md) for GitHub sign-in runs elsewhere.
