How to import Sanity's built-in slugify function?
No, Sanity doesn't expose its built-in slugify function as an importable module. However, the good news is that the default implementation is straightforward and uses a well-known package under the hood.
According to a response from the Sanity team, the defaultSlugify function is essentially this:
function defaultSlugify(value, type) {
const maxLength = (type.options && type.options.maxLength) || 200
const slugifyOpts = {truncate: maxLength, symbols: true}
return value ? speakingurl(value, slugifyOpts) : ''
}So Sanity uses the speakingurl package internally. You can install it yourself and use it in your custom slugify function:
npm install speakingurlThen in your schema:
import speakingurl from 'speakingurl'
{
name: 'slug',
type: 'slug',
options: {
source: 'title',
slugify: (value, type) => {
// Your custom logic here
const prefix = '/'
// Use Sanity's default slugification
const maxLength = (type.options && type.options.maxLength) || 200
const slugifyOpts = {truncate: maxLength, symbols: true}
const slug = value ? speakingurl(value, slugifyOpts) : ''
return `${prefix}${slug}`
}
}
}This approach lets you leverage the same slugification logic Sanity uses by default while adding your own customizations around it. The speakingurl package handles URL-friendly formatting, special character removal, and other typical slug transformations.
Show original thread9 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.