Sanity has a slug type. In Sanity, you'd define this in /schemas/schema.js
. A basic implementation would like like this simple blog post schema below:
{
title: "Blog",
name: "blog",
type: "document",
fields: [
{
title: "Title",
name: "title",
type: "string"
},
{
title: "Slug",
name: "slug",
type: "slug",
options: {
source: "title"
}
},
{
title: "Body",
name: "body",
type: "text"
}
]
}
The source
option defines what field Sanity will use to generate the slug. As you can see below, the slug isn't auto-generated from the title
field, but, instead, will generate when a user clicks on the "generate" button.
Sanity also provides a number of different customization options. For example, the slugify
option lets you specify a custom function for converting the source into a slug and the isUnique
option lets you supply a custom function to determine if the slug value is unique. For more details on those, check out Sanity's documentation.