Skip to content

Build a Markdown.Table instead of hand-rolling the markdown writer - #1008

Open
andreasnoack wants to merge 1 commit into
masterfrom
an/coeftable-markdown-table
Open

Build a Markdown.Table instead of hand-rolling the markdown writer#1008
andreasnoack wants to merge 1 commit into
masterfrom
an/coeftable-markdown-table

Conversation

@andreasnoack

@andreasnoack andreasnoack commented Aug 21, 2026

Copy link
Copy Markdown
Member

Follow-up to #1007, rebased on it now that it has merged.

#1007 fixed the escaping bug by hand, as a minimal short-term fix. This removes the reason the bug was possible: it stops reimplementing the markdown table format altogether. The rows still go through Base's print_matrix_row — that is what produces the decimal alignment — and the padded strings are handed to Markdown.Table, so escaping and the alignment rules come from the stdlib rather than from escape_markdown_pipes.

The justification for hand-rolling does not hold

The method carries this comment, from #664:

not using Markdown stdlib here because that won't give us nice decimal alignment (even if that is lost when rendering to HTML, it's still nice when looking at the markdown itself)

The stdlib does not produce decimal alignment, but it preserves it: padcells! adds the same width to every cell in a column, so pre-padded equal-width cells keep their decimal points lined up. Since print_matrix_row already emits exactly those strings, the alignment survives:

|     |      Estimate | Comments |  df |      t | p      |
|:--- | -------------:| --------:| ---:| ------:|:------ |
| x1  |   1.45666     |    Good  |   1 | -12.56 | 0.1200 |
| x2  | -23.14        |    Great |  56 |   0.13 | 0.3467 |
| x3  |   1.56734e-13 |    Bad   |   2 |   0.00 | <1e-15 |

Decimal points at the same offset in every row, pipes escaped, and the result parses as a Markdown.Table.

Costs

  • Markdown moves from the test-only dependency Escape pipes when writing a CoefTable as markdown #1007 added to a direct dependency (still a stdlib, but a new entry in [deps]/[compat], and it drops out of [extras]/[targets]).
  • The emitted spacing changes (|:--- | -------------:| rather than |:---|--------------:|), so both golden markdown tests get new expected output. Cell content is identical, and the rendered HTML is unaffected, as is the plain-text show method.

One thing to decide separately

The hand-rolled writer selects the alignment marker with j-1 in [ct.teststatcol; ct.pvalcol] while j indexes colnms, so the test-statistic column never receives its left-align marker and the p-value column receives it via the shift. That looks like an off-by-one, but changing it would change output beyond this refactor, so it is carried over verbatim with a comment. Happy to fix it here or in a follow-up, whichever you prefer.

#1007's regression test (a pipe in a header, a row name and a string cell; asserts equal cell counts per row and that the output parses as a Markdown.Table) is kept as-is and still passes. Full test suite passes locally on the rebased branch.

🤖 Generated with Claude Code

@andreasnoack
andreasnoack requested a review from palday August 21, 2026 11:58
@andreasnoack
andreasnoack force-pushed the an/coeftable-markdown-table branch from 877cced to 3636f83 Compare August 21, 2026 12:15
@andreasnoack
andreasnoack changed the base branch from an/escape-markdown-pipes to master August 21, 2026 12:15
`show(::IO, ::MIME"text/markdown", ::CoefTable)` reimplements the markdown
table format by hand, and in doing so never escapes `|`. Any cell containing
one therefore presents more cells than the alignment row, and the table stops
parsing as a table at all — downstream packages hit this with the R-style
`Pr(>|z|)` p-value header.

Rather than escaping by hand, stop reimplementing the format: render the rows
through Base's `print_matrix_row` as before — that is what produces the decimal
alignment — and hand the padded strings to `Markdown.Table`. Escaping and the
alignment rules then come from the stdlib.

The hand-rolled writer was justified by a comment saying the Markdown stdlib
"won't give us nice decimal alignment". It does not produce the alignment, but
it preserves it: `padcells!` adds the same width to every cell in a column, so
pre-padded equal-width cells keep their decimal points lined up.

Costs: Markdown becomes a (stdlib) dependency, and the emitted spacing changes,
so the two golden markdown tests get new expected output. Cell content is
unchanged, as is the rendered HTML and the plain-text show method.
@andreasnoack
andreasnoack force-pushed the an/coeftable-markdown-table branch from 3636f83 to 38dc069 Compare August 24, 2026 13:36
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