66 lines
2.1 KiB
PHP
66 lines
2.1 KiB
PHP
<?php
|
|
|
|
class ParsedownTableExtension extends Parsedown
|
|
{
|
|
protected function blockTable($Line, array $Block = null)
|
|
{
|
|
// Let Parsedown do its normal 'start-of-table' parsing.
|
|
$Block = parent::blockTable($Line, $Block);
|
|
|
|
// If this line didn't create or start a table, do nothing.
|
|
if (!isset($Block)) {
|
|
return null;
|
|
}
|
|
|
|
// 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 <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',
|
|
],
|
|
'handler' => 'elements',
|
|
'text' => [
|
|
$Block['element'], // the <table> itself
|
|
],
|
|
];
|
|
|
|
// Replace the original element with our wrapped version:
|
|
$Block['element'] = $wrapped;
|
|
}
|
|
|
|
return $Block;
|
|
}
|
|
} |