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 / stoppord_no
Created February 7, 2014 19:18
Norske stoppord
å
alle
andre
at
av
både
båe
bare
begge
ble
@kmelve
kmelve / BlockEditor.ts
Created May 24, 2024 11:10
Markdown to Portable Text paste handler
import { toPlainText } from "@portabletext/react"
import { BlockEditor as DefaultBlockEditor } from "sanity"
import { handlePaste } from "~/studio/components/blockEditor/handlePaste"
const wordsPerMinute = 200
export default function BlockEditor(props: any, ref) {
const value = props.value ?? []
const plainText = toPlainText(value)
const characterCount = plainText.length
@kmelve
kmelve / entries-by-date-this-year.js
Created May 9, 2020 18:23
Structure definition for listing entries by date of the current year for Sanity Studio
import S from '@sanity/desk-tool/structure-builder'
import eachDayOfInterval from 'date-fns/eachDayOfInterval'
import startOfYear from 'date-fns/startOfYear'
import startOfDay from 'date-fns/startOfDay'
import endOfDay from 'date-fns/endOfDay'
let now = new Date()
export default () => S.listItem()
.title('Entries by day this year')
.child(
@kmelve
kmelve / LinkedInEmbedInput.tsx
Last active September 18, 2024 15:19
LinkedIn Embed Input for Sanity Studio
import React, { useState } from "react"
import { TextInput, Stack } from "@sanity/ui"
import { set, unset } from "sanity"
interface LinkedInEmbed {
_key: string
_type: string
postUrl: string
height: number
}
@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 / 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 / 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 / page.tsx
Created May 17, 2024 17:39
Minimal totally-not-ready-for-prod Todo list example using Sanity Live Content API and Next.js Server Actions
import { redirect } from "next/navigation"
import { createClient } from "next-sanity"
const client = createClient({
apiVersion: "vX",
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
token: process.env.SANITY_WRITE_TOKEN,
useCdn: true,
})
@kmelve
kmelve / department.js
Created April 15, 2020 07:35
Example of listing documents in Sanity Studio‘s desk structure with real-time listener
export default {
name: 'department',
type: 'document',
title: 'Department',
fields: [
{
name: 'title',
type: 'string',
title: 'Title',
},
@kmelve
kmelve / schemas.js
Last active February 23, 2024 01:27
Super simple galleries on Sanity.io
/*
$ sanity init
# Follow steps, choose the clean template
# go to schemas/schema.js and paste this in
*/
// First, we must import the schema creator
import createSchema from 'part:@sanity/base/schema-creator'