| Total Complexity | 55 |
| Total Lines | 382 |
| Duplicated Lines | 0 % |
| Changes | 15 | ||
| Bugs | 7 | Features | 0 |
Complex classes like Parsedown often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Parsedown, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Parsedown extends \ParsedownToC |
||
| 24 | { |
||
| 25 | /** @var Builder */ |
||
| 26 | protected $builder; |
||
| 27 | |||
| 28 | /** {@inheritdoc} */ |
||
| 29 | protected $regexAttribute = '(?:[#.][-\w:\\\]+[ ]*|[-\w:\\\]+(?:=(?:["\'][^\n]*?["\']|[^\s]+)?)?[ ]*)'; |
||
| 30 | |||
| 31 | /** Valid a media block (image, audio or video) */ |
||
| 32 | protected $MarkdownMediaRegex = "~^!\[.*?\]\(.*?\)~"; |
||
| 33 | |||
| 34 | /** @var Highlighter */ |
||
| 35 | protected $highlighter; |
||
| 36 | |||
| 37 | public function __construct(Builder $builder) |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Insert inline. |
||
| 63 | * e.g.: ++text++ -> <ins>text</ins>. |
||
| 64 | */ |
||
| 65 | protected function inlineInsert($Excerpt) |
||
| 78 | ], |
||
| 79 | ]; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | protected function inlineImage($Excerpt) |
||
| 87 | { |
||
| 88 | $image = parent::inlineImage($Excerpt); |
||
| 89 | if (!isset($image)) { |
||
| 90 | return null; |
||
| 91 | } |
||
| 92 | |||
| 93 | // clean source path / URL |
||
| 94 | $image['element']['attributes']['src'] = $this->cleanUrl($image['element']['attributes']['src']); |
||
| 95 | |||
| 96 | // should be lazy loaded? |
||
| 97 | if ($this->builder->getConfig()->get('body.images.lazy.enabled')) { |
||
| 98 | $image['element']['attributes']['loading'] = 'lazy'; |
||
| 99 | } |
||
| 100 | |||
| 101 | // disable remote image handling? |
||
| 102 | if (Util\Url::isUrl($image['element']['attributes']['src']) && !$this->builder->getConfig()->get('body.images.remote.enabled') ?? true) { |
||
| 103 | return $image; |
||
| 104 | } |
||
| 105 | |||
| 106 | // create asset |
||
| 107 | $asset = new Asset($this->builder, $image['element']['attributes']['src'], ['force_slash' => false]); |
||
| 108 | $image['element']['attributes']['src'] = $asset; |
||
| 109 | $width = $asset->getWidth(); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Should be resized? |
||
| 113 | */ |
||
| 114 | $assetResized = null; |
||
| 115 | if (isset($image['element']['attributes']['width']) |
||
| 116 | && (int) $image['element']['attributes']['width'] < $width |
||
| 117 | && $this->builder->getConfig()->get('body.images.resize.enabled') |
||
| 118 | ) { |
||
| 119 | $width = (int) $image['element']['attributes']['width']; |
||
| 120 | |||
| 121 | try { |
||
| 122 | $assetResized = $asset->resize($width); |
||
| 123 | } catch (\Exception $e) { |
||
| 124 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 125 | |||
| 126 | return $image; |
||
| 127 | } |
||
| 128 | $image['element']['attributes']['src'] = $assetResized; |
||
| 129 | } |
||
| 130 | |||
| 131 | // set width |
||
| 132 | if (!isset($image['element']['attributes']['width']) && $asset['type'] == 'image') { |
||
| 133 | $image['element']['attributes']['width'] = $width; |
||
| 134 | } |
||
| 135 | // set height |
||
| 136 | if (!isset($image['element']['attributes']['height']) && $asset['type'] == 'image') { |
||
| 137 | $image['element']['attributes']['height'] = ($assetResized ?? $asset)->getHeight(); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Should be responsive? |
||
| 142 | */ |
||
| 143 | if ($this->builder->getConfig()->get('body.images.responsive.enabled')) { |
||
| 144 | if ($srcset = Image::buildSrcset( |
||
| 145 | $assetResized ?? $asset, |
||
| 146 | $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
||
| 147 | )) { |
||
| 148 | $image['element']['attributes']['srcset'] = $srcset; |
||
| 149 | $image['element']['attributes']['sizes'] = $this->builder->getConfig()->get('assets.images.responsive.sizes.default'); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | return $image; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Media block support: |
||
| 158 | * 1. <picture>/<source> for WebP images |
||
| 159 | * 2. <audio> and <video> elements |
||
| 160 | * 3. <figure>/<figcaption> for element with a title. |
||
| 161 | */ |
||
| 162 | protected function blockMedia($Excerpt) |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Note block-level markup. |
||
| 291 | * |
||
| 292 | * :::tip |
||
| 293 | * **Tip:** This is an advice. |
||
| 294 | * ::: |
||
| 295 | * |
||
| 296 | * Code inspired by https://github.com/sixlive/parsedown-alert from TJ Miller (@sixlive). |
||
| 297 | */ |
||
| 298 | protected function blockNote($block) |
||
| 299 | { |
||
| 300 | if (preg_match('/:::(.*)/', $block['text'], $matches)) { |
||
| 301 | return [ |
||
| 302 | 'char' => ':', |
||
| 303 | 'element' => [ |
||
| 304 | 'name' => 'aside', |
||
| 305 | 'text' => '', |
||
| 306 | 'attributes' => [ |
||
| 307 | 'class' => "note note-{$matches[1]}", |
||
| 308 | ], |
||
| 309 | ], |
||
| 310 | ]; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | protected function blockNoteContinue($line, $block) |
||
| 327 | } |
||
| 328 | |||
| 329 | protected function blockNoteComplete($block) |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Apply Highlight to code blocks. |
||
| 339 | */ |
||
| 340 | protected function blockFencedCodeComplete($block) |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | protected function parseAttributeData($attributeString) |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Returns URL without query string. |
||
| 401 | */ |
||
| 402 | private function cleanUrl(string $path): string |
||
| 407 |