diff --git a/app/extensions/ParsedownTableExtension.php b/app/extensions/ParsedownTableExtension.php index 8e5aff8..16e4899 100644 --- a/app/extensions/ParsedownTableExtension.php +++ b/app/extensions/ParsedownTableExtension.php @@ -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; + + 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 itself: + if (!isset($Block['element']['attributes'])) { + $Block['element']['attributes'] = []; + } + $Block['element']['attributes']['class'] = 'table is-bordered'; + + // Wrap the
in a
: + $wrapped = [ + 'name' => 'div', + 'attributes' => [ + 'class' => 'table-container', + ], + 'handler' => 'elements', + 'text' => [ + $Block['element'], // the
itself + ], + ]; + + // Replace the original element with our wrapped version: + $Block['element'] = $wrapped; + } - // wrap the table in a bulma div - $Block['element'] = [ - 'name' => 'div', - 'attributes' => [ - 'class' => 'table-container' - ], - 'handler' => 'element', - 'text' => $Block['element'], - ]; return $Block; } } \ No newline at end of file