Skip to content

Instantly share code, notes, and snippets.

// BROWSER
// ASCII to HEX
const a2h = s => [].map.call(s, c => c.charCodeAt(0).toString(16)).join('');
// a2h('mystring');
// HEX to ASCII
const h2a = s => s.match(/.{1,2}/g).map(c => String.fromCharCode(parseInt(c, 16))).join('');
// h2a('6d79737472696e67');
@jonsage
jonsage / ratio.js
Created April 30, 2021 10:40
Reduces a ratio to it's smallest form - good for finding smallest placeholder size for image dimensions
function ratio(w, h, a = w, b = h) {
return b ? ratio(w, h, b, a % b) : [w / a, h / a];
}
ratio(640, 480); // [4,3]
@jonsage
jonsage / buildtime.rb
Last active February 5, 2021 12:54
Registers :build permalink placeholder that is replaced with the site build time (useful for cache-busting files)
# https://articles.inqk.net/2020/02/10/jekyll-permalinks.html
# place in `_plugins` directory
module Jekyll
module Drops
class UrlDrop < Drop
def build
@context.registers[:site].time.strftime("%s")
end
end
end
const getAllFocusableElements = (parent) => Array.from(parent.querySelectorAll('*')).filter(elm => elm.tabIndex > -1).sort((a,b) => a.tabIndex > b.tabIndex ? 1 : a.tabIndex < b.tabIndex ? -1 : 0);
@jonsage
jonsage / shortid.md
Last active September 23, 2020 12:59
Short ID

Eight character random ids

JS

Math.round(Math.random()*(36**8)).toString(36)

Ruby

@jonsage
jonsage / noliquidinfences.rb
Created March 9, 2020 19:44
Jekyll plugin to prevent liquid syntax being processed inside of code fences
# Prevents liquid syntax being processed inside of code fences
Jekyll::Hooks.register :documents, :pre_render do |page, payload|
docExt = page.extname.tr('.', '')
# only process if we deal with a markdown file
if payload['site']['markdown_ext'].include? docExt
page.content.gsub!(/^(\s*?)(```[\s\S]+?```)(\s*?)$/, '\1{% raw %}\2{% endraw %}\3')
end
end
javascript:(function(){fetch(document.location+'media?size=l').then(r => r.blob()).then(blob => URL.createObjectURL(blob)).then(url => {let a = document.createElement('a'); a.href = url; a.download = document.location.href.replace(/^.*\/p\/([^\/]+)\/.*$/,'$1.jpg'); a.click(); })}())