Skip to content

Instantly share code, notes, and snippets.

View kmelve's full-sized avatar
💬
is typing

Knut Melvær kmelve

💬
is typing
View GitHub Profile
const axios = require("axios");
const sanityClient = require('@sanity/client')
const client = token => sanityClient({
projectId: '3do82whm',
dataset: 'production',
token
})
const NPM_API_SEARCH = "https://api.npms.io/v2/search/suggestions"
const NPM_API_PGKINFO = "https://api.npms.io/v2/package/"
@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.
//
@kmelve
kmelve / gist:687c729425439832ade18f02be1bcffd
Last active February 19, 2019 21:56
Write to Sanity from webtask.io/make
// Remember to add npm dependencies (click the 🔧)
const SanityClient = require('@sanity/client');
const client = token => SanityClient({
projectId: '<YouProjectId>',
dataset: 'production',
useCdn: false,
token
});
module.exports = async function(context, cb) {
@kmelve
kmelve / spotifyDropdown.js
Last active February 18, 2019 21:54
Rough Example of Custom spotify input
import PropTypes from 'prop-types'
import React from 'react'
import Fieldset from 'part:@sanity/components/fieldsets/default'
import { PatchEvent, setIfMissing, set } from 'part:@sanity/form-builder/patch-event'
import { FormBuilderInput } from 'part:@sanity/form-builder'
import { render } from 'react-dom'
import Downshift from 'downshift'
export default class CustomObjectInput extends React.PureComponent {
static propTypes = {
@kmelve
kmelve / sanityToMarkdownPosts.js
Created March 16, 2019 16:11
Convert a post type in Sanity.io to markdown files with frontmatter
/* eslint-disable */
const fs = require('fs')
const sanityClient = require('@sanity/client')
const groq = require('groq')
const BlocksToMarkdown = require('@sanity/block-content-to-markdown')
const config = { projectId: '<YourID>', dataset: '<DatasetName>', useCdn: true }
const client = sanityClient(config)
const serializers = {
@kmelve
kmelve / youtube.js
Created March 19, 2019 10:22
Simple implementation of a youtube embed field with a preview for Sanity.io
import React from 'react';
import getYouTubeID from 'get-youtube-id';
const YouTubePreview = ({ value }) => {
const id = getYouTubeID(value.url);
const url = `https://www.youtube.com/embed/${id}`;
if (!id) {
return <div>Missing YouTube URL</div>;
}
return (
@kmelve
kmelve / Bulk delete by field
Created March 20, 2019 08:16
Bulk deletion in Sanity.io with webtask.io/make
// Remember to add npm dependencies (click the 🔧)
const SanityClient = require('@sanity/client');
const client = token => SanityClient({
projectId: '<YourProjectID>',
dataset: 'production',
useCdn: false,
token
});
module.exports = async function(context, cb) {
@kmelve
kmelve / conditionalInput.js
Created March 27, 2019 15:58
Conditional fields via custom input component for Sanity.io
import PropTypes from 'prop-types'
import React from 'react'
import Fieldset from 'part:@sanity/components/fieldsets/default'
import {setIfMissing} from 'part:@sanity/form-builder/patch-event'
// FormBuilderInput automatically generates fields from a schema
import {FormBuilderInput} from 'part:@sanity/form-builder'
// a Higher Order Component that passes document values as props
import {withDocument} from 'part:@sanity/form-builder'
class CustomObjectInput extends React.PureComponent {
@kmelve
kmelve / gatsby-node.js
Last active May 23, 2019 10:32
Dated publishing in Gatsby and Sanity.io
const { isFuture, format } = require('date-fns')
async function createBlogPostPages(graphql, actions, reporter) {
const { createPage, createPageDependency } = actions
const result = await graphql(`
{
allSanityPost {
edges {
node {
id
@kmelve
kmelve / doc.json
Last active June 7, 2019 08:49
Character limit Portable Text (only top level)
{
"_createdAt": "2019-05-27T07:27:48Z",
"_id": "06b92a73-8502-45e8-ab8e-31b76181cf94",
"_rev": "CCW6uuhzMbOOH3oXLDotsE",
"_type": "post",
"_updatedAt": "2019-06-01T08:33:03Z",
"authors": [
{
"_key": "02bcfae524e0",
"_ref": "14165126-a0fd-4e5e-96af-686d36f408e0",