Skip to content

Instantly share code, notes, and snippets.

@jbmoelker
jbmoelker / .eleventy.js
Created January 23, 2021 17:53
GraphQL extension for Eleventy
const queryDatoGraphQL = require('./query-dato-graphql.js');
module.exports = function (config) {
// Custom data file formats (https://www.11ty.dev/docs/data-custom/)
config.addDataExtension(
'graphql',
async (query) => await queryDatoGraphQL({ query })
);
// Base Config
/**
* Nuxt2 DatoCMS Plugin
*
* features:
* - $datocms.fetchData: lightweight isomorphic data fetching of GraphQL queries
* - $datocms.subscribeToData: real-time data updates using GraphQL queries
* - $datocms.state: { error, status, statusMessage } for data subscription
* - $datocms.toHead: alias for vue-datocms' toHead method
* - <DatocmsImage>: global alias for vue-datocms's Image component
* - <DatocmsStructuredText>: global alias for vue-datocms's StructuredText component
<template>
<ClientOnly v-if="$nuxt.isPreview">
<div role="alert">
<span>Preview mode: {{ statusMessage }}</span>
<button type="button" @click="exitPreview">
Exit preview
</button>
</div>
</ClientOnly>
</template>
@jbmoelker
jbmoelker / svg2gif.js
Created April 5, 2016 07:49
[POC] Animated SVG to GIF
'use strict';
/**
* Script converts an animated SVG (`fileIn`) into an animated GIF (`fileOut`)
* with the given `duration`, `fps`, `width` and `height.
* This is just a proof-of-concept. Script needs an API etc etc.
*/
// config which should come from cli args
const fileIn = 'animation.svg';
@jbmoelker
jbmoelker / nunjucks-filter-limit-to.js
Created March 21, 2014 19:12
Nunjucks limitTo filter. Creates a new array or string containing only a specified number of elements. The elements are taken from either the beginning or the end of the source array or string, as specified by the value and sign (positive or negative) of limit.
/**
* @module limitTo
*
* Creates a new array or string containing only a specified number of elements.
* The elements are taken from either the beginning or the end of the source array
* or string, as specified by the value and sign (positive or negative) of limit.
*
* Same behavior as AngularJS limitTo filter: http://docs.angularjs.org/api/ng/filter/limitTo
*
* To use this filter in a template, first register it in the Nunjucks environment:
@jbmoelker
jbmoelker / README.md
Created March 12, 2017 14:34
Immutable caching of static assets with Express.js and Service Worker

Immutable caching of static assets with Express.js and Service Worker

This recipe revisions all asset files in a dist/assets/ directory using gulp-rev. The adds a unique content based hash to each asset file. The pattern of that hash (/.*-[0-9a-f]{10}\..*/) is then used to handle these files in Express.js and the Service Worker. Express.js sets the Cache-Control header to immutable. The Service Worker serves these files directly from cache without ever attempting the server again.

@jbmoelker
jbmoelker / angular-view-box-directive.js
Last active August 6, 2020 20:49
viewBox is an AngularJS directive which adds support for using an expression for the SVG viewBox, by using `data-view-box` which sets `viewBox` attribute. Code borrowed from http://stackoverflow.com/a/14596319
angular
.module('directives.viewBox', [])
.directive('viewBox', [
/**
* @ngdoc directive
* @name directives.viewBox.directive:viewBox
* @description
* Supports using expression for SVG viewBox, by
* using `data-view-box` which sets `viewBox` attribute.
* Code borrowed from http://stackoverflow.com/a/14596319
@jbmoelker
jbmoelker / HTMLDialogElement.d.ts
Created December 15, 2015 13:56
TypeScript definition for HTMLDialogElement based on MDN documentation
/**
* HTMLDialogELement
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement
*/
interface HTMLDialogElement extends HTMLElement {
/**
* Reflects the open HTML attribute,
* indicating that the dialog is available for interaction.
*/
open:boolean;
@jbmoelker
jbmoelker / App.jsx
Created July 8, 2019 18:49
Component Block pattern in React
import React from 'react';
import Section from './section';
export default App = () => (
<Section>
<Section.Header>
<Section.Title> My Section </Section.Title>
</Section.Header>
<Section.Body>
Any content here
@jbmoelker
jbmoelker / paint-timings-table.js
Created October 18, 2017 08:23
Paint timings table: log performance paint metrics to the console
if ('performance' in window) {
window.addEventListener('load', function () {
var paintMetrics = performance.getEntriesByType('paint');
if (paintMetrics !== undefined && paintMetrics.length > 0){
console.table(paintMetrics.map(function(metric) {
return {
event: metric.name,
startTime: metric.startTime.toFixed(2) + ' ms',
duration: metric.duration + ' ms'
}