Skip to content

fix: convertHtmlToBlocks emits <h2> headings without their level attribute - #38

Open
kleintech wants to merge 2 commits into
InstaWP:mainfrom
kleintech:fix/h2-heading-level
Open

kleintech wants to merge 2 commits into
InstaWP:mainfrom
kleintech:fix/h2-heading-level

Conversation

@kleintech

Copy link
Copy Markdown
Contributor

Bug

convertHtmlToBlocks() emits <!-- wp:heading {"level":N} --> for h1 and h3–h6, but a bare <!-- wp:heading --> for h2:

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 -->`);   // ← the odd one out
  break;

To be fair to the original code: bare-for-h2 matches Gutenberg's own canonical serialization (default attributes are omitted). But one implicit case among six explicit ones makes conversion output inconsistent across heading levels — and in the field it reads as data loss: content whose blocks carried an explicit {"level":2} came back from a convert_to_blocks round-trip with the attribute gone, reported as "stripped the level off every heading block."

Fix

All six heading tags share one case that emits the level explicitly (an explicit default attribute is valid for the block parser; the editor's next save may normalize it back out, which is fine):

case 'h1': case 'h2': case 'h3': case 'h4': case 'h5': case 'h6':
  blocks.push(`<!-- wp:heading {"level":${tagName.charAt(1)}} -->\n${element}\n<!-- /wp:heading -->`);
  break;

Content already in block format is untouched — the pass-through in processContent runs before the converter, and a test pins that.

Tests

tests/tools/html-to-blocks-headings.test.ts drives create_content with convert_to_blocks: true through a mocked transport and inspects the written content: all six levels explicit, block body preserved, the markdown route (## → <h2>) covered, and block-format pass-through untouched. 3 of 4 fail against the current implementation (the pass-through test correctly passes on both).

Full suite 39 passed / 2 skipped, tsc clean.

Suggested CHANGELOG entry (following the repo's release-time backfill convention rather than touching [Unreleased] in the PR):

Fixed

  • convert_to_blocks emitted <h2> headings without their {"level":2} attribute while every other heading level carried one, making converted output inconsistent and reading as attribute loss on round-trips. All heading levels are now explicit.

🤖 Generated with Claude Code

https://claude.ai/code/session_012xFYsxskaCvMkg5mDw2iVH

kleintech and others added 2 commits September 1, 2026 14:10
convertHtmlToBlocks() emitted <!-- wp:heading {"level":N} --> for h1 and
h3-h6, but a bare <!-- wp:heading --> for h2. Bare-for-h2 matches
Gutenberg's own canonical serialization (default attributes are
omitted), but a converter with one implicit case among six explicit ones
produces inconsistent output across heading levels — and when existing
content whose blocks carried an explicit {"level":2} was round-tripped
through convert_to_blocks, the attribute vanished, which was reported
from the field as "the level got stripped off every heading block".

All six heading tags now share one case that emits the level explicitly;
an explicit default attribute is valid for the block parser. Content
already in block format is untouched (pass-through happens before the
converter), covered by a test.

Tests drive create_content with convert_to_blocks through the mocked
transport and inspect the written content: 3/4 fail against the previous
implementation.

Co-Authored-By: Claude <noreply@anthropic.com>
Review nit: the bare '{"level":2}' substring could pass via some other
block type; use the file's stronger exact-comment idiom.

Co-Authored-By: Claude <noreply@anthropic.com>
@kilo-code-bot

kilo-code-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

Kilo Code Review could not run — your account is out of credits.

Add credits or switch to a free model to enable reviews on this change.

@kleintech

Copy link
Copy Markdown
Contributor Author

Adversarial review record (one independent reviewer over the diff; all findings verified before acting):

Clean, confirmed by execution: tagName.charAt(1) can never see anything but h1–h6 (the regex captures only the tag name; match[2] is only hr/br, which never reach the heading cases) — verified over 11 adversarial inputs including attributes, uppercase tags, > inside attribute values, nesting, and unclosed tags, with every emitted attribute object parsing as valid JSON. No other code in the repo consumes the bare <!-- wp:heading --> form. All four review mutations killed (bare-h2 revert, quoted level, wrong charAt index, dropped block-format pass-through) — no survivors on any changed line. Suite, shuffle, per-test isolation, and tsc all green.

Dispositioned: the reviewer flagged the missing CHANGELOG entry; every merged outside-contributor PR here (#23–#26) left CHANGELOG to release-time backfill, so this PR follows that precedent and carries a suggested entry in the description instead. A pre-existing nit (the converter's block bodies omit class="wp-block-heading", which WP 6.1+ core/heading saves — applies equally to all levels and to wp:list, unchanged by this diff) is noted here so the new exact-string test isn't later read as endorsing it.

kleintech added a commit to kleintech/mcp-wp that referenced this pull request Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant