Skip to content

Fix colspan cells producing misaligned markdown table columns#12

Merged
fantomc0der merged 1 commit into
mainfrom
fix-table-colspan-rendering
Apr 27, 2026
Merged

Fix colspan cells producing misaligned markdown table columns#12
fantomc0der merged 1 commit into
mainfrom
fix-table-colspan-rendering

Conversation

@fantomc0der
Copy link
Copy Markdown
Owner

Summary

  • HTML table cells with colspan > 1 now emit the correct number of pipe-delimited columns in the markdown output. A cell with colspan="2" and text "mikethecoder" previously rendered as | mikethecoder | (1 column), causing the header row to be narrower than the separator and data rows. Markdown renderers reject tables with inconsistent column counts, so the table displayed as raw text instead of a rendered table.
  • The fix appends colspan - 1 extra empty | cells after any cell that spans multiple columns, so every row has the same column count and the table renders correctly.
  • Two existing tests updated to reflect the corrected output (| someuser | | and | Header | | instead of the old single-column forms).

Approach

The change is in the table-cell Turndown rule in src/converters/html-to-markdown.ts. After computing the cell text, we read the colspan attribute and append " |".repeat(colspan - 1) to the replacement string. The separator row was already correct (it uses countDirectCells which already accounts for colspan); only the cell rendering needed fixing.

Test plan

  • bun test passes (129 tests, 0 failures)
  • | Header | | — colspan=2 header now has 2 columns
  • | someuser | | — MemeJack-style table header now correctly spans 2 columns
  • Existing 2-column data rows (| Left | Right |, | Magic Number | 17 |) unaffected
  • bun run build succeeds

🤖 Generated with Claude Code

When a table cell has colspan>1, the table-cell Turndown rule now emits
extra empty pipe-delimited columns (colspan-1 of them) so the rendered
row always has the same column count as the separator and data rows.

Previously a colspan="2" header like "mikethecoder" produced "| mikethecoder |"
(1 column) while the separator and data rows had 2 columns, causing most
markdown renderers to reject the table entirely.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@fantomc0der fantomc0der marked this pull request as draft April 27, 2026 22:44
@fantomc0der
Copy link
Copy Markdown
Owner Author

Auto-converted PR to Draft. Mark as Ready for Review when the PR is finished to trigger Claude review and the auto-merge gate.

@github-actions
Copy link
Copy Markdown
Contributor

TypeScript Review

✅ No TypeScript errors were found in these changes

@fantomc0der fantomc0der marked this pull request as ready for review April 27, 2026 22:44
@claude
Copy link
Copy Markdown

claude Bot commented Apr 27, 2026

Claude finished @fantomc0der's task in 1m 17s —— View job


Code Review

  • Gather context
  • Read changed files
  • Review implementation correctness
  • Review test coverage
  • Check CLAUDE.md compliance
  • Post final verdict

Summary

The fix is correct, minimal, and well-targeted. Here's my analysis:

Implementation (src/converters/html-to-markdown.ts:197–200)

The new cell rule mirrors the existing countDirectCells logic exactly — both read colspan with parseInt(..., 10) and guard against non-finite values. The cell now emits " |".repeat(span - 1) extra columns, which keeps every data row in sync with the separator row that countDirectCells already produces. Logic checks out for all edge cases:

Input countDirectCells result Cell output Match?
colspan="2" 2 | text | |
colspan="1" 1 | text |
colspan="0" 1 (fallback) | text | (fallback)
No attribute 1 (default) | text | (default)

One minor style nit (not a bug): the extra variable is only used once. " |".repeat(span - 1) can be inlined directly since "".repeat(0) is "", making the ternary redundant. But this doesn't affect correctness.

Test coverage

  • The renamed test "colspan cells emit extra empty columns to keep table width consistent" clearly describes the intended behavior.
  • The MemeJack test (someuser with colspan="2") provides a realistic integration scenario covering nested/unwrapped tables with colspan.
  • Edge cases (colspan=0, colspan>2, colspan on a non-first cell) aren't explicitly tested, but the logic is simple enough that they follow directly from the validated code path.

CLAUDE.md compliance

  • No real usernames ✓
  • No hard wrapping in added code or comments ✓
  • No @ts-ignore, @ts-expect-error, or as any ✓ (as HTMLElement is the existing pattern throughout the file)
  • No CLI options or output format changed — README/AGENTS.md sync not required ✓

REVIEW: PASS

@fantomc0der fantomc0der merged commit 2ada43d into main Apr 27, 2026
6 checks passed
@fantomc0der fantomc0der deleted the fix-table-colspan-rendering branch April 27, 2026 22:47
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