Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple Pico plugin to wrap Markdown images in <figure> elements. Pico is a stupidly simple, blazing fast, flat file CMS. http://picocms.org/
<?php
/**
* Parsedown figure extension
*
* Parsedown extension to wrap single-image lines in <figure>, not <p>.
*
* @author Krzysztof Antoniak
* @link https://gist.github.com/kantoniak/b1a5c7889e5583824487dc78d93da7cd
* @license Public Domain
* @version fdb7c2c3ccf561e2e84b31096d6ba1257b4e9058 (2019-08-31T13:49:00+0200)
*/
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;
}
}
<?php
/**
* Parsedown figure extension for Pico
*
* Uses Krzysztof Antoniak's FigureExtParsedown Parsedown plugin in Pico
* to wrap Markdown images in <figure> elements.
*
* @author Daniel Rudolf
* @link http://picocms.org
* @license http://opensource.org/licenses/MIT The MIT License
* @version 0.0.1
*/
class FigureExtParsedownPicoPlugin extends AbstractPicoPlugin
{
const API_VERSION = 2;
public function __construct(Pico $pico)
{
parent::__construct($pico);
require_once(__DIR__ . '/FigureExtParsedown.php');
}
public function onParsedownRegistered(Parsedown &$parsedown)
{
$config = $this->getPico()->getConfig();
$parsedown = new FigureExtParsedown();
$parsedown->setBreaksEnabled((bool) $config['content_config']['breaks']);
$parsedown->setMarkupEscaped((bool) $config['content_config']['escape']);
$parsedown->setUrlsLinked((bool) $config['content_config']['auto_urls']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.