1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Engines; |
4
|
|
|
|
5
|
|
|
use Highlight\Highlighter; |
6
|
|
|
|
7
|
|
|
class MarkdownEngine extends \Parsedown |
8
|
|
|
{ |
9
|
|
|
protected $highlighter; |
10
|
|
|
|
11
|
2 |
|
public function __construct () |
12
|
|
|
{ |
13
|
2 |
|
$this->highlighter = new Highlighter(); |
14
|
2 |
|
} |
15
|
|
|
|
16
|
1 |
|
protected function blockHeader($line) |
17
|
|
|
{ |
18
|
1 |
|
$Block = parent::blockHeader($line); |
19
|
|
|
|
20
|
|
|
// Create our unique ids by sanitizing the header content |
21
|
1 |
|
$id = strtolower($Block['element']['text']); |
22
|
1 |
|
$id = str_replace(' ', '-', $id); |
23
|
1 |
|
$id = preg_replace('/[^0-9a-zA-Z-_]/', '', $id); |
24
|
1 |
|
$id = preg_replace('/-+/', '-', $id); |
25
|
|
|
|
26
|
1 |
|
$Block['element']['attributes']['id'] = $id; |
27
|
|
|
|
28
|
1 |
|
return $Block; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function blockFencedCodeComplete ($block) |
32
|
|
|
{ |
33
|
|
|
// The class has a `language-` prefix, remove this to get the language |
34
|
|
|
if (isset($block['element']['text']['attributes'])) |
35
|
|
|
{ |
36
|
|
|
$language = substr($block['element']['text']['attributes']['class'], 9); |
37
|
|
|
|
38
|
|
|
try |
39
|
|
|
{ |
40
|
|
|
$highlighted = $this->highlighter->highlight($language, $block['element']['text']['text']); |
41
|
|
|
$block['element']['text']['text'] = $highlighted->value; |
42
|
|
|
|
43
|
|
|
// Only return the block if Highlighter knew the language and how to handle it. |
44
|
|
|
return $block; |
45
|
|
|
} |
46
|
|
|
// Exception thrown when language not supported, so just catch it and ignore it. |
47
|
|
|
catch (\DomainException $exception) {} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return parent::blockFencedCodeComplete($block); |
51
|
|
|
} |
52
|
|
|
} |