Last active
November 26, 2021 09:22
-
-
Save kantoniak/b1a5c7889e5583824487dc78d93da7cd to your computer and use it in GitHub Desktop.
Parsedown extension to wrap single-image lines in <figure>, not <p>. Public domain. See https://github.com/erusev/parsedown/issues/180#issuecomment-451486639
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FigureExtParsedown extends Parsedown { | |
// Matches Markdown image definition | |
private $MarkdownImageRegex = "~^!\[.*?\]\(.*?\)$~"; | |
public function __construct () { | |
// Add blockFigure to non-exclusive handlers for text starting with ! | |
$this->BlockTypes['!'][] = 'Figure'; | |
} | |
protected function blockFigure($Line) { | |
// If line does not match image def, don't handle it | |
if (1 !== preg_match($this->MarkdownImageRegex, $Line['text'])) { | |
return; | |
} | |
$InlineImage = $this->inlineImage($Line); | |
if (!isset($InlineImage)) { | |
return; | |
} | |
$FigureBlock = array( | |
'element' => array( | |
'name' => 'figure', | |
'handler' => 'elements', | |
'text' => array( | |
$InlineImage['element'] | |
) | |
), | |
); | |
// Add figcaption if title set | |
if (!empty($InlineImage['element']['attributes']['title'])) { | |
$InlineFigcaption = array( | |
'element' => array( | |
'name' => 'figcaption', | |
'text' => $InlineImage['element']['attributes']['title'] | |
), | |
); | |
$FigureBlock['element']['text'][] = $InlineFigcaption['element']; | |
} | |
return $FigureBlock; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's correct, thank you for pointing that out!