Skip to content

Instantly share code, notes, and snippets.

Avatar
💬
is typing

Knut Melvær kmelve

💬
is typing
View GitHub Profile
@kmelve
kmelve / disable-js.html
Last active Nov 30, 2020
JavaScript-less no JS please. Inspired by https://heydonworks.com/
View disable-js.html
<html>
<head>
<title>No JS please</title>
</head>
<body>
<main>
<noscript>
Content!
<style>
@kmelve
kmelve / PTEditor.js
Last active Nov 25, 2020
Handle pasting of Github flavored markdown for the Portable Text array.
View PTEditor.js
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 Oct 28, 2020
GIFs with Portable Text in Gatsby
View opt-out-for-gif.js
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 Oct 21, 2020
Simple Github repo selector for Sanity Studio
View GitHubSelector.module.css
@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 Aug 29, 2020
Migrate categories in an array field on sanity.io
View migrateValues.js
/* 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
Mininal exampe of how to Twitter embeds with Santiy.io
View BlockContent.js
// 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
How to import documents, and add them as a new item in an array of references with Sanity.io
View import.js
/**
* 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
@kmelve
kmelve / entries-by-date-this-year.js
Created May 9, 2020
Structure definition for listing entries by date of the current year for Sanity Studio
View entries-by-date-this-year.js
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 / gallery.js
Created May 9, 2020
A simple gallery type for Sanity.io
View gallery.js
@kmelve
kmelve / initialValueTemplates.js
Created Apr 30, 2020
Initial Value Templates Commynity Digest Example for Sanity.io
View initialValueTemplates.js
import T from '@sanity/base/initial-value-template-builder'
import client from 'part:@sanity/base/client'
const getNextDigestTitle = async () => {
const titles = await client.fetch(`*[
_type == "post" &&
title match "Community Digest*"
].title
`)
You can’t perform that action at this time.