Just a collection of recipies / snippets / frequently asked questions about Sanity.
Note that these things are just meant as inspiration and is usually not documented. You'll usually have to copy + paste the code and possibly adjust it slightly to fit your use case. Some might be published as NPM modules at some point if it makes sense.
Sometimes you just want to skim over a whole bunch of Portable Text as text. This function does this, with some basic options for handling non-text nodes.
Someone wanted to convert "whatever" to «whatever» when rendering. convertQuotationMarks()
to the rescue!
When uploading images and files, sometimes you are left with assets that are no longer in use by any documents. In these cases, you may want to purge the unused assets to free up some space and clear the clutter. This will probably be a core part of the Sanity toolkit at some point, but until then, this script should help you!
A script which demonstrates how to upload multiple assets (images in this case) concurrently. Edit projectId, dataset and token. Also, edit the concurrency constant to change how many uploads run in parallel. To run the script: node snippets/uploadImagesConcurrently.js
Sometimes you want to change the name of a field. This function can be run with sanity exec renameField.js --with-user-credentials
and do a migration even while users are working.
The Sanity documentation contains several examples on how to create a Custom Input Component, but what if you want one which is also aware of language filter options? This component provides a good point of departure for making your own.
This custom input component shows an example of how to render a field conditionally of a field’s value in your document.
All code is MIT-licensed. See LICENSE.