Skip to content

Instantly share code, notes, and snippets.

View kieranbarker's full-sized avatar

Kieran Barker kieranbarker

View GitHub Profile
@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 / .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 / 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 / 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 / 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 / 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 / favicon.svg
Created October 29, 2020 18:20
A simple snippet for an SVG favicon
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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();
}