league/commonmark-ext-table
Deprecated CommonMark table extension for PHP. Adds GitHub Flavored Markdown-style tables (alignment, header/body) to league/commonmark environments. Use league/commonmark 1.3+ instead, which includes the same Table extension under League\CommonMark\Extension\Table.
Check Laravel’s commonmark Version:
Verify your project uses league/commonmark <1.3 (this package is deprecated in favor of the bundled extension).
composer show league/commonmark
If ≥1.3, skip to Implementation Patterns and use the built-in TableExtension.
Install the Extension (if using <1.3):
composer require league/commonmark-ext-table
Basic Integration:
Add the extension to your Environment in a service provider (e.g., AppServiceProvider):
use League\CommonMark\Environment;
use League\CommonMark\Ext\Table\TableExtension;
public function boot()
{
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new TableExtension());
// Store for later use (e.g., in a Markdown service)
app()->singleton('commonmark.environment', fn() => $environment);
}
First Use Case: Parse a Markdown string with tables into HTML:
use League\CommonMark\Converter;
use League\CommonMark\DocParser;
use League\CommonMark\HtmlRenderer;
$environment = app('commonmark.environment');
$converter = new Converter(new DocParser($environment), new HtmlRenderer($environment));
$markdown = <<<MD
| Name | Role |
|----------|---------------|
| Alice | Developer |
| Bob | Designer |
MD;
echo $converter->convert($markdown);
Output:
<table>
<thead>
<tr><th>Name</th><th>Role</th></tr>
</thead>
<tbody>
<tr><td>Alice</td><td>Developer</td></tr>
<tr><td>Bob</td><td>Designer</td></tr>
</tbody>
</table>
Pattern: Register the configured Environment as a singleton in Laravel’s container for reuse across controllers/services.
// In AppServiceProvider::boot()
app()->singleton('markdown.converter', function ($app) {
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new TableExtension());
return new Converter(
new DocParser($environment),
new HtmlRenderer($environment)
);
});
Usage:
// In a controller
$converter = app('markdown.converter');
$html = $converter->convert($markdownContent);
Pattern: Create a Blade directive to render Markdown tables inline.
// In AppServiceProvider::boot()
Blade::directive('markdown', function ($expression) {
return "<?php echo app('markdown.converter')->convert({$expression}); ?>";
});
Usage in Blade:
@markdown($markdownVariable)
Pattern: Convert Markdown to HTML in API responses (e.g., for documentation).
use Illuminate\Http\Response;
public function getDocumentation()
{
$markdown = file_get_contents(storage_path('docs/api.md'));
$html = app('markdown.converter')->convert($markdown);
return response($html, 200)
->header('Content-Type', 'text/html');
}
Pattern: Extend the HtmlRenderer to add custom attributes (e.g., class names).
use League\CommonMark\Renderer\Node\TableRenderer;
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new TableExtension());
// Custom renderer
$environment->addRenderer(new class extends TableRenderer {
public function renderTable($node, NodeRendererInterface $htmlRenderer)
{
$htmlRenderer->getNodeData($node)->setAttribute('class', 'custom-table');
return parent::renderTable($node, $htmlRenderer);
}
});
Pattern: Sanitize Markdown input to prevent XSS (e.g., in user-generated content).
use League\CommonMark\Util\HtmlElement;
$environment = Environment::createCommonMarkEnvironment();
$environment->addExtension(new TableExtension());
// Sanitize HTML output
$html = app('markdown.converter')->convert($markdown);
$cleanHtml = preg_replace('/<[^>]*>/', '', $html); // Example: Strip all tags (use DOM for production)
Pattern: Use PHPUnit to test table rendering.
public function testTableRendering()
{
$markdown = "| Header | Data |\n|--------|------|\n| Row 1 | 123 |";
$html = app('markdown.converter')->convert($markdown);
$this->assertStringContainsString('<table>', $html);
$this->assertStringContainsString('<th>Header</th>', $html);
}
League\CommonMark\Extension\Table in league/commonmark ≥1.3.league/commonmark:^1.3 and use:
use League\CommonMark\Extension\Table\TableExtension;
$environment->addExtension(new TableExtension());
composer update league/commonmark to migrate seamlessly.:---, :--:, ---:) may not render as expected if the separator line has inconsistent spacing.
| Left | Center | Right |
|:------|:------:|------:| ❌ (Extra space after `:------`)
| Left | Center | Right |
|:-----|:-----:|------:| ✅
<table> directly in Markdown cells or flatten the structure.class="table" becomes class=""table"").league/commonmark-ext-table:^2.1.0 or use the bundled extension.Converter instance (Laravel’s service container handles this automatically) or pre-render tables to HTML.style="text-align: center") may conflict with CSS frameworks like Bootstrap.// In a custom renderer
$htmlRenderer->getNodeData($node)->setAttribute('class', 'text-center');
Then style with:
.text-center { text-align: center; }
$environment->addExtension(new \League\CommonMark\Extension\DebugExtension());
This adds debug comments to HTML output (e.g., <!-- DEBUG: Node type -->).TableRenderer to add features like:
class CustomTableRenderer extends TableRenderer {
public function renderTable($node, NodeRendererInterface $htmlRenderer)
{
if (auth()->user()->cannot('view-tables')) {
return '';
}
return parent::renderTable($node, $htmlRenderer);
}
}
Register it:
$environment->addRenderer(new CustomTableRenderer());
laravel-markdown for Blade integration:
composer require darkaonline/laravel-markdown
Usage:
{!! Markdown::parse($markdownWithTables) !!}
league/commonmark-ext-table with an incompatible league/commonmark version (eHow can I help you explore Laravel packages today?