Skip to content

Instantly share code, notes, and snippets.

View geball's full-sized avatar

Geoff Ball geball

View GitHub Profile
@SimeonGriggs
SimeonGriggs / Dereference.tsx
Created December 16, 2022 15:42
Sanity Studio v3 Document Action to "de-reference" a document by patching all documents that reference the current one. Incredibly dangerous.
import {useState, useEffect, useCallback} from 'react'
import {useToast, Card, Button, Stack, Text, Code} from '@sanity/ui'
import {extractWithPath} from '@sanity/mutator'
import {
Preview,
DocumentActionProps,
SanityDocument,
useClient,
useSchema,
pathToString,
@kmelve
kmelve / blurhash.js
Last active July 22, 2023 14:29
Retroactively add Blurhash strings to image assets in your Sanity Content Lake.
/**
* Retroactively add Blurhash strings to image assets in your Sanity Content Lake.
* 1. yarn add got sharp blurhash
* 2. run sanity exec blurhash --with-user-token
* 3. repeat (patches 100 assets in 1 transaction pr run)
*
* Some images might take a while to process.
*/
import client from 'part:@sanity/base/client'
import got from 'got'
@SimeonGriggs
SimeonGriggs / postType.ts
Last active July 14, 2023 14:42
Sanity.io: A slug field with a prefix based on a "category" reference's slug
import {SlugSourceFn, SlugifierFn, defineField, defineType} from 'sanity'
import get from 'lodash/get'
// Fetch the category's slug – if present – and append the post's title
const handleSlugSource: SlugSourceFn = async (doc, context) => {
const categoryRef = get(doc, `category._ref`)
if (categoryRef) {
const client = context.getClient({apiVersion: `2023-07-01`})
const categorySlug = await client.fetch(`*[_id == $id][0].slug.current`, {id: categoryRef})
import React, { useRef } from 'react'
import { map } from 'rxjs/operators'
import { createWeightedSearch } from 'part:@sanity/base/search/weighted'
import client from 'part:@sanity/base/client'
import ExternalReferenceInput from './ExternalReferenceInput'
const ExternalReference = props => {
const { type } = props
const { options } = type
const { dataset } = options
@kmelve
kmelve / promote-drafts.js
Created February 12, 2019 21:38
Sanity: Promote drafts
/* eslint-disable no-console */
import client from 'part:@sanity/base/client'
// Run this script with: `sanity exec --with-user-token migrations/renameField.js`
//
// This example shows how you may write a migration script that sets all drafts to published version
// and deletes the draft version
// This will migrate documents in batches of 100 and continue patching until no more documents are
// returned from the query.
//