Skip to content

Tables: add table directive with column width control - #2852

Merged
reakaleek merged 7 commits into
mainfrom
feature/table-directive-column-widths
Mar 13, 2026
Merged

reakaleek merged 7 commits into
mainfrom
feature/table-directive-column-widths

Conversation

@reakaleek

@reakaleek reakaleek commented Mar 4, 2026 •

Copy link
Copy Markdown
Member

What

  • Add {table} directive that wraps pipe tables and controls column widths via a 12-unit grid (similar to Bootstrap)
  • Support presets: auto (default, browser determines widths from content), description (4-8 for term/description)
  • Support custom widths: dash-separated values that sum to 12 (e.g. 4-8, 2-4-6, 4-4-4)
  • Inject <colgroup> and table-layout:fixed when widths are specified so browsers respect column proportions

Why

  • Authors need to control table column widths for definition lists, API parameter tables, and asymmetric layouts
  • Without explicit widths, the browser sizes columns from content, which often looks wrong for narrow keys vs wide descriptions

Notes

  • Column count validation runs at render time, not during block close, because Markdig converts pipe tables from paragraphs to table blocks during inline parsing (after the directive block is closed)
  • Uses invariant culture for decimal formatting in colgroup so widths render consistently across locales

@reakaleek
reakaleek requested review from a team as code owners March 4, 2026 21:08
@reakaleek
reakaleek requested a review from Mpdreamz March 4, 2026 21:08
@github-actions

github-actions Bot commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

🔍 Preview links for changed docs

reakaleek and others added 2 commits March 4, 2026 22:21
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>

@theletterf theletterf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yay!

@eedugon

eedugon commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Looking forward to trying this out!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a new {table} MyST directive to the Elastic.Markdown pipeline, enabling optional column-width control for pipe tables via a 12-unit grid and rendering support via a dedicated Razor slice.

Changes:

  • Introduces TableDirectiveBlock parsing/validation for :widths: (including the description preset and custom N-N-... values).
  • Adds HTML rendering support that injects <colgroup> and table-layout:fixed when column widths are provided.
  • Adds directive-focused tests and extends documentation with usage examples and validation rules.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/Elastic.Markdown.Tests/Directives/TableDirectiveTests.cs New unit tests for parsing, rendering, and validation scenarios of {table} widths.
src/Elastic.Markdown/Myst/Directives/Table/TableDirectiveBlock.cs New directive block implementation with :widths: parsing and validation logic.
src/Elastic.Markdown/Myst/Directives/Table/TableDirectiveViewModel.cs Renders directive content and injects <colgroup> + fixed layout styling when widths are specified.
src/Elastic.Markdown/Myst/Directives/Table/TableDirectiveView.cshtml Razor slice view that outputs the rendered table HTML.
src/Elastic.Markdown/Myst/Directives/DirectiveHtmlRenderer.cs Routes TableDirectiveBlock to the new Razor slice renderer.
src/Elastic.Markdown/Myst/Directives/DirectiveBlockParser.cs Recognizes {table} and instantiates TableDirectiveBlock.
docs/syntax/tables.md Documents the new {table} directive, presets, examples, and validation rules.

Comment on lines +101 to +106
{
if (!int.TryParse(part, out var unit) || unit < 1 || unit > 12)
{
block.EmitError(
$"Invalid widths value '{value}'. Use preset (even, definition) or dash-separated integers 1-12 that sum to 12 (e.g., 4-8, 4-4-4).");
return null;

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

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

The user-facing validation text is inconsistent with the actual supported presets. This error message (and the XML docs above) mention presets even/definition, but the implementation recognizes auto (default) and description. Please update the message/docs to reflect the real accepted values so users aren’t guided to invalid options.

Copilot uses AI. Check for mistakes.
Comment on lines +57 to +62
public void ValidateTableColumnCount()
{
var table = this.OfType<Markdig.Extensions.Tables.Table>().FirstOrDefault();

if (table is null)
{

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

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

ValidateTableColumnCount() only checks the first Table child via FirstOrDefault(). If the directive contains multiple pipe tables, only the first one is validated/targeted (and column widths would be injected into the first <table> in the rendered HTML). This conflicts with the documentation statement that the directive must contain exactly one pipe table; consider detecting multiple tables and emitting an error (or relax/update the docs).

Copilot uses AI. Check for mistakes.
Comment on lines +39 to +43
var openingTag = html[tableIndex..bracketEnd];
var hasTableLayout = openingTag.Contains("table-layout", StringComparison.OrdinalIgnoreCase);
var styleAttr = hasTableLayout ? "" : " style=\"table-layout:fixed\"";
var newOpening = html[tableIndex..bracketEnd] + styleAttr + ">";
var result = html[..tableIndex] + newOpening + colgroup + html[(bracketEnd + 1)..];

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

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

When the rendered <table> already has a style="..." attribute but does not mention table-layout, this logic will append a second style attribute (..." style="table-layout:fixed"), producing invalid HTML. Consider detecting an existing style= attribute and appending table-layout:fixed into it instead of adding a new attribute.

Copilot uses AI. Check for mistakes.
Comment thread docs/syntax/tables.md
Comment on lines +306 to +310
### Validation

- Widths must sum to 12.
- The number of width values must match the table column count.
- The directive must contain exactly one pipe table.

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

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

The docs state that the {table} directive must contain exactly one pipe table, but the current implementation only checks that at least one table exists (and uses the first if multiple are present). Either tighten the implementation to enforce the single-table constraint or update this bullet to match actual behavior.

Copilot uses AI. Check for mistakes.
Comment on lines +95 to +107
private static List<int>? ParseCustomWidths(string value, TableDirectiveBlock block)
{
var parts = value.Split('-', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
var result = new List<int>(parts.Length);

foreach (var part in parts)
{
if (!int.TryParse(part, out var unit) || unit < 1 || unit > 12)
{
block.EmitError(
$"Invalid widths value '{value}'. Use preset (even, definition) or dash-separated integers 1-12 that sum to 12 (e.g., 4-8, 4-4-4).");
return null;
}

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

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

There’s no test coverage for invalid :widths: values (non-integers / out-of-range units / malformed strings) even though ParseCustomWidths has a dedicated error path. Adding a test for an invalid widths token (e.g., :widths: 0-12 or :widths: foo) would help prevent regressions in validation behavior.

Copilot uses AI. Check for mistakes.
reakaleek and others added 2 commits March 13, 2026 11:12
- Use OfType/LINQ instead of manual foreach loop
- Fix error message and XML docs to reference correct presets (auto, description)
- Detect and reject multiple pipe tables in directive
- Merge table-layout:fixed into existing style attribute instead of duplicating
- Use GeneratedRegexAttribute for compile-time regex
- Add tests for invalid widths, out-of-range values, and multiple tables

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@reakaleek
reakaleek merged commit 05f168a into main Mar 13, 2026
27 checks passed
@reakaleek
reakaleek deleted the feature/table-directive-column-widths branch March 13, 2026 10:37
@cotti cotti mentioned this pull request May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants