Skip to content

Instantly share code, notes, and snippets.

@mrclay
mrclay / block-fragments.ts
Last active April 11, 2024 14:28
Script to generate a codegen-friendly CoreBlocksFragment.ts file from Faust's blocks.
/**
* Generates file ./utils/queries/WpBlocksFragment.ts containing
* a fragment with all the WP core block fragments Faust provides.
*
* USAGE: ts-node block-fragments.ts
*/
import { CoreBlocks } from '@faustwp/blocks';
import { glob } from 'glob';
import { mkdirSync, writeFileSync } from 'node:fs';
@mrclay
mrclay / parse-tags.js
Last active August 25, 2023 15:26
JS functions for parsing valid HTML
// Each match includes [fullElement, attributesStr, innerHtml]
function makeElementExpr(tagname, selfClosing = false) {
return selfClosing
? new RegExp(`<${tagname}\\b([^>]*)/?>`, 'g')
: new RegExp(`<${tagname}\\b([^>]*)>([\\s\\S]*?)</${tagname}>`, 'g');
}
// Parse into obj like React props
// Note: If you don't provide htmlDecoder, string attribute values will be returned
// as HTML escaped values rather than text.
@mrclay
mrclay / menus.graphql
Created August 18, 2023 18:44
GraphQL query for WP menu items
query MenuQuery {
menus {
nodes {
id
primaryMenuFields {
demoField
}
menuItems {
nodes {
url
@mrclay
mrclay / withoutNulls.ts
Last active July 12, 2023 20:11
Destructure objects so that defaults can be given for nulls, too.
type OmitNull<T> = T extends null ? Exclude<T, null> | undefined : T;
type OmitNullProps<T extends object> = { [Key in keyof T]: OmitNull<T[Key]> };
/**
* Allow destructuring an object, turning any null properties into undefined so
* a default can be given. If the given object may be null/undefined, then every
* property may be undefined.
*
* Useful for assigning defaults for properties that may be null. In a usual destructuring
* assignment, nulls are passed through so you can't provide a default for that case:
@mrclay
mrclay / vite-plugin-qwik-fragment.ts
Last active September 14, 2023 13:12
Vite plugin to place after the Qwik's static output plugin to generate fragment.html
/**
* Creates embed-friendly dist/fragment.html
*
* Install qwik: `npm create qwik@latest`
* Configure static: https://qwik.builder.io/docs/guides/static-site-generation/#static-site-generation-config
* In adapters/static/vite.config.ts place vitePluginQwikFragment() after the staticAdapter.
* Build: `npm run build`
* The file `dist/fragment.html` will be created.
* To test: `npx serve dist` and open http://localhost:3000/fragment.html
*
@mrclay
mrclay / empty.js
Created November 23, 2022 18:40
Find empty HTML elements in DOM
[...document.querySelectorAll('h1,h2,h3,h4,h5,h6')].filter(el => !el.textContent.trim()).forEach(el => console.log(el))
@mrclay
mrclay / file.json.d.ts
Created October 6, 2022 16:18
Example d.ts file for a JSON file
// filename.json <-- the data
// filename.json.d.ts <-- type definition (this file)
// Just an example type
const value: Record<string, string>;
export default value;
@mrclay
mrclay / fetch-inpector.ts
Last active September 17, 2022 15:27
Create a fetch wrapper that allows logging input/output.
export interface FetchInfo {
uri: RequestInfo;
init: RequestInit;
res?: unknown;
text?: string;
error?: unknown;
}
function defaultLogger({uri, init, text}: FetchInfo): void {
console.info({uri, text, init});
@mrclay
mrclay / teaful-sync.js
Last active August 8, 2022 03:20
Minimal teaful-like store using useSyncExternalStore()
import {useSyncExternalStore} from 'use-sync-external-store/shim';
const DOT = '.';
export default function createStore(defaultStore = {}) {
let allStore = defaultStore;
const listeners = new Set();
function subscribe(listener) {
listeners.add(listener);
@mrclay
mrclay / root-dir.sh
Last active May 2, 2022 23:58
Shell: Getting directory of code or active script
#!/bin/bash
# Execution-relative (all shells). Get dir path of the script executed
# by the user.
__0="$(CDPATH= cd -- $(dirname -- $0) && pwd -P)"
echo "$__0"/bar
# Code-relative (bash/zsh only). Get dir path of *this file*,