webuni/commonmark-table-extension
Deprecated: GitHub-Flavored Markdown table support for league/commonmark. Functionality is now bundled in league/commonmark 1.3+ as League\CommonMark\Extension\Table—upgrade and use the built-in TableExtension for parsing/rendering tables.
Replace with Built-in Extension:
Since this package is deprecated, migrate to league/commonmark v1.3+ (bundled Table extension).
Update composer.json:
"league/commonmark": "^1.3"
Then configure the environment:
use League\CommonMark\Environment;
use League\CommonMark\Extension\Table\TableExtension;
$env = Environment::createCommonMarkEnvironment();
$env->addExtension(new TableExtension());
First Use Case: Test with a simple GFM table in a Blade template or controller:
| Syntax | Description |
|-------------|-------------|
| Header | Title |
| Paragraph | Text |
Render via:
$converter = new Converter(new DocParser($env), new HtmlRenderer($env));
echo $converter->convertToHtml($markdown);
Key Files to Review:
config/commonmark.php (if using Laravel’s spatie/laravel-markdown).league/commonmark v1.3 changelog.Laravel Integration:
AppServiceProvider:
public function boot()
{
$env = Environment::createCommonMarkEnvironment();
$env->addExtension(new TableExtension());
$this->app->singleton(Converter::class, fn() => new Converter(
new DocParser($env),
new HtmlRenderer($env)
));
}
Blade::directive('markdown', function ($expression) {
$converter = app(Converter::class);
return "<?php echo {$expression}->convertToHtml(" . $expression . "); ?>";
});
Usage:
@markdown($content)
Dynamic Table Styling:
Extend the HtmlRenderer to customize table classes/attributes:
$renderer = new HtmlRenderer($env);
$renderer->getNodeRendererRegistry()->addRenderer(
TableSection::class,
new class extends TableSectionRenderer {
public function render(TableSection $table, RenderContext $context): string {
$html = parent::render($table, $context);
return str_replace('<table', '<table class="custom-table"', $html);
}
}
);
API Documentation: Parse API response tables from Markdown:
$markdown = <<<MD
| Endpoint | Method | Description |
|----------------|--------|----------------------|
| `/users` | GET | List all users |
MD;
$html = $converter->convertToHtml($markdown);
CMS Content:
Store Markdown with tables in a database (e.g., content column) and render dynamically:
$post = Post::find(1);
$html = $converter->convertToHtml($post->content);
return view('posts.show', compact('html'));
Table Captions: Use MultiMarkdown syntax for captions:
| Name | Age |
|-------|-----|
| Alice | 30 |
[*Users*][users-table]
Rendered as:
<table>
<caption id="users-table">Users</caption>
<!-- table content -->
</table>
Alignment Control:
Leverage GFM alignment syntax (:---, :--:, ---:) for left/center/right alignment:
| Left-Aligned | Center-Aligned | Right-Aligned |
|:-------------|:--------------:|--------------:|
| Left | Center | Right |
Nested Tables: Supported natively (though visually limited in HTML):
| Outer Table |
|-------------|
| |
| | Inner | |
| | Table | |
| |-----------|
| | Cell 1.1 | |
Deprecation Warning:
league/commonmark’s built-in TableExtension (v1.3+).use League\CommonMark\Ext\Table\TableExtension;
with:
use League\CommonMark\Extension\Table\TableExtension;
Double Escaping:
league/commonmark version.Alignment Quirks:
---:), not leading. Test edge cases like single-column tables:
| Right |
|------:|
| 123 |
Caption Limitations:
| A | B |
|---|---|
| 1 | 2 |
[*Caption*][ref]
Performance:
league/commonmark’s bundled extension.Malformed Tables:
league/commonmark's InlineParser to debug syntax errors:
$parser = new InlineParser($env);
$document = $parser->parse($markdown);
// Inspect $document for errors
HTML Output Issues:
HtmlRenderer to log raw HTML:
$renderer = new HtmlRenderer($env);
$renderer->getNodeRendererRegistry()->addRenderer(
TableSection::class,
new class extends TableSectionRenderer {
public function render(TableSection $table, RenderContext $context): string {
$html = parent::render($table, $context);
Log::debug('Table HTML:', ['html' => $html]);
return $html;
}
}
);
Alignment Not Rendering:
text-align style is applied in the rendered HTML. If missing, check for CSS conflicts or custom renderer overrides.Custom Renderers:
Extend TableSectionRenderer to modify table structure:
class CustomTableRenderer extends TableSectionRenderer {
public function render(TableSection $table, RenderContext $context): string {
$html = parent::render($table, $context);
return str_replace('<table', '<table data-custom="true"', $html);
}
}
Register it:
$renderer->getNodeRendererRegistry()->addRenderer(
TableSection::class,
new CustomTableRenderer()
);
Preprocessing Markdown:
Use Laravel’s Str::of() or regex to transform tables before parsing:
$markdown = Str::of($input)
->replaceMatches('/\|(.*)\|/', '| **$1** |') // Bold headers
->toString();
Post-Processing HTML: Use DOMDocument to modify the rendered HTML:
$dom = new DOMDocument();
@$dom->loadHTML($html);
$tables = $dom->getElementsByTagName('table');
foreach ($tables as $table) {
$table->setAttribute('class', 'data-table');
}
$cleanHtml = $dom->saveHTML();
Laravel Cache:
If using spatie/laravel-markdown, clear the cache after enabling the TableExtension:
php artisan cache:clear
Environment Order: Extensions must be added before the converter is instantiated. Order matters for conflicts:
$env = Environment::createCommonMarkEnvironment();
$env->addExtension(new TableExtension()); // Must be first
$env->addExtension(new YourCustomExtension());
PHP Version:
Requires PHP 7.1+. Use composer require league/commonmark:^1.3 to auto-resolve dependencies.
How can I help you explore Laravel packages today?