updatd Parsedown TableExtension to correctly wrap <table> inside <div> with appropriate bulma classes
This commit is contained in:
parent
9f7dab2a36
commit
88b832dd03
@ -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 <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;
|
||||
}
|
||||
|
||||
// wrap the table in a bulma div
|
||||
$Block['element'] = [
|
||||
'name' => 'div',
|
||||
'attributes' => [
|
||||
'class' => 'table-container'
|
||||
],
|
||||
'handler' => 'element',
|
||||
'text' => $Block['element'],
|
||||
];
|
||||
return $Block;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user