@@ 9-42 (lines=34) @@ | ||
6 | use Highlight\Highlighter; |
|
7 | use Parsedown; |
|
8 | ||
9 | class ParsedownHighlight extends Parsedown |
|
10 | { |
|
11 | protected $highlighter; |
|
12 | ||
13 | public function __construct() |
|
14 | { |
|
15 | $this->highlighter = new Highlighter; |
|
16 | } |
|
17 | ||
18 | protected function blockFencedCodeComplete($block) |
|
19 | { |
|
20 | if (! isset($block['element']['element']['attributes'])) { |
|
21 | return $block; |
|
22 | } |
|
23 | ||
24 | $code = $block['element']['element']['text']; |
|
25 | $languageClass = $block['element']['element']['attributes']['class']; |
|
26 | $language = explode('-', $languageClass); |
|
27 | ||
28 | try { |
|
29 | $highlighted = $this->highlighter->highlight($language[1], $code); |
|
30 | $block['element']['element']['attributes']['class'] = vsprintf('%s hljs %s', [ |
|
31 | $languageClass, |
|
32 | $highlighted->language, |
|
33 | ]); |
|
34 | $block['element']['element']['rawHtml'] = $highlighted->value; |
|
35 | unset($block['element']['element']['text']); |
|
36 | } catch (DomainException $e) { |
|
37 | // |
|
38 | } |
|
39 | ||
40 | return $block; |
|
41 | } |
|
42 | } |
|
43 |
@@ 9-42 (lines=34) @@ | ||
6 | use Highlight\Highlighter; |
|
7 | use ParsedownExtra; |
|
8 | ||
9 | class ParsedownHighlightExtra extends ParsedownExtra |
|
10 | { |
|
11 | protected $highlighter; |
|
12 | ||
13 | public function __construct() |
|
14 | { |
|
15 | $this->highlighter = new Highlighter; |
|
16 | } |
|
17 | ||
18 | protected function blockFencedCodeComplete($block) |
|
19 | { |
|
20 | if (! isset($block['element']['element']['attributes'])) { |
|
21 | return $block; |
|
22 | } |
|
23 | ||
24 | $code = $block['element']['element']['text']; |
|
25 | $languageClass = $block['element']['element']['attributes']['class']; |
|
26 | $language = explode('-', $languageClass); |
|
27 | ||
28 | try { |
|
29 | $highlighted = $this->highlighter->highlight($language[1], $code); |
|
30 | $block['element']['element']['attributes']['class'] = vsprintf('%s hljs %s', [ |
|
31 | $languageClass, |
|
32 | $highlighted->language, |
|
33 | ]); |
|
34 | $block['element']['element']['rawHtml'] = $highlighted->value; |
|
35 | unset($block['element']['element']['text']); |
|
36 | } catch (DomainException $e) { |
|
37 | // |
|
38 | } |
|
39 | ||
40 | return $block; |
|
41 | } |
|
42 | } |
|
43 |