Skip to content

Instantly share code, notes, and snippets.

View kieranbarker's full-sized avatar

Kieran Barker kieranbarker

View GitHub Profile
@kieranbarker
kieranbarker / getJSON.js
Last active March 21, 2022 09:42
Get the JSON data from a Response object.
/**
* Get the JSON data from a Response object.
* @param {Response} response The Response object.
* @returns {Promise} The JSON data or an Error.
*/
async function getJSON(response) {
if (response.ok) {
const data = await response.json();
return Promise.resolve(data);
}
@kieranbarker
kieranbarker / escapeQuotes.js
Created July 19, 2020 07:16
Escape double and single quotes inside a string
/**
* Escape double and single quotes inside a string
* @param {String} string The string
* @returns {String} The string with quotes escaped
*/
function escapeQuotes (string) {
return string.replace(/"/g, """).replace(/'/g, "'");
}
@kieranbarker
kieranbarker / visuallyHidden.css
Last active August 23, 2020 12:33
Visually hide an element but keep it accessible to screen readers
/**
* Visually hide an element but keep it accessible to screen readers
* {@link https://github.com/h5bp/html5-boilerplate/blob/master/dist/css/main.css}
* {@link http://snook.ca/archives/html_and_css/hiding-content-for-accessibility}
* {@link https://github.com/h5bp/main.css/issues/12#issuecomment-321106995}
*/
.visually-hidden {
border: 0;
clip: rect(0 0 0 0);
height: 1px;
@kieranbarker
kieranbarker / .gitignore
Last active August 23, 2020 12:31
A simple .gitignore for Eleventy projects
# Misc
*.log
npm-debug.*
*.scssc
*.swp
.DS_Store
Thumbs.db
.sass-cache
.env
.cache
@kieranbarker
kieranbarker / isValidEmail.js
Last active January 20, 2021 20:49
Check if a string is a valid email address
/**
* Check if a string is a valid email address
* {@link https://gist.github.com/kieranbarker/55a12cac034c386a5b3669b991290bf6}
* @param {String} str The string
* @returns {Boolean} Whether the string is a valid email address
*/
function isValidEmail (str) {
// The regular expression used by [type="email"]
// https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
const regex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
@kieranbarker
kieranbarker / addClass.js
Last active September 13, 2020 20:24
Add a class to an element
/**
* Add a class to an element
* @param {Object|String} selector A reference to an element or a CSS selector string
* @param {String} className The class name
*/
function addClass (selector, className) {
if (selector instanceof Element) {
selector.classList.add(className);
} else if (typeof selector === "string") {
@kieranbarker
kieranbarker / .eleventy.js
Created September 24, 2020 11:34
Barebones config for new Eleventy projects
module.exports = config => {
return {
markdownTemplateEngine: 'njk',
dataTemplateEngine: 'njk',
htmlTemplateEngine: 'njk',
dir: {
input: 'src',
output: 'dist'
}
};
@kieranbarker
kieranbarker / posts.11tydata.js
Last active October 18, 2020 14:12
Prevent draft and scheduled posts from being written to output in Eleventy
module.exports = {
layout: 'layouts/post.html',
eleventyComputed: {
permalink(data) {
if (!data.draft && new Date(data.date) <= new Date()) {
return data.permalink || '/blog/{{ title | slug }}/index.html';
}
return false;
}
}
@kieranbarker
kieranbarker / filter-blog-collection.js
Created October 18, 2020 08:30
Filter out draft and scheduled posts from blog collection in Eleventy
// Returns a collection of blog posts in reverse date order
config.addCollection('blog', collection => {
const isLive = post => !post.data.draft && post.date <= new Date();
return collection.getFilteredByGlob('./src/posts/*.md').filter(isLive).reverse();
});
@kieranbarker
kieranbarker / redirects.toml
Created October 18, 2020 15:39
Redirect Jekyll post format to Eleventy post format
[[redirects]]
from = "/:year/:month/:date/:slug"
to = "/blog/:slug"
status = 301
force = true