markdown-transform
markdown-transform copied to clipboard
fix(html): correctly handle <caption> element in HTML table deserialization
Fixes #635
Problem
HTML tables with a <caption> element produced broken Markdown output — each cell rendered on its own line instead of a valid table row:
Before (broken): Employee Details | | Name | Role | | Alice | Engineer |
Root Cause
The <caption> element was being processed as a regular table child node, corrupting the TableHead/TableBody structure during deserialization. This caused the table rows to break apart into individual lines.
Fix
- Extract
<caption>directly from the DOM before processing table children — no leaking intermediate nodes - Emit caption as a standalone
Paragraphblock before the table (plain text, noStrongwrapping of arbitrary content, no\n\ntext injection) - Filter table children to
TableHead/TableBodyonly, ignoring other child nodes - Promote first body row to header when it contains
thcells and notheadexists - Normalize whitespace in table cells via
cleanTableNodes()to removeSoftbreaknodes and merge adjacentTextnodes - Add regression tests covering: caption present, no caption (no regression), whitespace normalization in caption
Note on #640
This PR addresses the same issue as #640 but resolves all review feedback that blocked that PR from merging:
- ✅ Tests included (missing in #640)
- ✅ Caption extracted from DOM directly — no leaking
type: 'caption'nodes - ✅ No
Strongwrapping of arbitrary block content - ✅ No
\n\ntext node hack inside Paragraph - ✅ 4-space indentation throughout (ESLint compliant)
Checklist
- [x] DCO sign-off provided (
--signoff) - [x] Regression tests added
- [x] Commits follow AP conventional commits format
- [x] No documentation changes required
- [x] Merging to main from fork