How to import Sanity's built-in slugify function?

9 replies
Last updated: Nov 29, 2025
Does Sanity expose its built-in
slugify
function for import? I'm running some additional logic inside a custom
slugify
function for a slug field, but I'd like to ultimately use Sanity's out of the box function within my custom one.
AI Update

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 speakingurl

Then 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 thread
9 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.

Was this answer helpful?