Get a peek at our latest innovations at Sanity Product Day on Dec 8th β†’

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.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Free to get started, and pay-as-you-go on all plans. Find out more.