Skip to content

Commit

Permalink
Table alignment config options
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Feb 23, 2023
1 parent 843213d commit 15a6005
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
- `heading_permalink/apply_id_to_heading` - When `true`, the `id` attribute will be applied to the heading element itself instead of the `<a>` tag
- `heading_permalink/heading_class` - class to apply to the heading element
- `heading_permalink/insert` - now accepts `none` to prevent the creation of the `<a>` link
- Added new `table/alignment_attributes` configuration option to control how table cell alignment is rendered (#959)

### Changed

Expand Down
37 changes: 37 additions & 0 deletions docs/2.4/extensions/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ $config = [
'tag' => 'div',
'attributes' => [],
],
'alignment_attributes' => [
'left' => ['align' => 'left'],
'center' => ['align' => 'center'],
'right' => ['align' => 'right'],
],
],
];

Expand Down Expand Up @@ -122,6 +127,38 @@ $config = [
];
```

### Alignment

You can configure the HTML attributes used for alignment via the `alignment_attributes` option. For example, if you wanted Bootstrap classes to be applied, you could do so like this:

```php
$config = [
'table' => [
'alignment_attributes' => [
'left' => ['class' => 'text-start'],
'center' => ['class' => 'text-center'],
'right' => ['class' => 'text-end'],
],
],
];
```

Or you could use inline styles:

```php
$config = [
'table' => [
'alignment_attributes' => [
'left' => ['style' => 'text-align:left'],
'center' => ['style' => 'text-align:center'],
'right' => ['style' => 'text-align:right'],
],
],
];
```

Or any other HTML attributes you'd like!

## Credits

The Table functionality was originally built by [Martin Hasoň](https://github.com/hason) and [Webuni s.r.o.](https://www.webuni.cz) before it was merged into the core parser.
23 changes: 20 additions & 3 deletions src/Extension/Table/TableCellRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace League\CommonMark\Extension\Table;

use League\CommonMark\Extension\Attributes\Util\AttributesHelper;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
Expand All @@ -23,6 +24,23 @@

final class TableCellRenderer implements NodeRendererInterface, XmlNodeRendererInterface
{
private const DEFAULT_ATTRIBUTES = [
TableCell::ALIGN_LEFT => ['align' => 'left'],
TableCell::ALIGN_CENTER => ['align' => 'center'],
TableCell::ALIGN_RIGHT => ['align' => 'right'],
];

/** @var array<TableCell::ALIGN_*, array<string, string|string[]|bool>> */
private array $alignmentAttributes;

/**
* @param array<TableCell::ALIGN_*, array<string, string|string[]|bool>> $alignmentAttributes
*/
public function __construct(array $alignmentAttributes = self::DEFAULT_ATTRIBUTES)
{
$this->alignmentAttributes = $alignmentAttributes;
}

/**
* @param TableCell $node
*
Expand All @@ -35,9 +53,8 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): \
TableCell::assertInstanceOf($node);

$attrs = $node->data->get('attributes');

if ($node->getAlign() !== null) {
$attrs['align'] = $node->getAlign();
if (($alignment = $node->getAlign()) !== null) {
$attrs = AttributesHelper::mergeAttributes($attrs, $this->alignmentAttributes[$alignment]);
}

$tag = $node->getType() === TableCell::TYPE_HEADER ? 'th' : 'td';
Expand Down
12 changes: 11 additions & 1 deletion src/Extension/Table/TableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ final class TableExtension implements ConfigurableExtensionInterface
{
public function configureSchema(ConfigurationBuilderInterface $builder): void
{
$attributeArraySchema = Expect::arrayOf(
Expect::type('string|string[]|bool'), // attribute value(s)
'string' // attribute name
);

$builder->addSchema('table', Expect::structure([
'wrap' => Expect::structure([
'enabled' => Expect::bool()->default(false),
'tag' => Expect::string()->default('div'),
'attributes' => Expect::arrayOf(Expect::string()),
]),
'alignment_attributes' => Expect::structure([
'left' => $attributeArraySchema,
'center' => $attributeArraySchema,
'right' => $attributeArraySchema,
]),
]));
}

Expand All @@ -47,6 +57,6 @@ public function register(EnvironmentBuilderInterface $environment): void
->addRenderer(Table::class, $tableRenderer)
->addRenderer(TableSection::class, new TableSectionRenderer())
->addRenderer(TableRow::class, new TableRowRenderer())
->addRenderer(TableCell::class, new TableCellRenderer());
->addRenderer(TableCell::class, new TableCellRenderer($environment->getConfiguration()->get('table/alignment_attributes')));
}
}
17 changes: 17 additions & 0 deletions tests/unit/Extension/Table/TableCellRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public function testRenderWithTableCellHavingAlignment(): void
$this->assertSame('<td class="foo" align="center">::children::</td>', (string) $renderer->render($tableCell, $childRenderer));
}

public function testRenderWithTableCellWithCustomAttributes(): void
{
$tableCell = new TableCell(TableCell::TYPE_DATA, TableCell::ALIGN_CENTER);
$tableCell->data->set('attributes/class', 'foo');

$childRenderer = new FakeChildNodeRenderer();
$childRenderer->pretendChildrenExist();

$renderer = new TableCellRenderer([
'left' => ['class' => 'foo', 'style' => 'text-align: left'],
'center' => ['class' => 'bar', 'style' => 'text-align: center'],
'right' => ['class' => 'baz', 'style' => 'text-align: right'],
]);

$this->assertSame('<td class="foo bar" style="text-align: center">::children::</td>', (string) $renderer->render($tableCell, $childRenderer));
}

public function testRenderWithWrongType(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down

0 comments on commit 15a6005

Please sign in to comment.