我想为 Parsedown 编写一个扩展这样我就可以向每个表标签添加默认类。我发现我可以通过在 blockTable
函数中添加行来分配属性(大约第 870 行)来成功破解源代码:
$Block = array(
'alignments' => $alignments,
'identified' => true,
'element' => array(
'name' => 'table',
'handler' => 'elements',
'attributes' => array(
'class' => 'table',
),
),
);
但是,如果我尝试宽松地遵循 Change Element Markup extension tutorial我不成功(可能是因为表解析可能是一个迭代过程,并且教程中的示例是简单的字符串替换。)
我已经尝试过:
class Extension extends Parsedown
{
protected function blockTable($Line, array $Block = null)
{
$Block = parent::blockTable($Line, array $Block = null);
$Block['table']['attributes']['class'] = 'table';
return $Block;
}
}
但这不起作用。
最佳答案
我不太确定您的代码有什么问题,因为您的代码与我的代码匹配。我只是添加了
'attributes' => array(
'class' => 'table table-responsive'
),
到identifyTable,在第850行,这样它就变成了
$Block = array(
'alignments' => $alignments,
'identified' => true,
'element' => array(
'name' => 'table',
'handler' => 'elements',
'attributes' => array(
'class' => 'table table-responsive',
),
),
);
这对我来说效果很好。但这对你来说似乎是一样的,减去表响应。
您使用的是什么版本?
关于php - 如何扩展 Parsedown 以将类添加到表标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35783984/