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
@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'
@kmelve
kmelve / parseHTML.js
Created September 15, 2021 21:04
Deserializing Transcript HTML into Portable Text
const blockTools = require("@sanity/block-tools").default;
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const HTML_SPAN_TAGS = {
span: {object: 'text'},
}
const defaultSchema = require("./defaultSchema");
function tagName(el) {
@kmelve
kmelve / create-community-newsletter.js
Created December 8, 2020 23:33
Create Mailchimp campaign document action proof of concept
const fetch = require('got');
const client = require('../client');
const HTML = require('../lib/newsletter-template');
const MAILCHIMP_URL = 'https://us3.api.mailchimp.com/3.0/campaigns';
const API_TOKEN = process.env.MAILCHIMP_TOKEN;
const QUERY = `//groq
*[
@kmelve
kmelve / disable-js.html
Last active November 30, 2020 09:10
JavaScript-less no JS please. Inspired by https://heydonworks.com/
<html>
<head>
<title>No JS please</title>
</head>
<body>
<main>
<noscript>
Content!
<style>
@kmelve
kmelve / PTEditor.js
Last active November 25, 2020 13:12
Handle pasting of Github flavored markdown for the Portable Text array.
import React, { useState, forwardRef, Fragment } from 'react'
import { BlockEditor } from 'part:@sanity/form-builder'
import Switch from 'part:@sanity/components/toggles/switch'
import css from './PTeditor.module.css'
import { handlePaste } from './handlePaste'
function CustomEditor(props, ref){
const [customPaste, setCustomPaste] = useState(false)
function handleCustomPaste () {
@kmelve
kmelve / opt-out-for-gif.js
Created October 28, 2020 19:58
GIFs with Portable Text in Gatsby
import React from "react";
import Img from "gatsby-image";
import { getFluidGatsbyImage } from "gatsby-source-sanity";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { atomDark } from "react-syntax-highlighter/dist/esm/styles/prism";
import iconLinksList from "./iconLinksList.js";
import YouTube from "../components/YouTube.js";
import getYouTubeID from "get-youtube-id";
const sanityConfig = { projectId: "rx426fbd", dataset: "production" };
@kmelve
kmelve / GitHubSelector.module.css
Created October 21, 2020 10:25
Simple Github repo selector for Sanity Studio
@import 'part:@sanity/base/theme/variables-style';
.item {
padding: var(--small-padding);
}
.searchableSelect {
composes: spacing from 'part:@sanity/components/buttons/default-style';
grid-column: span 2;
@kmelve
kmelve / migrateValues.js
Created August 29, 2020 07:16
Migrate categories in an array field on sanity.io
/* eslint-disable no-console */
import client from 'part:@sanity/base/client'
// Run this script with: `sanity exec --with-user-token migrations/migrateValues.js`
//
// This example shows how you may write a migration script that migrates a field value
// on a specific document type.
// This will migrate documents in batches of 100 and continue patching until no more documents are
// returned from the query.
//
@kmelve
kmelve / BlockContent.js
Created May 14, 2020 07:45
Mininal exampe of how to Twitter embeds with Santiy.io
// Example of how to output this in a React frontend
import React from 'react'
import BlockContent from '@sanity/block-content-to-react'
import { TwitterTweetEmbed } from 'react-twitter-embed'
const Tweet = ({url}) => {
const exp = /\/status\/(\d+)($|[?/])/
const [, id] = exp.exec(url) || []
if (id) {
return <TwitterTweetEmbed className="sliderBoxes" tweetId={id} options={{conversation: 'none', 'hide-thread': true}} />
@kmelve
kmelve / import.js
Last active May 10, 2020 09:22
How to import documents, and add them as a new item in an array of references with Sanity.io
/**
* import.js
*
* Install dependencies:
* yarn add p-queue nanoid
* or
* npm i p-queue nanoid && sanity install
*
* Run this script with:
* sanity exec --with-user-token import.js