Skip to content

feat: support column alignment in tables - #53

Open
Akiyo974 wants to merge 2 commits into
tempestphp:mainfrom
Akiyo974:feat/table-alignment
Open

Akiyo974 wants to merge 2 commits into
tempestphp:mainfrom
Akiyo974:feat/table-alignment

Conversation

@Akiyo974

Copy link
Copy Markdown

Closes #11

The separator row of a table now sets the alignment of each column, as in GFM:

Separator Output
:--- align="left"
:---: align="center"
---: align="right"
--- no attribute

The alignment is applied to every <th> and <td> of the column. I used the align attribute because that's what the GFM spec and league/commonmark render by default. Happy to switch to style="text-align: …" if you'd prefer.

Changes

  • TableRule reads the alignments from the separator row instead of discarding it. Splitting a row into cells is moved to a cells() method, used by both regular and separator rows.
  • TableToken has a new $alignments property (a list<string|null>, one entry per column).
  • TableRow::$cells is typed as list<string>, so that the column index passed to align() is an int.

Tests

  • Mixed alignments, columns without alignment, a separator without outer pipes, and several data rows
  • A TableToken unit test with alignments
  • Updated test_lex_separator_with_alignment and test_table_with_empty_cells, which expected the alignment to be ignored

mago analyse, mago lint and mago fmt report nothing new.

Note: #45 also touches TableToken, so I'm happy to rebase whichever lands first.

table-alignment-before-after

Copilot AI balanced review requested due to automatic review settings October 26, 2026 16:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Later separator-like rows overwrite established alignments, and separators without both outer pipes remain incorrectly handled.

2 open issues
What changed in this PR

Adds separator-driven column alignment to Markdown tables, addressing #11 by rendering alignment attributes on header and data cells.

Changes:

  • Extracts column alignments from separator rows using shared cell-splitting logic.
  • Stores alignments on TableToken and applies them during HTML rendering.
  • Adds alignment tests and updates existing expectations.
File Description
tests/​Tokens/​TableTokenTest.php Tests rendering with explicit column alignments.
tests/​Rules/​TableRuleTest.php Adds separator alignment cases and updates expected HTML.
src/​Tokens/​TableToken.php Stores alignments and renders attributes on table cells.
src/​Tokens/​TableRow.php Documents cells as a list with integer indexes.
src/​Rules/​TableRule.php Extracts separator alignments and shares cell splitting.

Review effort: Balanced

Comment thread src/Rules/TableRule.php Resolved
Comment thread tests/Rules/TableRuleTest.php Resolved
@github-actions

Copy link
Copy Markdown

Benchmark Results

Comparison of feat/table-alignment against main (b4cd28e8892eb3049af2bc67612f34149ada5aa7).

Open to see the benchmark results

No benchmark changes above ±5%.

Generated by phpbench against commit b279367

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Table alignment

2 participants