Repository navigation
Tables: add table directive with column width control - #2852
Conversation
Made-with: Cursor
Made-with: Cursor
🔍 Preview links for changed docs |
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
|
Looking forward to trying this out! |
There was a problem hiding this comment.
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
TableDirectiveBlockparsing/validation for:widths:(including thedescriptionpreset and customN-N-...values). - Adds HTML rendering support that injects
<colgroup>andtable-layout:fixedwhen 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. |
| { | ||
| 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; |
There was a problem hiding this comment.
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.
| public void ValidateTableColumnCount() | ||
| { | ||
| var table = this.OfType<Markdig.Extensions.Tables.Table>().FirstOrDefault(); | ||
|
|
||
| if (table is null) | ||
| { |
There was a problem hiding this comment.
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).
| 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)..]; |
There was a problem hiding this comment.
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.
| ### 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. |
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
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.
- 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]>
What
{table}directive that wraps pipe tables and controls column widths via a 12-unit grid (similar to Bootstrap)auto(default, browser determines widths from content),description(4-8 for term/description)4-8,2-4-6,4-4-4)<colgroup>andtable-layout:fixedwhen widths are specified so browsers respect column proportionsWhy
Notes