Parsedown::inlineImage()   F
last analyzed

Complexity

Conditions 41
Paths > 20000

Size

Total Lines 226
Code Lines 124

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 6 Features 0
Metric Value
cc 41
eloc 124
nc 1142605
nop 1
dl 0
loc 226
rs 0
c 8
b 6
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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