1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Cecil\Converter; |
15
|
|
|
|
16
|
|
|
use Cecil\Assets\Asset; |
17
|
|
|
use Cecil\Assets\Image; |
18
|
|
|
use Cecil\Builder; |
19
|
|
|
use Cecil\Exception\RuntimeException; |
20
|
|
|
use Cecil\Util; |
21
|
|
|
use Highlight\Highlighter; |
22
|
|
|
|
23
|
|
|
class Parsedown extends \ParsedownToC |
24
|
|
|
{ |
25
|
|
|
/** @var Builder */ |
26
|
|
|
protected $builder; |
27
|
|
|
|
28
|
|
|
/** @var \Cecil\Config */ |
29
|
|
|
protected $config; |
30
|
|
|
|
31
|
|
|
/** {@inheritdoc} */ |
32
|
|
|
protected $regexAttribute = '(?:[#.][-\w:\\\]+[ ]*|[-\w:\\\]+(?:=(?:["\'][^\n]*?["\']|[^\s]+)?)?[ ]*)'; |
33
|
|
|
|
34
|
|
|
/** Regex who's looking for images */ |
35
|
|
|
protected $regexImage = "~^!\[.*?\]\(.*?\)~"; |
36
|
|
|
|
37
|
|
|
/** @var Highlighter */ |
38
|
|
|
protected $highlighter; |
39
|
|
|
|
40
|
1 |
|
public function __construct(Builder $builder, ?array $options = null) |
41
|
|
|
{ |
42
|
1 |
|
$this->builder = $builder; |
43
|
1 |
|
$this->config = $builder->getConfig(); |
44
|
|
|
|
45
|
|
|
// "insert" line block: ++text++ -> <ins>text</ins> |
46
|
1 |
|
$this->InlineTypes['+'][] = 'Insert'; |
47
|
1 |
|
$this->inlineMarkerList = implode('', array_keys($this->InlineTypes)); |
48
|
1 |
|
$this->specialCharacters[] = '+'; |
49
|
|
|
|
50
|
|
|
// Image block (to avoid paragraph) |
51
|
1 |
|
$this->BlockTypes['!'][] = 'Image'; |
52
|
|
|
|
53
|
|
|
// "notes" block |
54
|
1 |
|
$this->BlockTypes[':'][] = 'Note'; |
55
|
|
|
|
56
|
|
|
// code highlight |
57
|
1 |
|
$this->highlighter = new Highlighter(); |
58
|
|
|
|
59
|
|
|
// options |
60
|
1 |
|
$options = array_merge(['selectors' => (array) $this->config->get('pages.body.toc')], $options ?? []); |
61
|
|
|
|
62
|
1 |
|
parent::__construct($options); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Insert inline. |
67
|
|
|
* e.g.: ++text++ -> <ins>text</ins>. |
68
|
|
|
*/ |
69
|
1 |
|
protected function inlineInsert($Excerpt) |
70
|
|
|
{ |
71
|
1 |
|
if (!isset($Excerpt['text'][1])) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
if ($Excerpt['text'][1] === '+' && preg_match('/^\+\+(?=\S)(.+?)(?<=\S)\+\+/', $Excerpt['text'], $matches)) { |
76
|
1 |
|
return [ |
77
|
1 |
|
'extent' => \strlen($matches[0]), |
78
|
1 |
|
'element' => [ |
79
|
1 |
|
'name' => 'ins', |
80
|
1 |
|
'text' => $matches[1], |
81
|
1 |
|
'handler' => 'line', |
82
|
1 |
|
], |
83
|
1 |
|
]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
protected function inlineLink($Excerpt) |
91
|
|
|
{ |
92
|
1 |
|
$link = parent::inlineLink($Excerpt); |
93
|
|
|
|
94
|
1 |
|
if (!isset($link)) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Link to a page with "page:page_id" as URL |
99
|
1 |
|
if (Util\Str::startsWith($link['element']['attributes']['href'], 'page:')) { |
100
|
1 |
|
$link['element']['attributes']['href'] = new \Cecil\Assets\Url($this->builder, substr($link['element']['attributes']['href'], 5, \strlen($link['element']['attributes']['href']))); |
101
|
|
|
|
102
|
1 |
|
return $link; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// External link |
106
|
1 |
|
if (str_starts_with($link['element']['attributes']['href'], 'http') && !str_starts_with($link['element']['attributes']['href'], (string) $this->config->get('baseurl'))) { |
107
|
1 |
|
if ($this->config->get('pages.body.links.external.blank') ?? false) { |
108
|
|
|
$link['element']['attributes']['target'] = '_blank'; |
109
|
|
|
} |
110
|
1 |
|
if (!\array_key_exists('rel', $link['element']['attributes'])) { |
111
|
1 |
|
$link['element']['attributes']['rel'] = ''; |
112
|
|
|
} |
113
|
1 |
|
if ($this->config->get('pages.body.links.external.noopener') ?? true) { |
114
|
1 |
|
$link['element']['attributes']['rel'] .= ' noopener'; |
115
|
|
|
} |
116
|
1 |
|
if ($this->config->get('pages.body.links.external.noreferrer') ?? true) { |
117
|
1 |
|
$link['element']['attributes']['rel'] .= ' noreferrer'; |
118
|
|
|
} |
119
|
1 |
|
if ($this->config->get('pages.body.links.external.nofollow') ?? true) { |
120
|
1 |
|
$link['element']['attributes']['rel'] .= ' nofollow'; |
121
|
|
|
} |
122
|
1 |
|
$link['element']['attributes']['rel'] = trim($link['element']['attributes']['rel']); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* |
126
|
|
|
* Embed link? |
127
|
|
|
*/ |
128
|
1 |
|
$embed = false; |
129
|
1 |
|
$embed = (bool) $this->config->get('pages.body.links.embed.enabled') ?? false; |
130
|
1 |
|
if (isset($link['element']['attributes']['embed'])) { |
131
|
1 |
|
$embed = true; |
132
|
1 |
|
if ($link['element']['attributes']['embed'] == 'false') { |
133
|
1 |
|
$embed = false; |
134
|
|
|
} |
135
|
1 |
|
unset($link['element']['attributes']['embed']); |
136
|
|
|
} |
137
|
|
|
// video or audio? |
138
|
1 |
|
$extension = pathinfo($link['element']['attributes']['href'], PATHINFO_EXTENSION); |
139
|
1 |
|
if (\in_array($extension, (array) $this->config->get('pages.body.links.embed.video.ext'))) { |
140
|
1 |
|
if (!$embed) { |
141
|
1 |
|
$link['element']['attributes']['href'] = (string) new Asset($this->builder, $link['element']['attributes']['href'], ['force_slash' => false]); |
142
|
|
|
|
143
|
1 |
|
return $link; |
144
|
|
|
} |
145
|
1 |
|
$video = $this->createMediaFromLink($link, 'video'); |
146
|
1 |
|
if ((bool) $this->config->get('pages.body.images.caption.enabled')) { |
147
|
1 |
|
return $this->createFigure($video); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $video; |
151
|
|
|
} |
152
|
1 |
|
if (\in_array($extension, (array) $this->config->get('pages.body.links.embed.audio.ext'))) { |
153
|
1 |
|
if (!$embed) { |
154
|
1 |
|
$link['element']['attributes']['href'] = (string) new Asset($this->builder, $link['element']['attributes']['href'], ['force_slash' => false]); |
155
|
|
|
|
156
|
1 |
|
return $link; |
157
|
|
|
} |
158
|
1 |
|
$audio = $this->createMediaFromLink($link, 'audio'); |
159
|
1 |
|
if ((bool) $this->config->get('pages.body.images.caption.enabled')) { |
160
|
1 |
|
return $this->createFigure($audio); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $audio; |
164
|
|
|
} |
165
|
1 |
|
if (!$embed) { |
166
|
1 |
|
return $link; |
167
|
|
|
} |
168
|
|
|
// GitHub Gist link? |
169
|
|
|
// https://regex101.com/r/QmCiAL/1 |
170
|
1 |
|
$pattern = 'https:\/\/gist\.github.com\/[-a-zA-Z0-9_]+\/[-a-zA-Z0-9_]+'; |
171
|
1 |
|
if (preg_match('/' . $pattern . '/is', (string) $link['element']['attributes']['href'], $matches)) { |
172
|
1 |
|
$gist = [ |
173
|
1 |
|
'extent' => $link['extent'], |
174
|
1 |
|
'element' => [ |
175
|
1 |
|
'name' => 'script', |
176
|
1 |
|
'text' => $link['element']['text'], |
177
|
1 |
|
'attributes' => [ |
178
|
1 |
|
'src' => $matches[0] . '.js', |
179
|
1 |
|
'title' => $link['element']['attributes']['title'], |
180
|
1 |
|
], |
181
|
1 |
|
], |
182
|
1 |
|
]; |
183
|
1 |
|
if ((bool) $this->config->get('pages.body.images.caption.enabled')) { |
184
|
1 |
|
return $this->createFigure($gist); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $gist; |
188
|
|
|
} |
189
|
|
|
// Youtube link? |
190
|
|
|
// https://regex101.com/r/gznM1j/1 |
191
|
1 |
|
$pattern = '(?:https?:\/\/)?(?:www\.)?youtu(?:\.be\/|be.com\/\S*(?:watch|embed)(?:(?:(?=\/[-a-zA-Z0-9_]{11,}(?!\S))\/)|(?:\S*v=|v\/)))([-a-zA-Z0-9_]{11,})'; |
192
|
1 |
|
if (preg_match('/' . $pattern . '/is', (string) $link['element']['attributes']['href'], $matches)) { |
193
|
1 |
|
$iframe = [ |
194
|
1 |
|
'element' => [ |
195
|
1 |
|
'name' => 'iframe', |
196
|
1 |
|
'text' => $link['element']['text'], |
197
|
1 |
|
'attributes' => [ |
198
|
1 |
|
'width' => '560', |
199
|
1 |
|
'height' => '315', |
200
|
1 |
|
'title' => $link['element']['text'], |
201
|
1 |
|
'src' => 'https://www.youtube.com/embed/' . $matches[1], |
202
|
1 |
|
'frameborder' => '0', |
203
|
1 |
|
'allow' => 'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture', |
204
|
1 |
|
'allowfullscreen' => '', |
205
|
1 |
|
'style' => 'position:absolute; top:0; left:0; width:100%; height:100%; border:0', |
206
|
1 |
|
], |
207
|
1 |
|
], |
208
|
1 |
|
]; |
209
|
1 |
|
$youtube = [ |
210
|
1 |
|
'extent' => $link['extent'], |
211
|
1 |
|
'element' => [ |
212
|
1 |
|
'name' => 'div', |
213
|
1 |
|
'handler' => 'elements', |
214
|
1 |
|
'text' => [ |
215
|
1 |
|
$iframe['element'], |
216
|
1 |
|
], |
217
|
1 |
|
'attributes' => [ |
218
|
1 |
|
'style' => 'position:relative; padding-bottom:56.25%; height:0; overflow:hidden', |
219
|
1 |
|
'title' => $link['element']['attributes']['title'], |
220
|
1 |
|
], |
221
|
1 |
|
], |
222
|
1 |
|
]; |
223
|
1 |
|
if ((bool) $this->config->get('pages.body.images.caption.enabled')) { |
224
|
1 |
|
return $this->createFigure($youtube); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $youtube; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $link; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
1 |
|
protected function inlineImage($Excerpt) |
237
|
|
|
{ |
238
|
1 |
|
$InlineImage = parent::inlineImage($Excerpt); |
239
|
1 |
|
if (!isset($InlineImage)) { |
240
|
|
|
return null; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// normalize path |
244
|
1 |
|
$InlineImage['element']['attributes']['src'] = $this->normalizePath($InlineImage['element']['attributes']['src']); |
245
|
|
|
|
246
|
|
|
// should be lazy loaded? |
247
|
1 |
|
if ((bool) $this->config->get('pages.body.images.lazy.enabled') && !isset($InlineImage['element']['attributes']['loading'])) { |
248
|
1 |
|
$InlineImage['element']['attributes']['loading'] = 'lazy'; |
249
|
|
|
} |
250
|
|
|
// should be decoding async? |
251
|
1 |
|
if ((bool) $this->config->get('pages.body.images.decoding.enabled') && !isset($InlineImage['element']['attributes']['decoding'])) { |
252
|
1 |
|
$InlineImage['element']['attributes']['decoding'] = 'async'; |
253
|
|
|
} |
254
|
|
|
// add default class? |
255
|
1 |
|
if ((string) $this->config->get('pages.body.images.class')) { |
256
|
1 |
|
if (!\array_key_exists('class', $InlineImage['element']['attributes'])) { |
257
|
1 |
|
$InlineImage['element']['attributes']['class'] = ''; |
258
|
|
|
} |
259
|
1 |
|
$InlineImage['element']['attributes']['class'] .= ' ' . (string) $this->config->get('pages.body.images.class'); |
260
|
1 |
|
$InlineImage['element']['attributes']['class'] = trim($InlineImage['element']['attributes']['class']); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
// disable remote image handling? |
264
|
1 |
|
if (Util\Url::isUrl($InlineImage['element']['attributes']['src']) && !(bool) $this->config->get('pages.body.images.remote.enabled') ?? true) { |
265
|
|
|
return $InlineImage; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
// create asset |
269
|
1 |
|
$assetOptions = ['force_slash' => false]; |
270
|
1 |
|
if ((bool) $this->config->get('pages.body.images.remote.fallback.enabled')) { |
271
|
1 |
|
$assetOptions += ['remote_fallback' => (string) $this->config->get('pages.body.images.remote.fallback.path')]; |
272
|
|
|
} |
273
|
1 |
|
$asset = new Asset($this->builder, $InlineImage['element']['attributes']['src'], $assetOptions); |
274
|
1 |
|
$InlineImage['element']['attributes']['src'] = $asset; |
275
|
1 |
|
$width = $asset['width']; |
276
|
|
|
|
277
|
|
|
/* |
278
|
|
|
* Should be resized? |
279
|
|
|
*/ |
280
|
1 |
|
$shouldResize = false; |
281
|
1 |
|
$assetResized = null; |
282
|
|
|
if ( |
283
|
1 |
|
(bool) $this->config->get('pages.body.images.resize.enabled') |
284
|
1 |
|
&& isset($InlineImage['element']['attributes']['width']) |
285
|
1 |
|
&& (int) $InlineImage['element']['attributes']['width'] < $width |
286
|
|
|
) { |
287
|
1 |
|
$shouldResize = true; |
288
|
1 |
|
$width = (int) $InlineImage['element']['attributes']['width']; |
289
|
|
|
} |
290
|
|
|
if ( |
291
|
1 |
|
!$shouldResize |
292
|
1 |
|
&& (bool) $this->config->get('body.images.responsive.enabled') |
293
|
1 |
|
&& max($this->config->getAssetsImagesWidths()) < $width |
294
|
|
|
) { |
295
|
|
|
$shouldResize = true; |
296
|
|
|
$width = max($this->config->getAssetsImagesWidths()); |
297
|
|
|
} |
298
|
1 |
|
if ($shouldResize) { |
299
|
|
|
try { |
300
|
1 |
|
$assetResized = $asset->resize($width); |
301
|
1 |
|
$InlineImage['element']['attributes']['src'] = $assetResized; |
302
|
|
|
} catch (\Exception $e) { |
303
|
|
|
$this->builder->getLogger()->debug($e->getMessage()); |
304
|
|
|
|
305
|
|
|
return $InlineImage; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
// set width |
310
|
1 |
|
if (!isset($InlineImage['element']['attributes']['width'])) { |
311
|
1 |
|
$InlineImage['element']['attributes']['width'] = $width; |
312
|
|
|
} |
313
|
|
|
// set height |
314
|
1 |
|
if (!isset($InlineImage['element']['attributes']['height'])) { |
315
|
1 |
|
$InlineImage['element']['attributes']['height'] = $assetResized['height'] ?? $asset['height']; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
// placeholder |
319
|
|
|
if ( |
320
|
1 |
|
(!empty($this->config->get('pages.body.images.placeholder')) || isset($InlineImage['element']['attributes']['placeholder'])) |
321
|
1 |
|
&& \in_array($InlineImage['element']['attributes']['src']['subtype'], ['image/jpeg', 'image/png', 'image/gif']) |
322
|
|
|
) { |
323
|
1 |
|
if (!\array_key_exists('placeholder', $InlineImage['element']['attributes'])) { |
324
|
|
|
$InlineImage['element']['attributes']['placeholder'] = (string) $this->config->get('pages.body.images.placeholder'); |
325
|
|
|
} |
326
|
1 |
|
if (!\array_key_exists('style', $InlineImage['element']['attributes'])) { |
327
|
1 |
|
$InlineImage['element']['attributes']['style'] = ''; |
328
|
|
|
} |
329
|
1 |
|
$InlineImage['element']['attributes']['style'] = trim($InlineImage['element']['attributes']['style'], ';'); |
330
|
1 |
|
switch ($InlineImage['element']['attributes']['placeholder']) { |
331
|
1 |
|
case 'color': |
332
|
1 |
|
$InlineImage['element']['attributes']['style'] .= sprintf(';max-width:100%%;height:auto;background-color:%s;', Image::getDominantColor($InlineImage['element']['attributes']['src'])); |
333
|
1 |
|
break; |
334
|
1 |
|
case 'lqip': |
335
|
1 |
|
$InlineImage['element']['attributes']['style'] .= sprintf(';max-width:100%%;height:auto;background-image:url(%s);background-repeat:no-repeat;background-position:center;background-size:cover;', Image::getLqip($InlineImage['element']['attributes']['src'])); |
336
|
1 |
|
break; |
337
|
|
|
} |
338
|
1 |
|
unset($InlineImage['element']['attributes']['placeholder']); |
339
|
1 |
|
$InlineImage['element']['attributes']['style'] = trim($InlineImage['element']['attributes']['style']); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/* |
343
|
|
|
* Should be responsive? |
344
|
|
|
*/ |
345
|
1 |
|
$sizes = ''; |
346
|
1 |
|
if ((bool) $this->config->get('pages.body.images.responsive.enabled')) { |
347
|
|
|
try { |
348
|
|
|
if ( |
349
|
1 |
|
$srcset = Image::buildSrcset( |
350
|
1 |
|
$assetResized ?? $asset, |
351
|
1 |
|
$this->config->getAssetsImagesWidths() |
352
|
1 |
|
) |
353
|
|
|
) { |
354
|
1 |
|
$InlineImage['element']['attributes']['srcset'] = $srcset; |
355
|
1 |
|
$sizes = Image::getSizes($InlineImage['element']['attributes']['class'] ?? '', (array) $this->config->getAssetsImagesSizes()); |
356
|
1 |
|
$InlineImage['element']['attributes']['sizes'] = $sizes; |
357
|
|
|
} |
358
|
|
|
} catch (\Exception $e) { |
359
|
|
|
$this->builder->getLogger()->debug($e->getMessage()); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/* |
364
|
|
|
<!-- if title: a <figure> is required to put in it a <figcaption> --> |
365
|
|
|
<figure> |
366
|
|
|
<!-- if WebP is enabled: a <picture> is required for the WebP <source> --> |
367
|
|
|
<picture> |
368
|
|
|
<source type="image/webp" |
369
|
|
|
srcset="..." |
370
|
|
|
sizes="..." |
371
|
|
|
> |
372
|
|
|
<img src="..." |
373
|
|
|
srcset="..." |
374
|
|
|
sizes="..." |
375
|
|
|
> |
376
|
|
|
</picture> |
377
|
|
|
<figcaption><!-- title --></figcaption> |
378
|
|
|
</figure> |
379
|
|
|
*/ |
380
|
|
|
|
381
|
1 |
|
$image = $InlineImage; |
382
|
|
|
|
383
|
|
|
// converts image (JPEG, PNG or GIF) to WebP and put it in picture > source |
384
|
|
|
if ( |
385
|
1 |
|
((bool) $this->config->get('pages.body.images.webp.enabled') ?? false) |
386
|
1 |
|
&& \in_array($InlineImage['element']['attributes']['src']['subtype'], ['image/jpeg', 'image/png', 'image/gif']) |
387
|
|
|
) { |
388
|
|
|
try { |
389
|
|
|
// InlineImage src must be an Asset instance |
390
|
1 |
|
if (!$InlineImage['element']['attributes']['src'] instanceof Asset) { |
391
|
|
|
throw new RuntimeException(sprintf('Asset "%s" can\'t be converted to WebP.', $InlineImage['element']['attributes']['src'])); |
392
|
|
|
} |
393
|
|
|
// abord if InlineImage is an animated GIF |
394
|
1 |
|
if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) { |
395
|
1 |
|
throw new RuntimeException(sprintf('Asset "%s" is an animated GIF and can\'t be converted to WebP.', $InlineImage['element']['attributes']['src'])); |
396
|
|
|
} |
397
|
1 |
|
$assetWebp = $InlineImage['element']['attributes']['src']->webp(); |
398
|
|
|
$srcset = ''; |
399
|
|
|
// build responsives WebP? |
400
|
|
|
if ((bool) $this->config->get('pages.body.images.responsive.enabled')) { |
401
|
|
|
try { |
402
|
|
|
$srcset = Image::buildSrcset( |
403
|
|
|
$assetWebp, |
404
|
|
|
$this->config->getAssetsImagesWidths() |
405
|
|
|
); |
406
|
|
|
} catch (\Exception $e) { |
407
|
|
|
$this->builder->getLogger()->debug($e->getMessage()); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
// if not, default image as srcset |
411
|
|
|
if (empty($srcset)) { |
412
|
|
|
$srcset = (string) $assetWebp; |
413
|
|
|
} |
414
|
|
|
$picture = [ |
415
|
|
|
'extent' => $InlineImage['extent'], |
416
|
|
|
'element' => [ |
417
|
|
|
'name' => 'picture', |
418
|
|
|
'handler' => 'elements', |
419
|
|
|
'attributes' => [ |
420
|
|
|
'title' => $image['element']['attributes']['title'], |
421
|
|
|
], |
422
|
|
|
], |
423
|
|
|
]; |
424
|
|
|
$source = [ |
425
|
|
|
'element' => [ |
426
|
|
|
'name' => 'source', |
427
|
|
|
'attributes' => [ |
428
|
|
|
'type' => 'image/webp', |
429
|
|
|
'srcset' => $srcset, |
430
|
|
|
'sizes' => $sizes, |
431
|
|
|
'width' => $InlineImage['element']['attributes']['width'], |
432
|
|
|
'height' => $InlineImage['element']['attributes']['height'], |
433
|
|
|
], |
434
|
|
|
], |
435
|
|
|
]; |
436
|
|
|
$picture['element']['text'][] = $source['element']; |
437
|
|
|
unset($image['element']['attributes']['title']); |
438
|
|
|
$picture['element']['text'][] = $image['element']; |
439
|
|
|
$image = $picture; |
440
|
1 |
|
} catch (\Exception $e) { |
441
|
1 |
|
$this->builder->getLogger()->debug($e->getMessage()); |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
// if title: put the <img> (or <picture>) in a <figure> and create a <figcaption> |
446
|
1 |
|
if ((bool) $this->config->get('pages.body.images.caption.enabled')) { |
447
|
1 |
|
return $this->createFigure($image); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
return $image; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Image block. |
455
|
|
|
*/ |
456
|
1 |
|
protected function blockImage($Excerpt) |
457
|
|
|
{ |
458
|
1 |
|
if (1 !== preg_match($this->regexImage, $Excerpt['text'])) { |
459
|
|
|
return; |
460
|
|
|
} |
461
|
|
|
|
462
|
1 |
|
$InlineImage = $this->inlineImage($Excerpt); |
463
|
1 |
|
if (!isset($InlineImage)) { |
464
|
|
|
return; |
465
|
|
|
} |
466
|
|
|
|
467
|
1 |
|
return $InlineImage; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Note block-level markup. |
472
|
|
|
* |
473
|
|
|
* :::tip |
474
|
|
|
* **Tip:** This is an advice. |
475
|
|
|
* ::: |
476
|
|
|
* |
477
|
|
|
* Code inspired by https://github.com/sixlive/parsedown-alert from TJ Miller (@sixlive). |
478
|
|
|
*/ |
479
|
1 |
|
protected function blockNote($block) |
480
|
|
|
{ |
481
|
1 |
|
if (preg_match('/:::(.*)/', $block['text'], $matches)) { |
482
|
1 |
|
$block = [ |
483
|
1 |
|
'char' => ':', |
484
|
1 |
|
'element' => [ |
485
|
1 |
|
'name' => 'aside', |
486
|
1 |
|
'text' => '', |
487
|
1 |
|
'attributes' => [ |
488
|
1 |
|
'class' => 'note', |
489
|
1 |
|
], |
490
|
1 |
|
], |
491
|
1 |
|
]; |
492
|
1 |
|
if (!empty($matches[1])) { |
493
|
1 |
|
$block['element']['attributes']['class'] .= " note-{$matches[1]}"; |
494
|
|
|
} |
495
|
|
|
|
496
|
1 |
|
return $block; |
497
|
|
|
} |
498
|
|
|
} |
499
|
|
|
|
500
|
1 |
|
protected function blockNoteContinue($line, $block) |
501
|
|
|
{ |
502
|
1 |
|
if (isset($block['complete'])) { |
503
|
1 |
|
return; |
504
|
|
|
} |
505
|
1 |
|
if (preg_match('/:::/', $line['text'])) { |
506
|
1 |
|
$block['complete'] = true; |
507
|
|
|
|
508
|
1 |
|
return $block; |
509
|
|
|
} |
510
|
1 |
|
$block['element']['text'] .= $line['text'] . "\n"; |
511
|
|
|
|
512
|
1 |
|
return $block; |
513
|
|
|
} |
514
|
|
|
|
515
|
1 |
|
protected function blockNoteComplete($block) |
516
|
|
|
{ |
517
|
1 |
|
$block['element']['rawHtml'] = $this->text($block['element']['text']); |
518
|
1 |
|
unset($block['element']['text']); |
519
|
|
|
|
520
|
1 |
|
return $block; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* Apply Highlight to code blocks. |
525
|
|
|
*/ |
526
|
1 |
|
protected function blockFencedCodeComplete($block) |
527
|
|
|
{ |
528
|
1 |
|
if (!(bool) $this->config->get('pages.body.highlight.enabled')) { |
529
|
|
|
return $block; |
530
|
|
|
} |
531
|
1 |
|
if (!isset($block['element']['text']['attributes'])) { |
532
|
|
|
return $block; |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
try { |
536
|
1 |
|
$code = $block['element']['text']['text']; |
537
|
1 |
|
$languageClass = $block['element']['text']['attributes']['class']; |
538
|
1 |
|
$language = explode('-', $languageClass); |
539
|
1 |
|
$highlighted = $this->highlighter->highlight($language[1], $code); |
540
|
1 |
|
$block['element']['text']['attributes']['class'] = vsprintf('%s hljs %s', [ |
541
|
1 |
|
$languageClass, |
542
|
1 |
|
$highlighted->language, |
543
|
1 |
|
]); |
544
|
1 |
|
$block['element']['text']['rawHtml'] = $highlighted->value; |
545
|
1 |
|
$block['element']['text']['allowRawHtmlInSafeMode'] = true; |
546
|
1 |
|
unset($block['element']['text']['text']); |
547
|
|
|
} catch (\Exception $e) { |
548
|
|
|
$this->builder->getLogger()->debug($e->getMessage()); |
549
|
|
|
} finally { |
550
|
1 |
|
return $block; |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* {@inheritdoc} |
556
|
|
|
*/ |
557
|
1 |
|
protected function parseAttributeData($attributeString) |
558
|
|
|
{ |
559
|
1 |
|
$attributes = preg_split('/[ ]+/', $attributeString, -1, PREG_SPLIT_NO_EMPTY); |
560
|
1 |
|
$Data = []; |
561
|
1 |
|
$HtmlAtt = []; |
562
|
|
|
|
563
|
1 |
|
foreach ($attributes as $attribute) { |
564
|
1 |
|
switch ($attribute[0]) { |
565
|
1 |
|
case '#': // ID |
566
|
1 |
|
$Data['id'] = substr($attribute, 1); |
567
|
1 |
|
break; |
568
|
1 |
|
case '.': // Classes |
569
|
1 |
|
$classes[] = substr($attribute, 1); |
570
|
1 |
|
break; |
571
|
|
|
default: // Attributes |
572
|
1 |
|
parse_str($attribute, $parsed); |
573
|
1 |
|
$HtmlAtt = array_merge($HtmlAtt, $parsed); |
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
|
577
|
1 |
|
if (isset($classes)) { |
578
|
1 |
|
$Data['class'] = implode(' ', $classes); |
579
|
|
|
} |
580
|
1 |
|
if (!empty($HtmlAtt)) { |
581
|
1 |
|
foreach ($HtmlAtt as $a => $v) { |
582
|
1 |
|
$Data[$a] = trim($v, '"'); |
583
|
|
|
} |
584
|
|
|
} |
585
|
|
|
|
586
|
1 |
|
return $Data; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Turns a path relative to static or assets into a website relative path. |
591
|
|
|
* |
592
|
|
|
* "../../assets/images/img.jpeg" |
593
|
|
|
* -> |
594
|
|
|
* "/images/img.jpeg" |
595
|
|
|
*/ |
596
|
1 |
|
private function normalizePath(string $path): string |
597
|
|
|
{ |
598
|
|
|
// https://regex101.com/r/Rzguzh/1 |
599
|
1 |
|
$pattern = sprintf( |
600
|
1 |
|
'(\.\.\/)+(\b%s|%s\b)+(\/.*)', |
601
|
1 |
|
(string) $this->config->get('static.dir'), |
602
|
1 |
|
(string) $this->config->get('assets.dir') |
603
|
1 |
|
); |
604
|
1 |
|
$path = Util::joinPath($path); |
605
|
1 |
|
if (!preg_match('/' . $pattern . '/is', $path, $matches)) { |
606
|
1 |
|
return $path; |
607
|
|
|
} |
608
|
|
|
|
609
|
1 |
|
return $matches[3]; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* Create a media (video or audio) element from a link. |
614
|
|
|
*/ |
615
|
1 |
|
private function createMediaFromLink(array $link, string $type = 'video'): array |
616
|
|
|
{ |
617
|
1 |
|
$block = [ |
618
|
1 |
|
'extent' => $link['extent'], |
619
|
1 |
|
'element' => [ |
620
|
1 |
|
'text' => $link['element']['text'], |
621
|
1 |
|
], |
622
|
1 |
|
]; |
623
|
1 |
|
$block['element']['attributes'] = $link['element']['attributes']; |
624
|
1 |
|
unset($block['element']['attributes']['href']); |
625
|
1 |
|
$block['element']['attributes']['src'] = (string) new Asset($this->builder, $link['element']['attributes']['href'], ['force_slash' => false]); |
626
|
|
|
switch ($type) { |
627
|
1 |
|
case 'video': |
628
|
1 |
|
$block['element']['name'] = 'video'; |
629
|
1 |
|
if (!isset($block['element']['attributes']['controls'])) { |
630
|
1 |
|
$block['element']['attributes']['autoplay'] = ''; |
631
|
1 |
|
$block['element']['attributes']['loop'] = ''; |
632
|
|
|
} |
633
|
1 |
|
if (isset($block['element']['attributes']['poster'])) { |
634
|
1 |
|
$block['element']['attributes']['poster'] = (string) new Asset($this->builder, $block['element']['attributes']['poster'], ['force_slash' => false]); |
635
|
|
|
} |
636
|
|
|
|
637
|
1 |
|
return $block; |
638
|
1 |
|
case 'audio': |
639
|
1 |
|
$block['element']['name'] = 'audio'; |
640
|
|
|
|
641
|
1 |
|
return $block; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
throw new \Exception(sprintf('Can\'t create %s from "%s".', $type, $link['element']['attributes']['href'])); |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Create a figure / caption element. |
649
|
|
|
*/ |
650
|
1 |
|
private function createFigure(array $inline): array |
651
|
|
|
{ |
652
|
1 |
|
if (empty($inline['element']['attributes']['title'])) { |
653
|
1 |
|
return $inline; |
654
|
|
|
} |
655
|
|
|
|
656
|
1 |
|
$titleRawHtml = $this->line($inline['element']['attributes']['title']); |
657
|
1 |
|
$inline['element']['attributes']['title'] = strip_tags($titleRawHtml); |
658
|
|
|
|
659
|
1 |
|
$figcaption = [ |
660
|
1 |
|
'element' => [ |
661
|
1 |
|
'name' => 'figcaption', |
662
|
1 |
|
'allowRawHtmlInSafeMode' => true, |
663
|
1 |
|
'rawHtml' => $titleRawHtml, |
664
|
1 |
|
], |
665
|
1 |
|
]; |
666
|
1 |
|
$figure = [ |
667
|
1 |
|
'extent' => $inline['extent'], |
668
|
1 |
|
'element' => [ |
669
|
1 |
|
'name' => 'figure', |
670
|
1 |
|
'handler' => 'elements', |
671
|
1 |
|
'text' => [ |
672
|
1 |
|
$inline['element'], |
673
|
1 |
|
$figcaption['element'], |
674
|
1 |
|
], |
675
|
1 |
|
], |
676
|
1 |
|
]; |
677
|
|
|
|
678
|
1 |
|
return $figure; |
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
|