1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pageon\Lib; |
4
|
|
|
|
5
|
|
|
use Pageon\Html\Image\ImageFactory; |
6
|
|
|
use \Parsedown as LibParsedown; |
7
|
|
|
use Stitcher\Exception\InvalidConfiguration; |
8
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This class adds extensions to the Parsedown library: |
12
|
|
|
* |
13
|
|
|
* - Code block classes. |
14
|
|
|
* - External links with `target="_blank"`. |
15
|
|
|
* - Images are parsed with with the image factory |
16
|
|
|
* |
17
|
|
|
* Class Parsedown |
18
|
|
|
*/ |
19
|
|
|
class Parsedown extends LibParsedown |
20
|
|
|
{ |
21
|
|
|
private $imageFactory; |
22
|
|
|
|
23
|
5 |
|
public function __construct(ImageFactory $imageFactory) { |
24
|
5 |
|
$this->imageFactory = $imageFactory; |
25
|
5 |
|
} |
26
|
|
|
|
27
|
|
|
protected function element(array $Element) { |
28
|
|
|
$markup = parent::element($Element); |
29
|
|
|
|
30
|
|
|
if (isset($Element['attributes']['href']) && strpos($Element['attributes']['href'], '*') === 0) { |
31
|
|
|
return $this->parseBlankLink($Element); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (isset($Element['attributes']['srcset'])) { |
35
|
|
|
return $this->parseImageWithSrcset($Element); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $markup; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function blockFencedCode($Line) { |
42
|
|
|
$block = parent::blockFencedCode($Line); |
43
|
|
|
|
44
|
|
|
if (isset($block['element']['text']['attributes']['class'])) { |
45
|
|
|
$block['element']['attributes']['class'] = $block['element']['text']['attributes']['class']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $block; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function inlineImage($Excerpt) { |
52
|
|
|
$Inline = parent::inlineImage($Excerpt); |
53
|
|
|
|
54
|
|
|
if (!isset($Inline['element']['attributes']['src'])) { |
55
|
|
|
return $Inline; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$src = $Inline['element']['attributes']['src']; |
59
|
|
|
|
60
|
|
|
try { |
61
|
|
|
$responsiveImage = $this->imageFactory->create($src); |
62
|
|
|
} catch (FileNotFoundException $e) { |
63
|
|
|
throw InvalidConfiguration::fileNotFound($src); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$Inline['element']['attributes']['srcset'] = $responsiveImage->srcset() ?? null; |
67
|
|
|
$Inline['element']['attributes']['sizes'] = $responsiveImage->sizes() ?? null; |
68
|
|
|
$Inline['element']['attributes']['alt'] = $Inline['element']['attributes']['alt'] ?? null; |
69
|
|
|
|
70
|
|
|
return $Inline; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function parseBlankLink($Element): string { |
74
|
|
|
$href = $Element['attributes']['href']; |
75
|
|
|
$href = substr($href, 1); |
76
|
|
|
|
77
|
|
|
return "<a href='$href' target='_blank' rel='noreferrer noopener'>{$Element['text']}</a>"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function parseImageWithSrcset($Element): string { |
81
|
|
|
$src = $Element['attributes']['src']; |
82
|
|
|
$srcset = $Element['attributes']['srcset']; |
83
|
|
|
$sizes = $Element['attributes']['sizes']; |
84
|
|
|
$alt = $Element['attributes']['alt']; |
85
|
|
|
|
86
|
|
|
return "<img src=\"{$src}\" srcset=\"{$srcset}\" sizes=\"{$sizes}\" alt=\"{$alt}\" />"; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|