Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
12345678910111213141516171819202122232425// A section lacking a block before its subsections is repaired on load: the// schema wants one, and the editor's DOM re-read mangles a section without.import { test } from 'node:test';import assert from 'node:assert/strict';import { repairBody } from '../src/doc.ts';import type { PMJson } from '../src/types.ts';
const sec = (id: string, kids: PMJson[]): PMJson => ({ type: 'section', attrs: { id }, content: [{ type: 'sectionTitle' }, ...kids] });
test('repairBody leaves a valid document as it is', () => { const doc: PMJson = { type: 'doc', content: [sec('a', [{ type: 'paragraph', attrs: { id: 'p' } }, sec('b', [{ type: 'paragraph', attrs: { id: 'q' } }])])] }; assert.equal(repairBody(doc, () => 'x'), doc);});
test('repairBody gives a section whose subsections follow its title an empty paragraph first, at every depth', () => { const doc: PMJson = { type: 'doc', content: [sec('a', [sec('b', [sec('c', [{ type: 'paragraph', attrs: { id: 'q' } }])])]), sec('d', [])] }; let n = 0; const out = repairBody(doc, () => `n${++n}`); assert.notEqual(out, doc); assert.deepEqual(out.content![0]!.content!.map((c) => c.type), ['sectionTitle', 'paragraph', 'section']); assert.deepEqual(out.content![0]!.content![2]!.content!.map((c) => c.type), ['sectionTitle', 'paragraph', 'section']); assert.deepEqual(out.content![1]!.content!.map((c) => c.type), ['sectionTitle', 'paragraph']); assert.equal(n, 3);});