Skip to content

Instantly share code, notes, and snippets.

Avatar

Kieran Barker kieranbarker

View GitHub Profile
@kieranbarker
kieranbarker / getJSON.js
Last active Mar 21, 2022
Get the JSON data from a Response object.
View getJSON.js
/**
* 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 Jul 19, 2020
Escape double and single quotes inside a string
View escapeQuotes.js
/**
* 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 Aug 23, 2020
Visually hide an element but keep it accessible to screen readers
View visuallyHidden.css
/**
* 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 Aug 23, 2020
A simple .gitignore for Eleventy projects
View .gitignore
# Misc
*.log
npm-debug.*
*.scssc
*.swp
.DS_Store
Thumbs.db
.sass-cache
.env
.cache
@kieranbarker
kieranbarker / isValidEmail.js
Last active Jan 20, 2021
Check if a string is a valid email address
View isValidEmail.js
/**
* 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 Sep 13, 2020
Add a class to an element
View addClass.js
/**
* 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 Sep 24, 2020
Barebones config for new Eleventy projects
View .eleventy.js
module.exports = config => {
return {
markdownTemplateEngine: 'njk',
dataTemplateEngine: 'njk',
htmlTemplateEngine: 'njk',
dir: {
input: 'src',
output: 'dist'
}
};
@kieranbarker
kieranbarker / posts.11tydata.js
Last active Oct 18, 2020
Prevent draft and scheduled posts from being written to output in Eleventy
View posts.11tydata.js
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 Oct 18, 2020
Filter out draft and scheduled posts from blog collection in Eleventy
View filter-blog-collection.js
// 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 Oct 18, 2020
Redirect Jekyll post format to Eleventy post format
View redirects.toml
[[redirects]]
from = "/:year/:month/:date/:slug"
to = "/blog/:slug"
status = 301
force = true