Does Sanity expose its built-in slugify function for import?
slugifyfunction for import? I'm running some additional logic inside a custom
slugifyfunction for a slug field, but I'd like to ultimately use Sanity's out of the box function within my custom one.
Sanity doesn't officially export its built-in defaultSlugify function for import, but you can easily replicate it in your custom function. According to a community answer, Sanity's default slugification is just a thin wrapper around the speakingurl package.
Here's what the default implementation looks like:
function defaultSlugify(value, type) {
const maxLength = (type.options && type.options.maxLength) || 200
const slugifyOpts = {truncate: maxLength, symbols: true}
return value ? speakingurl(value, slugifyOpts) : ''
}So if you want to add custom logic while still using Sanity's default behavior, you can install speakingurl 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',
maxLength: 96,
slugify: (value, type) => {
const maxLength = (type.options && type.options.maxLength) || 200
const slugifyOpts = {truncate: maxLength, symbols: true}
const slug = value ? speakingurl(value, slugifyOpts) : ''
// Add your custom logic here
return `/${slug}` // e.g., add a leading slash
}
}
}This gives you the exact same slugification behavior as Sanity's default, with the flexibility to add your own transformations before or after.
Show original thread5 replies
Was this answer helpful?
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.