✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

How to automatically generate slugs for a large set of articles?

6 replies
Last updated: Oct 1, 2021
I have tons of articles, but they don't have slugs. I've now added a slug field to the schema, but it looks like I have to manually go in to every article, click generate, and then publish.
Is there a quicker way?
😄
Oct 1, 2021, 10:57 AM
Make a script that does it? You can adapt this and run
slugify
or something to set
slug.current
?
Oct 1, 2021, 12:32 PM
const buildPatches = docs =>
  docs.map(doc => ({
    id: doc._id,
    patch: {
      set: {'slug.current': slugify(doc.title)},
      // ifRevisionID: doc._rev
    }
  }))

Oct 1, 2021, 12:32 PM
could work! where can I read up on running these kinds of scripts?
Oct 1, 2021, 12:33 PM
sanity exec --with-user-token slug.js
Oct 1, 2021, 12:34 PM
ah great! thanks 🙂
Oct 1, 2021, 12:40 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?