/** * Regenerates `content-page.yjs` from `content-page.md` (issue #32). * * `content-page.md` is the source of truth — a Markdown document * deliberately covering every node type and mark in the editor schema * (packages/shared/src/editor-schema/schema.ts, issue #24) and already a * fixed point of the Markdown round-trip (`docToMarkdown(markdownToDoc(x)) * === x`); the seed script (`../seed.ts`) loads the checked-in `.yjs` * binary directly rather than re-parsing Markdown on every run. * * Deterministic: `Y.Doc`'s `clientID` is normally randomized per instance, * which would make the encoded update differ on every regeneration even * for identical content. Pinning it to a fixed value makes the output a * pure function of `content-page.md`, so re-running this without editing * the Markdown produces a byte-identical `.yjs` (verified — re-run and * `git diff` should show no changes). * * Run after editing `content-page.md`: * pnpm --filter @dorfteich/api fixtures:regenerate */ import { readFileSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { docToMarkdown, editorSchema, markdownToDoc } from '@dorfteich/shared'; import { prosemirrorJSONToYXmlFragment } from 'y-prosemirror'; import * as Y from 'yjs'; const FIXTURE_DIR = join(__dirname); const FIXED_CLIENT_ID = 1; function main(): void { const markdown = readFileSync(join(FIXTURE_DIR, 'content-page.md'), 'utf8'); const doc = markdownToDoc(markdown); // Round-trip sanity check: if this ever fails, the schema or serializer // changed in a way that makes content-page.md no longer a fixed point — // fix the schema/serializer or update content-page.md, don't silently // check in a snapshot that doesn't match its own source. const roundTripped = docToMarkdown(doc); if (roundTripped !== markdown) { console.error('content-page.md is not a fixed point of the Markdown round-trip.'); console.error('--- expected (content-page.md) ---'); console.error(markdown); console.error('--- got (docToMarkdown(markdownToDoc(content-page.md))) ---'); console.error(roundTripped); process.exit(1); } const ydoc = new Y.Doc(); ydoc.clientID = FIXED_CLIENT_ID; const fragment = ydoc.getXmlFragment('default'); prosemirrorJSONToYXmlFragment(editorSchema, doc.toJSON(), fragment); const state = Y.encodeStateAsUpdate(ydoc); ydoc.destroy(); writeFileSync(join(FIXTURE_DIR, 'content-page.yjs'), state); console.log(`fixtures: wrote content-page.yjs (${state.length} bytes)`); } main();