Skip to content

Instantly share code, notes, and snippets.

@zadeviggers
zadeviggers / embed-yotube-videos-in-md.ts
Last active June 22, 2022 23:56
A function to embed yotube videos in markdown
// This code looks for syntax like `{{youtube: https://www.youtube.com/watch?v=dQw4w9WgXcQ}}`
// rather than a plain URL, but it should be simple enough to modify the regex.
// Call this function on your raw markdown before your markdown processor (e.g. Marked, ReMark, etc) is run on it,
// and then pass the output of this function to your markdown processor.
function embedYoutubeVideos(md: string): string {
const ytRegexGlobal = /{{youtube: ?([^{}]+)}}/g;
const matches = md.matchAll(ytRegexGlobal);
let output = md;
@zadeviggers
zadeviggers / generate-json-file.ts
Last active February 22, 2022 03:21
The code for my search bar (https://publictransportforum.nz/articles). Made in SolidJS (with FuseJS for fuzzy matching) to be used in an Astro site. Note that this code is ripped straight from my source so it might need a bit of tweaking to make it work for you.
// Here are some snippets from the file that loads all my article's markdown files and does processing on them
// including generating the json file for search
import * as fs from "fs";
import path from "path";
const publicDirectory = path.join(process.cwd(), "public");
// At the bottom of the function that populates the cache of articles...
const searchFile = JSON.stringify(
articles.map((article) => ({
@zadeviggers
zadeviggers / openAuthPopupInScreenCenter.js
Created March 1, 2022 00:46
Opens a login window popup in the center of the screen using window.open().
const popupWidth = 400;
const popupHeight = 600;
// Maths "borrowed" from the smart folks at Auth0 (https://github.com/auth0/auth0-spa-js/blob/451956f5615bdd7e8fe3c313dbe30d1e31b855f1/src/utils.ts#L92)
const popupLeft = window.screenX + (window.innerWidth - popupWidth) / 2;
const popupTop = window.screenY + (window.innerHeight - popupHeight) / 2;
// The window.open call needs to be called inside an event listener for a user-created action like a click
document.addEventListener("click", ()=>window.open("https://exmaple.com?in_popup=ye", "my-auth-popup", `popup,width=${popupWidth},height=${popupHeight},left=${popupLeft},top=${popupTop}`));
@zadeviggers
zadeviggers / easeInOutQuad.py
Created March 19, 2022 08:49
Ease in-out one-liner function in python
def easeInOutQuad(x: int | float) -> int | float:
return 2 * x * x if x < 0.5 else 1 - pow(-2 * x + 2, 2) / 2
const escaped_fragment_id = CSS.escape(location.hash.slice(1));
const element = document.querySelector(`#${escaped_fragment_id}, a[name="${escaped_fragment_id}"`);
element.scrollIntoView();
element.classList.add("fragment-targeted");
@zadeviggers
zadeviggers / emmet-in-jinja-vscode.json
Created June 8, 2022 05:50
Add this to your vscode json configuration file when using the better jinja extention (samuelcolvin.jinjahtml) to get emmet snippet support.
{
"emmet.includeLanguages": {
"jinja-html": "html"
}
}

Parses any well-formed URI (and makes safe asumptions for commonly used bad formations), and outputs all the useful information in capture groups:

^(?:(.+?):\/\/)?(?:([^@/\n]+)@)?((?:[a-z]|[0-9]|\.)+)(?:\:([0-9]{1,5}))?([^#?\n]+)?(\?[^#\n]+)?(?:#(.+))?$

Tests:

http://beans.com@google.com/@page/page.html?query&q=very true#fragment
http://beans.com/bees@google.com/@page/page.html?query&q=very true#fragment
http://beans.com@google.com:1000/@page/page.html?query&q=very true#fragment