Skip to content

Instantly share code, notes, and snippets.

@mrclay
mrclay / exceljs-extras.js
Last active April 23, 2024 16:00
Convert Exceljs rich text values to HTML or plaintext strings
function isRichValue(value) {
return Boolean(value && Array.isArray(value.richText));
}
function richToString(rich) {
return rich.richText.map(({ text }) => text).join('');
}
function richToHtml(rich) {
let str = rich.richText.map(({ font = {}, text }) => {
@mrclay
mrclay / flush-iptables.sh
Last active April 22, 2024 14:36
Flush IP tables and restart docker
#!/bin/bash
# Script is needed because my default firewall rules are messed up and after
# every restart, docker containers can't make connections to the host, notably
# preventing debuggers like xdebug from attaching.
# If networking fails in your containers but works in others, rm and re-create the
# docker network that container is bound to.
set -euo pipefail
@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 / 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 / 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 / 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 / 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 / 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});