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 / customNumber.js
Last active December 1, 2020 14:31
Custom Number Select input component for Sanity.io
import CustomNumberSelect from './customNumberSelect'
export default {
name: 'sizes',
type: 'number',
inputComponent: CustomNumber,
options: {
list: [
{title: 'xsmall', value: 1},
{title: 'small', value: 2},
@kmelve
kmelve / gatsby-config.js
Last active January 9, 2020 21:53
RSS feed in Gatsby with Sanity as source
// Load variables from `.env` as soon as possible
require('dotenv').config({
path: `.env.${process.env.NODE_ENV || 'development'}`
})
const fetch = require('isomorphic-fetch')
const { createHttpLink } = require('apollo-link-http')
const PortableText = require('@sanity/block-content-to-html')
const imageUrlBuilder = require('@sanity/image-url')
@kmelve
kmelve / serverless-translate.js
Created September 16, 2019 14:45
Translate all fields that starts with `locale<Name>` in a Sanity dataset.
const sanityClient = require('@sanity/client')
const reduce = require('json-reduce').default
const Translate = require('@google-cloud/translate')
const DATASET = 'production'
const SUPPORTED_LANGUAGES = [
{id: 'en', title: 'English', isDefault: true, googleTranslateCode: 'en'},
{id: 'de', title: 'German (Deutsch)', googleTranslateCode: 'de'},
{id: 'jp', title: 'Japanese (日本語)', googleTranslateCode: 'ja'}
]
@kmelve
kmelve / BlockEditor.js
Created September 14, 2019 11:56
Custom portable text / block editor for Sanity with markdown paste and stats
import React, { Component, Fragment } from 'react'
import { BlockEditor } from 'part:@sanity/form-builder'
import Switch from 'part:@sanity/components/toggles/switch'
import css from './BlockEditor.module.css'
import { handlePaste } from './handlePaste'
export default class CustomEditor extends Component {
state = {
customPaste: false
}
@kmelve
kmelve / [slug].js
Last active August 19, 2019 20:14
Sapper-template with mainImage
<script context="module">
import blocksToHtml from '@sanity/block-content-to-html'
import urlBuilder from '@sanity/image-url'
import client from '../../sanityClient'
import serializers from '../../components/serializers'
const urlFor = source => urlBuilder(client).image(source);
export async function preload({ params }) {
// the `slug` parameter is available because
@kmelve
kmelve / InputPicker.js
Last active August 16, 2024 21:15
CUSTOM SANITY STUDIO INPUT COMPONENT FOR FONT-AWESOME
/**
* CUSTOM SANITY STUDIO INPUT COMPONENT FOR FONT-AWESOME
*
* This is a proof of concept custom input component that loads the whole free library of font awesome icons and makes them
* searchable in a downshift component. If they're selected the font name will be saved as a string on the document.
*
* Install dependencies in your studio folder with:
* yarn add @fortawesome/fontawesome-svg-core @fortawesome/react-fontawesome @fortawesome/free-solid-svg-icons downshift
*
* Use as a custom input component in your shcema:
@kmelve
kmelve / CleanSomeLyrics.js
Last active July 18, 2019 17:49
Naively clean chords from Jonathan Mann lyrics (https://twitter.com/songadaymann/status/1151885756237004805)
#!/usr/bin/env node
// require some file system methods
const fs = require('fs')
(() => {
// read the file with the lyrics++
const lyrics = fs.readFileSync('./lyrics.txt', 'utf-8')
// clean out things we don't want
const withoutChordsAndStuff = lyrics
@kmelve
kmelve / prepareStudio.js
Created July 11, 2019 11:16
Script to edit brand specific overrides for dataset and css overrides for Sanity studio
#!/usr/bin/env node
const editJsonFile = require('edit-json-file');
const stylePath = 'part:@sanity/base/theme/variables/override-style';
const folder = `${__dirname}/studio/`;
const file = editJsonFile(`${folder}sanity.json`);
const prepareFile = ({ ENVIRONMENT, BRAND }) => {
const parts = file.get('parts');
@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",
@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