Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/tools/unified-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,22 +373,19 @@ function convertHtmlToBlocks(html: string): string {
blocks.push(`<!-- wp:paragraph -->\n${element}\n<!-- /wp:paragraph -->`);
break;
case 'h1':
blocks.push(`<!-- wp:heading {"level":1} -->\n${element}\n<!-- /wp:heading -->`);
break;
case 'h2':
blocks.push(`<!-- wp:heading -->\n${element}\n<!-- /wp:heading -->`);
break;
case 'h3':
blocks.push(`<!-- wp:heading {"level":3} -->\n${element}\n<!-- /wp:heading -->`);
break;
case 'h4':
blocks.push(`<!-- wp:heading {"level":4} -->\n${element}\n<!-- /wp:heading -->`);
break;
case 'h5':
blocks.push(`<!-- wp:heading {"level":5} -->\n${element}\n<!-- /wp:heading -->`);
break;
case 'h6':
blocks.push(`<!-- wp:heading {"level":6} -->\n${element}\n<!-- /wp:heading -->`);
// 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(`<!-- wp:heading {"level":${tagName.charAt(1)}} -->\n${element}\n<!-- /wp:heading -->`);
break;
case 'ul':
blocks.push(`<!-- wp:list -->\n${element}\n<!-- /wp:list -->`);
Expand Down
82 changes: 82 additions & 0 deletions tests/tools/html-to-blocks-headings.test.ts
Original file line number Diff line number Diff line change
@@ -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<any>;

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<string, any> = {}): Promise<string> {
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: <h2> was the one heading case that emitted a bare
// `<!-- wp:heading -->` 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 = '<h1>One</h1><h2>Two</h2><h3>Three</h3><h4>Four</h4><h5>Five</h5><h6>Six</h6>';
const blocks = await convertViaCreate(html);

for (const level of [1, 2, 3, 4, 5, 6]) {
expect(blocks).toContain(`<!-- wp:heading {"level":${level}} -->`);
}
// No heading may fall through to the attribute-less form.
expect(blocks).not.toMatch(/<!-- wp:heading -->/);
});

it('keeps the original heading element as the block body', async () => {
const blocks = await convertViaCreate('<h2>Section title</h2>');

expect(blocks).toContain('<!-- wp:heading {"level":2} -->\n<h2>Section title</h2>\n<!-- /wp:heading -->');
});

it('markdown headings get the explicit level too', async () => {
// Markdown is the other route into the converter: ## → <h2> → wp:heading.
const blocks = await convertViaCreate('## From markdown\n\nBody text.', {
content_format: 'markdown',
});

expect(blocks).toContain('<!-- wp:heading {"level":2} -->');
expect(blocks).not.toMatch(/<!-- wp:heading -->/);
});

it('content already in block format is passed through untouched', async () => {
// Guards the pass-through: a document that already contains a bare
// `<!-- wp:heading -->` (Gutenberg's own canonical h2 serialization) must
// not be rewritten — the converter only runs on non-block input.
const original = '<!-- wp:heading -->\n<h2 class="wp-block-heading">Existing</h2>\n<!-- /wp:heading -->';
const result = await convertViaCreate(original);

expect(result).toBe(original);
});
});
Loading