updatd Parsedown TableExtension to correctly wrap <table> inside <div> with appropriate bulma classes

This commit is contained in:
tp_dhu 2025-04-03 13:22:56 +01:00
parent 9f7dab2a36
commit 88b832dd03

View File

@ -2,27 +2,65 @@
class ParsedownTableExtension extends Parsedown
{
protected function blockTable($Line, ?array $Block = null)
protected function blockTable($Line, array $Block = null)
{
// Let Parsedown do its normal 'start-of-table' parsing.
$Block = parent::blockTable($Line, $Block);
if(!isset($Block)){
// If this line didn't create or start a table, do nothing.
if (!isset($Block)) {
return null;
}
// add classes to the table element
$Block['element']['attributes'] = [
'class' => 'table is-bordered',
];
// Flag it so we know in blockTableComplete that this is a table block.
$Block['isMyTable'] = true;
// wrap the table in a bulma div
$Block['element'] = [
return $Block;
}
protected function blockTableContinue($Line, array $Block)
{
// Continue letting Parsedown do its normal table parsing.
$Block = parent::blockTableContinue($Line, $Block);
return $Block;
}
protected function blockTableComplete(array $Block)
{
// Let Parsedown finalize the table structure.
// $Block = parent::blockTableComplete($Block);
// If we flagged this as our table block, wrap it now.
if (!empty($Block['isMyTable'])) {
// $Block['element'] should now be fully formed, e.g.:
// [
// 'name' => 'table',
// 'handler' => 'elements',
// 'text' => [ ... ],
// 'attributes' => [...],
// ]
// Add your custom class to the <table> itself:
if (!isset($Block['element']['attributes'])) {
$Block['element']['attributes'] = [];
}
$Block['element']['attributes']['class'] = 'table is-bordered';
// Wrap the <table> in a <div class="table-container">:
$wrapped = [
'name' => 'div',
'attributes' => [
'class' => 'table-container'
'class' => 'table-container',
],
'handler' => 'elements',
'text' => [
$Block['element'], // the <table> itself
],
'handler' => 'element',
'text' => $Block['element'],
];
// Replace the original element with our wrapped version:
$Block['element'] = $wrapped;
}
return $Block;
}
}