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:
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
]);
}
<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?
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.