duanjiong2021 2017-09-21 13:39
浏览 128
已采纳

如何为解析后的table元素添加class属性?

I have some markdown files and I want to display them on my blog which developed by Laravel.

So I think out such a solution:

Controller

public function display() {
    $pd = new Parsedown();
    $text = Storage::disk('local')->get('x.md');
    $html = $pd->text($text);
    $title = "title";
    return view('x.y', [
        "html" => $html,
        "title" => $title
    ]);
}

Blade

<div class="container-fluid">
    <div class="row">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title text-center">{{ $title }}</h3>
            </div>
            <div class="panel-body">
                {!! $html !!}
            </div>
        </div>
    </div>
</div>

It works well except table element:

Parsedown parse table like that:

header 1 | header 2
-------- | --------
cell 1.1 | cell 1.2
cell 2.1 | cell 2.2

into:

<table>
<thead>
<tr>
<th>header 1</th>
<th>header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>cell 1.1</td>
<td>cell 1.2</td>
</tr>
<tr>
<td>cell 2.1</td>
<td>cell 2.2</td>
</tr>
</tbody>
</table>

Then I got a table with no bootstrap styles, and it looks weird. What I want to get is:

<table class="table">
  ...
</table>

So, could anyone please give me some suggestions about that?

  • 写回答

2条回答 默认 最新

  • dongya3627 2017-09-21 14:20
    关注

    After I read the source code of Parsedown, I find a solution.

    In blockTable() method:

    change this:

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

    to:

            $Block = array(
                'alignments' => $alignments,
                'identified' => true,
                'element' => array(
                    'name' => 'table',
                    'handler' => 'elements',
                    'attributes' => [       //+
                        "class" => "table"  //+
                    ]                       //+
                ),
            );
    

    And it will output table element with class="table".

    Finally, I created a new class which extends Parsedown, and override that blockTable method with my own implementation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题