A calm place to write long-form, and publish it to the open social web.
skypress.blog/
1/**
2 * The @wordpress block oracle — registration + serialize/parse.
3 *
4 * Importing this pulls the full `@wordpress/block-library` + `@wordpress/block-editor`
5 * graph, which only runs under a DOM (browser or the Vitest/jsdom env) — it CANNOT
6 * render in pure Node/edge (Decision 0003). So this module is used by:
7 * - the editor island (browser), and
8 * - tests, as the fidelity oracle for the dependency-free `render.ts`.
9 * It must NOT be imported by reading pages. The light reader path lives in `render.ts`.
10 */
11import {
12 serialize,
13 parse,
14 getBlockTypes,
15 unregisterBlockType,
16 type BlockInstance,
17} from '@wordpress/blocks';
18import { registerCoreBlocks } from '@wordpress/block-library';
19import { ALLOWED_BLOCKS } from './allowlist';
20
21let registered = false;
22
23/** Block names kept for parsing even when not user-insertable. */
24const FALLBACK_BLOCKS = [ 'core/missing', 'core/freeform' ];
25
26/**
27 * Register exactly the curated block set (idempotent). Registers core blocks,
28 * then trims everything outside the allowlist so the serializer's surface
29 * matches the editor's content model.
30 */
31export function registerSkyPressBlocks(): void {
32 if ( registered ) {
33 return;
34 }
35 registerCoreBlocks();
36
37 const keep = new Set< string >( [ ...ALLOWED_BLOCKS, ...FALLBACK_BLOCKS ] );
38 for ( const blockType of getBlockTypes() ) {
39 if ( ! keep.has( blockType.name ) ) {
40 unregisterBlockType( blockType.name );
41 }
42 }
43 registered = true;
44}
45
46/** Serialize a block tree to Gutenberg block markup. */
47export function serializeBlocks( blocks: BlockInstance[] ): string {
48 return serialize( blocks );
49}
50
51/** Parse Gutenberg block markup back into a block tree. */
52export function parseBlocks( markup: string ): BlockInstance[] {
53 return parse( markup );
54}