1 | <?php |
||
14 | class MarkdownEngine extends \Parsedown |
||
15 | { |
||
16 | /** |
||
17 | * Whether or not to allow images to be rendered. If set to false, it'll be rendered as a hyperlink |
||
18 | * @var bool |
||
19 | */ |
||
20 | protected $allowImages; |
||
21 | |||
22 | /** |
||
23 | * Store all of the settings related to Camo |
||
24 | * @var array |
||
25 | */ |
||
26 | private $camo; |
||
27 | |||
28 | /** |
||
29 | * Constructor for our extension of Parsedown |
||
30 | */ |
||
31 | 1 | public function __construct() |
|
32 | { |
||
33 | 1 | $this->camo = array(); |
|
34 | 1 | $this->camo['enabled'] = \Service::getParameter('bzion.features.camo.enabled'); |
|
35 | |||
36 | 1 | if ($this->camo['enabled']) { |
|
37 | $this->camo['key'] = \Service::getParameter('bzion.features.camo.key'); |
||
38 | $this->camo['base_url'] = \Service::getParameter('bzion.features.camo.base_url'); |
||
39 | $this->camo['whitelisted_domains'] = \Service::getParameter('bzion.features.camo.whitelisted_domains'); |
||
40 | } |
||
41 | 1 | } |
|
42 | |||
43 | /** |
||
44 | * Overload the function to either return nothing (if image image rendering is disable) or return the |
||
45 | * rendered image by calling the Parsedown function |
||
46 | * |
||
47 | * @param string $Excerpt The excerpt of text to be processed as an image |
||
48 | * |
||
49 | * @return array|null|void |
||
50 | */ |
||
51 | protected function inlineImage($Excerpt) |
||
73 | |||
74 | /** |
||
75 | * Overload the function replace the scheme + host of the link if belongs to the current website serving the link. |
||
76 | * This will prevent CSS rules from treating full URLs as external links. |
||
77 | * |
||
78 | * @param array $Excerpt The structure of the link to be processed |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | protected function inlineLink($Excerpt) |
||
83 | { |
||
84 | $link = parent::inlineLink($Excerpt); |
||
85 | |||
86 | if (isset($link['element']['attributes']['href'])) { |
||
87 | $href = &$link['element']['attributes']['href']; |
||
88 | |||
89 | if (strpos($href, $scheme = \Service::getRequest()->getSchemeAndHttpHost()) === 0) { |
||
90 | $href = str_replace($scheme, '', $href); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | return $link; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Overload this function to add IDs to the headings (with pound signs) so they can be used as anchors |
||
99 | * |
||
100 | * @param array $Line |
||
101 | * |
||
102 | * @return array|null |
||
103 | */ |
||
104 | protected function blockHeader($Line) |
||
105 | { |
||
106 | $Block = parent::blockHeader($Line); |
||
107 | |||
108 | if (isset($Block['element']['text'])) |
||
109 | { |
||
110 | $Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
||
111 | } |
||
112 | |||
113 | return $Block; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Overload this function to add IDs to headings (with horizontal dashes) so they can be used as anchors |
||
118 | * |
||
119 | * @param array $Line |
||
120 | * @param array|null $Block |
||
121 | * |
||
122 | * @return array|null |
||
123 | */ |
||
124 | 1 | protected function blockSetextHeader($Line, array $Block = null) |
|
125 | { |
||
126 | 1 | $Block = parent::blockSetextHeader($Line, $Block); |
|
127 | |||
128 | 1 | if (isset($Block['element']['name'])) |
|
129 | { |
||
130 | $element = $Block['element']['name']; |
||
131 | |||
132 | if ($element == 'h1' || $element == 'h2') |
||
133 | { |
||
134 | $Block['element']['attributes']['id'] = $this->slugifyHeader($Block); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | 1 | return $Block; |
|
139 | } |
||
140 | |||
141 | private function slugifyHeader($Block) |
||
149 | |||
150 | /** |
||
151 | * Disable or enable Parsedown from rendering images |
||
152 | * |
||
153 | * @param bool $allowImages Whether to allow images to be rendered or not |
||
154 | */ |
||
155 | 1 | public function setAllowImages($allowImages) |
|
159 | } |
||
160 |