You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Content Collections] Add slug frontmatter field (#5941)
* feat: respect `slug` frontmatter prop
* chore: replace `slug` check with proper types
* fix: regen types on `slug` change
* chore: add TODO on slug gen
* tests: update to use `slug` frontmatter prop
* chore: add error message on `slug` inside object schema
* lint
* chore: add note on frontmatter parse
* refactor: move content errors to new heading
* chore: ContentSchemaContainsSlugError
* chore: changeset
* docs: be 10% less gentle
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* fix: avoid parsing slug on unlink
* docs: clarify old API is for beta users
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Content collections: Introduce a new `slug` frontmatter field for overriding the generated slug. This replaces the previous `slug()` collection config option from Astro 1.X and the 2.0 beta.
6
+
7
+
When present in a Markdown or MDX file, this will override the generated slug for that entry.
8
+
9
+
```diff
10
+
# src/content/blog/post-1.md
11
+
---
12
+
title: Post 1
13
+
+ slug: post-1-custom-slug
14
+
---
15
+
```
16
+
17
+
Astro will respect this slug in the generated `slug` type and when using the `getEntryBySlug()` utility:
18
+
19
+
```astro
20
+
---
21
+
import { getEntryBySlug } from 'astro:content';
22
+
23
+
// Retrieve `src/content/blog/post-1.md` by slug with type safety
24
+
const post = await getEntryBySlug('blog', 'post-1-custom-slug');
25
+
---
26
+
```
27
+
28
+
#### Migration
29
+
30
+
If you relied on the `slug()` config option, you will need to move all custom slugs to `slug` frontmatter properties in each collection entry.
31
+
32
+
Additionally, Astro no longer allows `slug` as a collection schema property. This ensures Astro can manage the `slug` property for type generation and performance. Remove this property from your schema and any relevant `slug()` configuration:
* A content collection schema should not contain the `slug` field. This is reserved by Astro for generating entry slugs. Remove the `slug` field from your schema, or choose a different name.
639
+
*/
640
+
ContentSchemaContainsSlugError: {
641
+
title: 'Content Schema should not contain `slug`.',
642
+
code: 9003,
643
+
message: (collection: string)=>{
644
+
return`A content collection schema should not contain \`slug\` since it is reserved for slug generation. Remove this from your ${collection} collection schema.`;
645
+
},
646
+
hint: 'See https://docs.astro.build/en/guides/content-collections/ for more on the `slug` field.',
0 commit comments