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 / conditional-object-snippet1.js
Last active August 24, 2018 05:23
How to conditionally build an object in ES6 – 1
function episodeParser(data) {
const { id,
title,
description,
optionalField,
anotherOptionalField
} = data
const parsedEpisode = { guid: id, title, summary: description }
if (optionalField) {
parsedEpisode.optionalField = optionalField
function episodeParser({
id,
title,
description = 'No summary',
optionalField,
anotherOptionalField
}) {
return {
guid: id,
title,
const data = {
id: 1,
title: 'An episode',
description: 'An episode summary',
anotherOptionalField: 'some data'
}
episodeParser(data)
//> { guid: 1, title: 'An episode', summary: 'An episode summary', anotherOptionalField: 'some data' }
@kmelve
kmelve / sanity-blog-content-to-markdown.js
Created August 27, 2018 20:17
This is an example script on how to export content from the Sanity blog schema out to markdown.
/**
* This is an example script on how to export content from
* the blog schemas out to markdown.
*
* Run this script in a Sanity project folder with
* $ sanity exec markdown.js > blogposts.md
* on a public dataset, and
* $ sanity exec --with-user-token markdown.js > blogposts.md
* on a private dataset
*/
@kmelve
kmelve / webtask-sanity-blog-comment.js
Created September 25, 2018 06:00
Webtask.io Serverless Comments for Sanity.io based blog projects with SPAM protection
/*
* Webtask.io Serverless Comments for Sanity.io based blog projects with SPAM protection
*
* Get Akismet API token (free for non-commercial projects): https://akismet.com/development/
* Make an API token with write permissions in manage.sanity.io/projects/<ProjectId>
*
* Remember to add Secrets and NPM Modules
*/
@kmelve
kmelve / run.js
Created November 21, 2018 17:53
Indexing in Algolia using serverless functions (and observables!) https://www.sanity.io/blog/indexing-in-algolia-using-serverless-functions-and-observables
const algoliasearch = require('algoliasearch');
const request = require('request');
const ndjson = require('ndjson');
const {bindNodeCallback} = require('rxjs');
const {streamToRx} = require('rxjs-stream');
const {bufferCount, map, mergeMap, toArray, tap} = require('rxjs/operators');
// Algolia configuration
const algoliaApp = 'your_app_id';
@kmelve
kmelve / javascript.json
Last active June 28, 2019 04:10
Sanity Shema Snippets for VS Code
{
"sanity-schema": {
"prefix": "schema",
"body": [
"export default {\n name: '$1',\n type: '${2|document,object|}',\n title: '$1',\n fields: [\n $3\n ]\n}\n"
],
"description": "Makes a new Sanity schema"
},
"sanity-type": {
"prefix": "type",
@kmelve
kmelve / index.js
Created December 20, 2018 06:19
Sync npm plugin packages to Sanity using webtask.io
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 / EditorWithTableOfContents.jsx
Created January 27, 2019 10:23
A simple example of sanity.io’s Editor for Portable Text with a Table of Contents viewer
import React, { Fragment } from 'react'
import { BlockEditor } from 'part:@sanity/form-builder'
class EditorWithToC extends React.PureComponent {
generateToC = portableText => {
if (!portableText) { return [] }
const defaultHeadings = /h[1-4]/
const headings = portableText.filter(({ style }) => style.match(defaultHeadings))
return (
<Fragment>
@kmelve
kmelve / EditorWithTextAnalysis.jsx
Created January 27, 2019 10:24
An example of simple text analysis for Sanity.io’s editor for Portable Text
import React, { Fragment } from 'react'
import { BlockEditor } from 'part:@sanity/form-builder'
import calculateLix from 'lix-index'
const lixIllustration = lix => {
if (lix > 55) return '😭' // very difficult
if (lix > 45) return '😟' // difficult
if (lix > 35) return '😐' // medium
if (lix > 25) return '🙂' // easy
if (lix > 10) return '😃' // very easy