Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / persist-page-views.js
Created October 10, 2021 01:21
Example of persisting page views via analytics package
import Analytics from 'analytics'
import onRouteChange from '@analytics/router-utils'
const persistPageViewsPlugin = {
name: 'persist-page-data-plugin',
page: ({ payload }) => {
const { properties } = payload
const pageView = {
path: properties.path,
title: properties.title,
// via https://yeun.github.io/open-color/
const palette = {
dark: [
'#d5d7e0',
'#acaebf',
'#8c8fa3',
'#666980',
'#4d4f66',
'#34354a',
@DavidWells
DavidWells / package.json
Created September 12, 2021 22:11
Symlink a directory for package.json scripts
{
"if-not-ci": "pnpm ts support/scripts/run-if-not-ci.ts",
}
@DavidWells
DavidWells / javascript-highlight-text.js
Created September 10, 2021 20:01
Highlight Search match in JS
function highlightMatch(string, regexp) {
return escapeString(string).replace(regexp, (match) => `<mark>${match}</mark>`)
}
// Used to match HTML entities and HTML characters.
const unescapedHtml = /[&<>"']/g
const hasUnescapedHtml = RegExp(unescapedHtml.source)
const htmlEscapes = {
'&': '&amp;',
@DavidWells
DavidWells / appendArguments.js
Last active January 2, 2022 00:15
Automagically append arguments to function calls or add to `this` within function
/* Append arguments to functions */
function appendArguments(fn, append, context) {
if (!append) return fn
// console.log('context', context)
return function () {
/* Original args */
const args = Array.prototype.slice.call(arguments)
// console.log('original args', arguments)
/* Create clone of args */
@DavidWells
DavidWells / package.json
Created July 26, 2021 17:41
Passing multiple args to npm-run-all. "npm run test --email=foo --pw=foo --stage=foo"
{
"scripts": {
"test": "npm-run-all 'test:* -- {1}'",
"test:unit": "ava --config ./_tests-unit.cjs",
"test:integration": "ava --config ./_tests-integration.cjs -- --email=$npm_config_email --pw=$npm_config_pw --stage=$npm_config_stage",
}
}
@DavidWells
DavidWells / package.json
Created July 21, 2021 17:59
Download repo via package.json scripts. Remove $GH_DOWNLOAD_TOKEN if public repo. `npm run download`
{
"scripts": {
"predownload": "rimraf ./tmp-folder && rimraf ./folder && mkdirp ./folder",
"download": "curl -H \"Authorization: token $GH_DOWNLOAD_TOKEN\" -L https://api.github.com/repos/repo/repoName/zipball/master > repo.zip",
"postdownload": "unzip -q -d ./tmp-folder repo.zip && cd tmp-folder && mv `ls` repo && mv `ls` ../folder && echo 'repo downloaded!'",
}
}
@DavidWells
DavidWells / message.md
Created June 29, 2021 17:26
Does the new variable setup still allow for me to reference `AWS::`, `Params` & resources with `${}`?
@DavidWells
DavidWells / regex-find-image-links-in-markdow.js
Created June 24, 2021 20:43
Find images in markdown file Regex
@DavidWells
DavidWells / validate-post-data.js
Created June 23, 2021 21:12
Validate markdown post details
const fs = require('fs')
const path = require('path')
const matter = require('gray-matter')
const outdent = require('outdent')
const { promisify } = require('util')
const { globby } = require('markdown-magic')
const authorDirectory = path.join(__dirname, 'authors')
const postsDirectory = path.join(__dirname, 'posts')
const dateFormatRegex = /[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])-/g