Skip to content

Instantly share code, notes, and snippets.

View kieranbarker's full-sized avatar

Kieran Barker kieranbarker

View GitHub Profile
@kieranbarker
kieranbarker / serialize.js
Last active January 11, 2021 12:30
Serialize all form data into a query string
/**
* Serialize all form data into a query string
* {@link https://gist.github.com/kieranbarker/bb7c54bd12156c069419f64a22e61552}
* @param {HTMLFormElement} form The form
* @returns {String} The query string
*/
function serialize (form) {
// Create a new FormData object
const formData = new FormData(form);
@kieranbarker
kieranbarker / formatJSON.js
Last active February 24, 2021 11:34
Format a JSON string for readability
/**
* Format a JSON string for readability
* @param {String} text The JSON string
* @param {Number|String} space The number of spaces, or a string, to indent by
* @returns {String} The newly formatted JSON string
* @license MIT
*/
function formatJSON (text, space = 2) {
return JSON.stringify(JSON.parse(text), null, space);
}
@kieranbarker
kieranbarker / getLastWord.js
Created November 27, 2020 10:09
Get the last word in a string
/**
* Get the last word in a string
* @param {String} str The string
* @returns {String} The last word
*/
function getLastWord (str) {
return str.trim().split(/\s+/).pop();
}
@kieranbarker
kieranbarker / favicon.svg
Created October 29, 2020 18:20
A simple snippet for an SVG favicon
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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
@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 / 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 / .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 / 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 / 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])?)*$/;