Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Can I use Sanity's built-in slugify function in my custom function?

5 replies
Last updated: Apr 22, 2020
Does Sanity expose its built-in
function for import? I'm running some additional logic inside a custom
function for a slug field, but I'd like to ultimately use Sanity's out of the box function within my custom one.
Apr 21, 2020, 3:26 PM
Hi User, is this example helpful for you? It’s not an import but it goes in the same direction.
      name: 'nested',
      type: 'object',
      fields: [
          name: 'slugWithSlugify',
          type: 'slug',
          title: 'Custom slugify function',
          description: 'This is a slug field that should update according to current title',
          options: {
            source: 'title',
            maxLength: 96,
            slugify: (value, type) => {
              return encodeURI(`${type.name}_${value}`).toLocaleLowerCase()
Apr 21, 2020, 5:43 PM
Interesting...Thanks for chiming in! However, what I'm after is Sanity's default behavior (i.e. when no custom
is written) exposed as a function.
A simplified version of what I'm wanting to accomplish is construct the slug the same way Sanity normally would by default, but then add a slash to the beginning.

I've already written my own slugifier function that mimics Sanity's as closely as I could get it to...Just thought it'd be nice if the default slugify logic could be reused.
Apr 21, 2020, 6:02 PM
Agreed and understood 🙂 I don’t think there’s an available part that you can implement for this specifically. However, the
function is basically just this:
function defaultSlugify(value, type) {
  const maxLength = (type.options && type.options.maxLength) || 200
  const slugifyOpts = {truncate: maxLength, symbols: true}
  return value ? speakingurl(value, slugifyOpts) : ''
In other words, you could also make use of the
package: https://www.npmjs.com/package/speakingurl
Apr 21, 2020, 6:21 PM
Gotcha. I appreciate the help!
Apr 21, 2020, 6:22 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?