Skip to content

Unmerge Word table cells on conversion — today a merged header shifts every value under the wrong heading and drops the cells that overflow #178

Closed

Description

@bencode

Problem

Word tables merge cells — a header spanning several columns, a value spanning several rows. Mammoth hands those over correctly (colspan/rowspan) and reports no messages, and merged cells are not among the content markUnconvertedContent marks (it covers equations, charts/diagrams and bare objects only, ui-packages/web/src/formats/docx-unconverted.ts:49-83). The loss happens later, in the converter's own DOM pass: flattenTableCells and promoteHeaderRow (ui-packages/web/src/formats/docx.ts:26-61) fix paragraph wrapping and header promotion but do nothing about spans — promoteHeaderRow even copies the colspan attribute into the <th>. GFM pipe tables have no span syntax, so a colspan="2" header leaves the header row one cell shorter than the body rows.

Verified end to end with the repository's own dependencies on a 3×3 Word table (gridSpan + vMerge):

  1. Mammoth emits <td colspan="2"> / <td rowspan="2"> with messages: [] — nothing to warn with.
  2. After the header promotion + turndown/GFM steps, the markdown is a 2-column header over 3-column body rows; both span attributes are silently gone.
  3. The shared remark→rehype chain (ui-packages/web/src/formats/markdown-text.ts:29 for the assistant, ui-packages/web/src/components/markdown.tsx:103-109 for the reader) truncates every row to the header's column count: the third cell's content (audited) disappears from the hast tree entirely, and the surviving values sit under the wrong headings — Total 120 and pending both render in the Note column.

So a Word report with merged table cells opens with silently corrupted tables, identically wrong for the reader and for the assistant, with no signal at any layer. The same merged-cell fact is already tracked for the xlsx grid as #170; this is the Word side, with a different failure mode (columns shift and overflow cells vanish, not blank runs).

Proposal

  • In the DOM pass in docx.ts, next to flattenTableCells and promoteHeaderRow, expand spanned cells before turndown so every row of a table has the same cell count:
    • colspan: repeat the cell's content into each spanned column, or pad with empty cells;
    • rowspan: insert the cell (or a continuation) into each following row at the same column position.
  • The output must parse as an aligned GFM table: no cell dropped by the mdast→hast column-count truncation, no value under a wrong heading.
  • If padding is chosen over repetition, mark continuation cells (e.g. ditto marks) rather than leaving blanks a reader reads as empty — pick one behavior and pin it with a test.
  • Leave the Mammoth-messages path alone: this is structure Mammoth delivers correctly and the converter flattens wrongly.

Goal

After this, a Word document whose tables merge cells opens with every value under its own column heading, in the reader's table and in the assistant's tab-separated text view alike — no cell content dropped, no column shifted — so "what is the note for South?" is answered from the same table the reader sees.

Verification

grep -qi 'merge' ui-packages/web/src/formats/docx.test.ts
pnpm --filter @gamma-reader/web test
pnpm check

Refs

Activity

  1. added
    enhancement
    source:scheduled
    on Oct 22, 2026
  2. bencode commented on Oct 22, 2026

    @bencode
    OwnerAuthor

    Independent review — posted by the scheduled issue-reviewer loop (a session separate from the one that files these issues), under the maintainer's standing instruction to evaluate, sharpen, or retire source:scheduled proposals.

    Evidence audit (against main@ebb0ee0): every citation confirmed, most to the exact line.

    • ✅ The DOM pass is exactly as cited — withinTableCell/flattenTableCells/promoteHeaderRow span docx.ts:26-61, invoked with turndown+gfm at :110-124; neither helper touches spans, and promoteHeaderRow's attribute loop (for (const attribute of cell.attributes) heading.setAttribute(...)) does copy colspan into the <th>, as claimed.
    • ✅ The marker exclusion holds — docx-unconverted.ts:49-83 covers equations, charts and bare embedded objects only; no merge/table concept anywhere in it.
    • ✅ Both render chains confirmed — markdown-text.ts:29 (unified().use(remarkParse).use(markdownPlugins).use(remarkRehype)) for the assistant, components/markdown.tsx:103-109 (ReactMarkdown over the same plugin family) for the reader. remark-gfm derives table width from the header row and drops overflow cells in mdast→hast, so the 2-column-header/3-column-body corruption follows from the code as cited.
    • ✅ Family/precedent refs accurate — Tell readers which Word content did not convert, instead of logging it to the browser console #45 (closed) is the inline-marker precedent for content Mammoth drops; merged cells arrive intact, so "fix in the converter" is the right call. Show a merged cell as merged — its value stops at the top-left square today, leaving blank runs that reader and assistant both read as empty #170 is the xlsx side with a different failure mode (blank runs vs. shifted columns + dropped overflow) — sibling, not duplicate.

    Duplicate check: no other open or closed issue touches Word table conversion.

    Sensor: lint ✓, verify ✗ → not implemented on main; stays open.

    Verdict: valid, keep open. The diagnosis layer is unusually precise (loss located in the converter's DOM pass, not Mammoth), and the fix site sits next to two existing helpers doing the same kind of pre-turndown normalization.

    One anchor sharpening before implementation (the #41/#55 lesson): grep -qi 'merge' docx.test.ts misses a test suite that speaks colspan/rowspan/span — and the proposal itself floats OOXML vocabulary (gridSpan, vMerge). One pattern covers every vocabulary in play (including "unmerge" and both OOXML terms, case-insensitively):

    grep -qiE 'merge|span' ui-packages/web/src/formats/docx.test.ts
  3. bencode commented on Oct 24, 2026

    @bencode
    OwnerAuthor

    Re-audit — posted by the scheduled issue-reviewer loop, independent of the authoring session. (Signal check: marker-only comments, no reactions, no assignee.)

    Delta since last review (ebb0ee0 → 90b046f): zero commits touch formats/ or either markdown render chain — the ~20 commits on main since are all composer/conversation/tab work.

    Verdict: still valid, keep open; no action. The prior review's sharpened test anchor (grep -qiE 'merge|span' ui-packages/web/src/formats/docx.test.ts) remains the right gate — it covers every vocabulary in play (colspan, gridSpan, vMerge, "unmerge").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementsource:scheduled

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions