Skip to content

Instantly share code, notes, and snippets.

View dmsnell's full-sized avatar

Dennis Snell dmsnell

View GitHub Profile
@dmsnell
dmsnell / formula-block-editor.js
Last active September 9, 2019 19:51
Formula-entry and mathematical typesetting block
const { registerBlockType } = wp.blocks;
const { TextareaControl } = wp.components;
const { createElement: el } = wp.element;
const attributes = {
formula: { type: 'string' },
rendered: {
type: 'string',
source: 'attribute',
selector: 'img',
@dmsnell
dmsnell / BlockParser.java
Last active April 12, 2020 03:49
Native Java BlockParser for Gutenberg Posts
import java.util.ArrayList;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
class BlockParser {
@dmsnell
dmsnell / tag-hasher.java
Last active July 5, 2020 01:01
Simplenote Tag Hashing Functions - generate tag entity id from tag name
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.Normalizer;
import java.util.Locale;
class TagHasher {
public static String tagHash(String tagName) {
try {
String normalized = Normalizer.normalize(tagName, Normalizer.Form.NFC);
String lowercased = normalized.toLowerCase(Locale.ENGLISH);
@dmsnell
dmsnell / sequenced-async.ts
Created August 17, 2020 16:24
JS worker processing sync and async inbound messages sequentially, inspired by Erlang gen_server
import { MessageChannel, MessagePort } from 'worker_threads';
type ServerDescription<K extends string> = {
[type in K]: (...args: unknown[]) => unknown;
}
type ServerHandler<M extends ServerDescription<keyof M>, S> = <T extends keyof M>(msg: T, state: S) => [ReturnType<M[T]>, S] | Promise<[ReturnType<M[T]>, S]>;
class GenericServer<M extends ServerDescription<keyof M>, S = undefined> {
count = 0;
@dmsnell
dmsnell / demo.js
Last active August 27, 2020 21:56
Process Primitives
const { spawn, send } = require('./process.js')
const counter = spawn( async (receive) => {
let count = 0;
while (1) {
const message = await receive();
if (message === 'inc') {
console.log(++count);
@dmsnell
dmsnell / counter.mjs
Created August 29, 2020 20:31
50k `async` JS processes
export const counter = async ({send, receive}) => {
let count = 0;
while (1) {
const [action, args] = await receive();
switch (action) {
case 'inc':
count++;
break;
@dmsnell
dmsnell / post-no-sourced-attributes.html
Created December 7, 2022 00:10
Gutenberg serialized HTML post with no sourced attributes.
<!-- wp:paragraph {"content":"This paragraph has \u003cem\u003e\u003cstrong\u003eno sourced attributes\u003c/strong\u003e\u003c/em\u003e. It's content is fully stored inside the JSON attributes \u003cem\u003eand\u003c/em\u003e it still contains the rendered HTML useful for rendering it with no server is available."} -->
<p>This paragraph has <em><strong>no sourced attributes</strong></em>. It's content is fully stored inside the JSON attributes <em>and</em> it still contains the rendered HTML useful for rendering it with no server is available.</p>
<!-- /wp:paragraph -->
<!-- wp:image {"url":"https://wordpress.org/files/2022/08/theme-styles.png","caption":"Image captions are usually sourced from the \u003ccode\u003efigcaption\u003c/code\u003e element.","width":650,"height":406,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://wordpress.org/files/2022/08/theme-styles.png" alt="" width="650" height="406"/><figcaption class="wp-element-captio