diff --git a/src/tools/unified-content.ts b/src/tools/unified-content.ts index 1f133be..c19496e 100644 --- a/src/tools/unified-content.ts +++ b/src/tools/unified-content.ts @@ -373,22 +373,19 @@ function convertHtmlToBlocks(html: string): string { blocks.push(`\n${element}\n`); break; case 'h1': - blocks.push(`\n${element}\n`); - break; case 'h2': - blocks.push(`\n${element}\n`); - break; case 'h3': - blocks.push(`\n${element}\n`); - break; case 'h4': - blocks.push(`\n${element}\n`); - break; case 'h5': - blocks.push(`\n${element}\n`); - break; case 'h6': - blocks.push(`\n${element}\n`); + // Always emit the level explicitly. h2 used to be the lone bare case + // (Gutenberg's serializer omits the default level), which made + // conversion output inconsistent across heading levels and read as + // "the {"level":2} attribute got stripped" when existing content was + // round-tripped through this converter. An explicit default attribute + // is valid for the block parser, and a single deterministic form is + // worth more here than canonical minimalism. + blocks.push(`\n${element}\n`); break; case 'ul': blocks.push(`\n${element}\n`); diff --git a/tests/tools/html-to-blocks-headings.test.ts b/tests/tools/html-to-blocks-headings.test.ts new file mode 100644 index 0000000..d06cc7d --- /dev/null +++ b/tests/tools/html-to-blocks-headings.test.ts @@ -0,0 +1,82 @@ +import { describe, it, expect, vi, beforeAll } from 'vitest'; + +// Captures the body of each write so tests can inspect the exact content the +// converter produced, without any WordPress on the other end. +const writes: Array<{ endpoint: string; data: any }> = []; + +vi.mock('../../src/wordpress.js', () => ({ + makeWordPressRequest: vi.fn(async (method: string, endpoint: string, data?: any) => { + if (method === 'POST' || method === 'PUT') { + writes.push({ endpoint, data }); + return { id: 1, ...data }; + } + return {}; + }), + logToFile: vi.fn(), +})); + +let createContent: (params: any) => Promise; + +beforeAll(async () => { + const mod = await import('../../src/tools/unified-content.js'); + createContent = (mod.unifiedContentHandlers as any).create_content; +}); + +async function convertViaCreate(content: string, extra: Record = {}): Promise { + const before = writes.length; + await createContent({ + content_type: 'post', + title: 'Heading conversion test', + content, + convert_to_blocks: true, + ...extra, + }); + expect(writes.length).toBe(before + 1); + return writes[writes.length - 1].data.content as string; +} + +describe('convertHtmlToBlocks heading levels', () => { + // The bug this catches:

was the one heading case that emitted a bare + // `` with no {"level":2}, while h1 and h3-h6 all carried + // their level. Converted documents came out inconsistent across heading + // levels, and content whose blocks previously carried an explicit + // {"level":2} appeared to have the attribute stripped after any + // convert_to_blocks round-trip — which is exactly how it was reported from + // the field ("stripped the level off every heading block"). + it('emits an explicit level attribute for every heading tag, h2 included', async () => { + const html = '

One

Two

Three

Four

Five
Six
'; + const blocks = await convertViaCreate(html); + + for (const level of [1, 2, 3, 4, 5, 6]) { + expect(blocks).toContain(``); + } + // No heading may fall through to the attribute-less form. + expect(blocks).not.toMatch(//); + }); + + it('keeps the original heading element as the block body', async () => { + const blocks = await convertViaCreate('

Section title

'); + + expect(blocks).toContain('\n

Section title

\n'); + }); + + it('markdown headings get the explicit level too', async () => { + // Markdown is the other route into the converter: ## →

→ wp:heading. + const blocks = await convertViaCreate('## From markdown\n\nBody text.', { + content_format: 'markdown', + }); + + expect(blocks).toContain(''); + expect(blocks).not.toMatch(//); + }); + + it('content already in block format is passed through untouched', async () => { + // Guards the pass-through: a document that already contains a bare + // `` (Gutenberg's own canonical h2 serialization) must + // not be rewritten — the converter only runs on non-block input. + const original = '\n

Existing

\n'; + const result = await convertViaCreate(original); + + expect(result).toBe(original); + }); +});