【问题标题】:How to extend Parsedown to add a class to table tags如何扩展 Parsedown 以将类添加到表标签
【发布时间】:2016-06-17 11:25:26
【问题描述】:

我想为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;
        }

}   

但这不起作用。

【问题讨论】:

    标签: php html-parsing php-extension parsedown


    【解决方案1】:

    我不太确定您的代码有什么问题,因为您的代码与我的一致。我只是添加了

    'attributes' => array(
          'class' => 'table table-responsive'
    ),
    

    识别表,在第 850 行使其变为

            $Block = array(
                'alignments' => $alignments,
                'identified' => true,
                'element' => array(
                    'name' => 'table',
                    'handler' => 'elements',
                    'attributes' => array(
                        'class' => 'table table-responsive',
                    ),
                ),
            );
    

    这对我来说很好用。但这对你来说似乎是一样的,减去表响应。

    你用的是什么版本?

    【讨论】:

    • 如果您想从 OP 获取更多信息,请使用 cmets。
    • 我愿意,但我还不能发表评论。我试图提供帮助,但我可以但网站不允许。
    • 这完全可以理解。它在 meta Stack Overflow 上讨论过很多次。一旦你有足够的reputation,你就可以comment on any post;相反,provide answers that don't require clarification from the asker.
    • 复制副本!谢谢
    • 刚刚查看了您的一个问题。现在你有足够的代表在任何地方发表评论。祝你好运:)
    【解决方案2】:

    我在 symfony 演示应用程序中遇到了完全相同的问题。 最后发现不是 parsedown,因为输出被 html-sanitizer 清理了。 允许表的类属性解决了这个问题。

    对于 symfony 4 演示应用添加到 config/packages/html_sanitizer.yaml:

    html_sanitizer:
      #[...]
      sanitizers:
        default:
          # [...]
          tags:
            table:
              allowed_attributes:
               - "class"
    

    【讨论】:

      【解决方案3】:

      我知道这是 5 年前 3 个月前提出的一个非常古老的问题,但我在 google 搜索中发现了这个答案,所以认为值得用输出表类的代码来回答。

      class Extension extends Parsedown {
      
          protected function blockTable($Line, ?array $Block = null)
          {   
              $Block = parent::blockTable($Line, $Block);
      
              if(is_null($Block)){ return; }
      
              $Block['element']['attributes']['class'] = 'table table-responsive';
              return $Block;
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-10
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 2013-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多